PHP Class
It is possible to implement a new action 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\ConditionalTextAction
interface.
The class can be named as you prefer, there are no restrictions applied. It is good practice to always include Action
as class suffix.
<?php
// No direct access
defined('_JEXEC') or die;
use E4J\VikRestaurants\Mail\Mail;
use E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextAction;
class CustomAction implements ConditionalTextAction
{
/**
* @inheritDoc
*/
public function preflight(Mail $mail)
{
// do some stuff before applying the action
}
/**
* @inheritDoc
*/
public function apply(Mail $mail)
{
// apply the action
}
}
The implementation of the interface as previously described let you interact with the action only via code. This because the selection of the actions from the management page of a conditional text takes only those actions 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\Mail;
use E4J\VikRestaurants\Mail\ConditionalText\ConditionalTextActionAware;
class CustomAction extends ConditionalTextActionAware
{
/**
* @inheritDoc
*
* @see ConditionalTextAction
*/
public function preflight(Mail $mail)
{
// do some stuff before applying the action
}
/**
* @inheritDoc
*
* @see ConditionalTextAction
*/
public function apply(Mail $mail)
{
// apply the action
}
/**
* Returns the identifier of the conditional text action.
*
* @return string
*
* @see ConditionalTextManageable
*/
public function getID()
{
return 'custom';
}
/**
* Returns a readable name for the conditional text action.
*
* @return string
*
* @see ConditionalTextManageable
*/
public function getName()
{
return 'Custom Action';
}
/**
* Returns an extended description for the conditional text action.
*
* @return string
*
* @see ConditionalTextManageable
*/
public function getDescription()
{
return 'This is how this action works';
}
/**
* Returns an icon (FontAwesome) for the conditional text action.
*
* @see ConditionalTextManageable
*/
public function getIcon()
{
return 'fas fa-plug';
}
/**
* Renders a summary text according to the parameters of the conditional text action.
*
* @return string
*
* @see ConditionalTextManageable
*/
public function getSummary()
{
return 'Here you can summarize the parameters configured for this action.';
}
/**
* @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 action provider, as described by the onSetupConditionalTexts
hook.
Let's take the following plugin structure.
- myplugin/
- myplugin.php
- actions/
- custom_action_one.php
- custom_action_two.php
According to the structure of these files, both the actions 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 action provider to create the first action
$factory->registerActionProvider('custom_action_one', function($options) {
// autoload the file ./actions/custom_action_one.php, which must declare a class named as:
// FirstCustomConditionalTextAction
require_once dirname(__FILE__) . '/actions/custom_action_one.php';
// instantiate the class
return new FirstCustomConditionalTextAction($options);
});
// register a action provider to create the second action
$factory->registerActionProvider('custom_action_two', function($options) {
// autoload the file ./actions/custom_action_two.php, which must declare a class named as:
// SecondCustomConditionalTextAction
require_once dirname(__FILE__) . '/actions/custom_action_two.php';
// instantiate the class
return new SecondCustomConditionalTextAction($options);
});
}