PHP Class
It is possible to implement a new filter for the conditional texts by creating an apposite PHP file, containing a new class that implements the methods defined by the E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextFilter
interface.
The class can be named as you prefer, there are no restrictions applied. It is good practice to always include Filter
as class suffix.
<?php
// No direct access
defined('_JEXEC') or die;
use E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextFilter;
class CustomFilter implements ConditionalTextFilter
{
/**
* @inheritDoc
*/
public function isEligible(string $templateId, array $data)
{
return false;
}
}
The implementation of the interface as previously described let you interact with the filter only via code. This because the selection of the filters from the management page of a conditional text takes only those filters that implement the E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextManageable
interface.
The framework of VikRestaurants already provides a few abstractions to speed up this kind of implementation.
Here's how the extended class should look like.
<?php
// No direct access
defined('_JEXEC') or die;
use E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextFilterAware;
class CustomFilter extends ConditionalTextFilterAware
{
/**
* @inheritDoc
*
* @see ConditionalTextFilter
*/
public function isEligible(string $templateId, array $data)
{
return false;
}
/**
* Returns the identifier of the conditional text filter.
*
* @return string
*
* @see ConditionalTextManageable
*/
public function getID()
{
return 'custom';
}
/**
* Returns a readable name for the conditional text filter.
*
* @return string
*
* @see ConditionalTextManageable
*/
public function getName()
{
return 'Custom Filter';
}
/**
* Returns an extended description for the conditional text filter.
*
* @return string
*
* @see ConditionalTextManageable
*/
public function getDescription()
{
return 'This is how this filter works';
}
/**
* Returns an icon (FontAwesome) for the conditional text filter.
*
* @see ConditionalTextManageable
*/
public function getIcon()
{
return 'fas fa-sliders-h';
}
/**
* Renders a summary text according to the parameters of the conditional text filter.
*
* @return string
*
* @see ConditionalTextManageable
*/
public function getSummary()
{
return 'Here you can summarize the parameters configured for this filter.';
}
/**
* @inheritDoc
*
* @see ConditionalTextManageable
*/
public function getForm()
{
return [];
}
}
Where should I place that file?
You can place that file wherever you want and name it as you wish. Just remember to automatically load it during the registration of the filter provider, as described by the onSetupConditionalTexts
hook.
Let's take the following plugin structure.
- myplugin/
- myplugin.php
- filters/
- custom_filter_one.php
- custom_filter_two.php
According to the structure of these files, both the filters can be supported by using the following code, which should be placed within the main myplugin.php file.
// subscribe to the hook used to set up the conditional texts framework of VikRestaurants
public function onSetupConditionalTexts($factory)
{
// register a filter provider to create the first filter
$factory->registerFilterProvider('custom_filter_one', function($options) {
// autoload the file ./filters/custom_filter_one.php, which must declare a class named as:
// FirstCustomConditionalTextFilter
require_once dirname(__FILE__) . '/filters/custom_filter_one.php';
// instantiate the class
return new FirstCustomConditionalTextFilter($options);
});
// register a filter provider to create the second filter
$factory->registerFilterProvider('custom_filter_two', function($options) {
// autoload the file ./filters/custom_filter_two.php, which must declare a class named as:
// SecondCustomConditionalTextFilter
require_once dirname(__FILE__) . '/filters/custom_filter_two.php';
// instantiate the class
return new SecondCustomConditionalTextFilter($options);
});
}