public function isEligible(string $templateId, array $data) : bool

Checks whether the conditional text is eligible or not.


Description

This method is used by the system to check whether the current filter allows the usage of the conditional text to which it belongs.

The actions of a conditional text are applied only in case all the assigned filters (if any) are eligible.

In case the class extends the ConditionalTextFilterAware abstraction, it is possible to use the code below to access the configuration of the filter.

$this->options->get('param', false);

Where the first argument is the name of the setting defined by the getForm() method and the second one is the default value to use in case of no stored setting.


Parameters

$templateId

(string)  The template identifier used to send the mail notification. The template identifier is usually built as {group}.{alias}, where the group can be restaurant or takeaway, while the alias one of the following values: customer, administrator, cancellation, review, stock.

$data

(array)  The data wrapped and provided by the mail template. The arguments provided by this array may vary depending on the template identifier. In example, the first argument of a customer notification is always an object holding the reservation/order details.

Return Value

Boolean. Whether the filters allows the usage of the conditional text.


Example

The example below explains how to implement a filter that allows the conditional texts applicability only for restaurant reservations assigned to specific tables.

/**
 * @inheritDoc
 */
public function isEligible(string $templateId, array $data)
{
    // take the first argument from the template data
    $reservation = $data[0] ?? null;

    // the received argument must be an object holding the reservation details
    if (!$reservation instanceof VREOrderRestaurant) {
        // the provided e-mail template is not observable
        return false;
    }

    // take the list of configured tables
    $acceptedTables = (array) $this->options->get('tables', []);

    // scan all the tables assigned to the current reservation
    foreach ($reservation->tables as $table) {
        // make sure the current table is contained within the list of configured ones
        if (in_array($table->id, $acceptedTables)) {
            // table allowed, we can apply the actions of this conditional text
            return true;
        }
    }

    // no matching tables
    return false;
}
Last Update: 2023-12-29 14:15
Helpful?