class ActionSet in Business Rules 8
Same name and namespace in other branches
- 2.x src/Plugin/BusinessRulesAction/ActionSet.php \Drupal\business_rules\Plugin\BusinessRulesAction\ActionSet
Class ActionSet.
@package Drupal\business_rules\Plugin\BusinessRulesAction
Plugin annotation
@BusinessRulesAction(
id = "action_set",
label = @Translation("Action set"),
group = @Translation("Action"),
description = @Translation("Set of actions. Only actions with same target Entity and Bundles can be included on the set."),
reactsOnIds = {},
isContextDependent = FALSE,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\business_rules\Plugin\BusinessRulesItemPluginBase implements BusinessRulesItemPluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesActionPlugin implements BusinessRulesActionPluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesAction\ActionSet
- class \Drupal\business_rules\Plugin\BusinessRulesActionPlugin implements BusinessRulesActionPluginInterface
- class \Drupal\business_rules\Plugin\BusinessRulesItemPluginBase implements BusinessRulesItemPluginInterface
Expanded class hierarchy of ActionSet
2 files declare their use of ActionSet
- ActionSetController.php in src/
Controller/ ActionSetController.php - ConfigEntityCalculateDependencyTest.php in tests/
src/ Unit/ ConfigEntityCalculateDependencyTest.php
File
- src/
Plugin/ BusinessRulesAction/ ActionSet.php, line 29
Namespace
Drupal\business_rules\Plugin\BusinessRulesActionView source
class ActionSet extends BusinessRulesActionPlugin {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* We don't want to use the same wait two times for an item.
*
* @var array
*/
private $usedWeight = [];
/**
* {@inheritdoc}
*/
public function __construct(array $configuration = [], $plugin_id = 'action_set', $plugin_definition = []) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $this->util->container
->get('entity_type.manager');
}
/**
* Return all others actions with the same target Entity and Bundle.
*
* @param \Drupal\business_rules\Entity\Action $action
* The business rules Action.
*
* @return array
* Array of actions matched.
*/
public static function getAvailableItems(Action $action) {
$actions = Action::loadMultiple();
$current_items = $action
->getSettings('items');
$actions_matched = [];
if (count($actions)) {
foreach ($actions as $a) {
if ($action
->id() != $a
->id() && self::checkInnerAction($action, $a) && !in_array($a
->id(), array_keys($current_items))) {
$actions_matched[] = $a;
}
}
}
return $actions_matched;
}
/**
* Do not show actionSets that already contains the main_action.
*
* Important to avoid infinite action check loops.
*
* @param \Drupal\business_rules\ActionInterface $main_action
* The main action.
* @param \Drupal\business_rules\ActionInterface $child_action
* The child action.
*
* @return bool
* I check succeed or fails.
*/
private static function checkInnerAction(ActionInterface $main_action, ActionInterface $child_action) {
$check = TRUE;
if ($child_action
->getType() == 'action_set') {
$actions = $child_action
->getSettings();
foreach ($actions as $action) {
$action = Action::load($action['action']);
if ($main_action
->id() == $action
->id()) {
$check = FALSE;
break;
}
elseif ($action
->getType() == 'action_set') {
$inner_actions = $action
->getSettings();
foreach ($inner_actions as $inner_action) {
$inner_action = Action::load($inner_action['action']);
$check = self::checkInnerAction($action, $inner_action);
}
}
}
}
return $check;
}
/**
* {@inheritdoc}
*/
public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
if ($item
->isNew()) {
$form_state
->set('business_rule_action', $item);
return [];
}
// The actions to be executed.
$settings['items'] = [
'#type' => 'details',
'#title' => t('Actions to execute'),
'#open' => TRUE,
];
$settings['items'][] = $this
->formItems($form, $form_state, $item);
return $settings;
}
/**
* Provide the form fields for add Business Rule's Items.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
* @param \Drupal\business_rules\ItemInterface $action
* The current action.
*
* @return array
* The render array.
*/
public function formItems(array $form, FormStateInterface $form_state, ItemInterface $action) {
$user_input = $form_state
->getUserInput();
$label = t('Item');
$raw_items = $action
->getSettings('items');
$items = [];
if (is_array($raw_items)) {
foreach ($raw_items as $key => $item) {
$itemObj = new BusinessRulesItemObject($key, $item['type'], $item['weight']);
$items[$key] = $itemObj;
}
uasort($items, function ($a, $b) {
return $a
->getWeight() < $b
->getWeight() ? -1 : 1;
});
}
$header = [
'item_type' => t('Type'),
'label' => $label,
'weight' => t('Weight'),
'id' => t('Machine name'),
'subtype' => t('Subtype'),
'description' => t('Description'),
'operations' => t('Operations'),
'type' => [
'data' => '',
'width' => '0px',
],
];
$table['items'] = [
'#type' => 'table',
'#header' => $header,
'#attributes' => [
'id' => 'business_rules-items',
],
'#empty' => t('There are currently no actions in this action set. Add one by selecting an option below.'),
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'items-order-weight',
],
],
];
if (is_array($items)) {
foreach ($items as $value) {
$item = Action::load($value
->getId());
$item_weight = !empty($item) ? $value
->getWeight() : '';
$route_remove_item = 'business_rules.action_set.items.remove';
if (!empty($item)) {
$key = $item
->id();
$listBuilder = $this->entityTypeManager
->getListBuilder($item
->getEntityTypeId());
$operations = $listBuilder
->buildOperations($item);
$operations['#links']['remove'] = [
'title' => t('Remove'),
'url' => Url::fromRoute($route_remove_item, [
'action_id' => $action
->id(),
'item_id' => $item
->id(),
'method' => 'nojs',
], [
'attributes' => [
'class' => [
'use-ajax',
],
],
]),
'weight' => 1,
];
uasort($operations['#links'], function ($a, $b) {
return $a['weight'] < $b['weight'] ? -1 : 1;
});
foreach ($operations['#links'] as $i => $link) {
$uri = $this->util
->getCurrentUri()
->toString();
$operations['#links'][$i]['url']
->setRouteParameter('destination', $uri);
}
$item_weight = $this
->generateItemWeight('item', $item_weight);
$weight = [
'#type' => 'weight',
'#title' => t('Weight for item'),
'#title_display' => 'invisible',
'#delta' => 100,
'#default_value' => $item_weight,
'#attributes' => [
'class' => [
'items-order-weight',
],
],
];
$table['items'][$key] = [
'#attributes' => [
'class' => 'draggable',
'id' => $item
->id(),
],
'#weight' => isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL,
'item_type' => [
'#markup' => $item
->getBusinessRuleItemTranslatedType(),
],
'item' => [
'#tree' => FALSE,
'label' => [
'#markup' => $item
->label(),
],
],
'weight' => $weight,
'id' => [
'#markup' => $item
->id(),
],
'subtype' => [
'#markup' => $item
->getTypeLabel(),
],
'description' => [
'#markup' => $item
->getDescription(),
],
'operations' => $operations,
'type' => [
'#type' => 'value',
'#value' => $value
->getType(),
],
];
}
}
}
$add_action = Link::createFromRoute(t('Add Action'), 'business_rules.action_set.items.table', [
'action_id' => $action
->id(),
'method' => 'nojs',
], [
'attributes' => [
'class' => [
'use-ajax',
],
],
]);
$table['add'][] = [
'data' => [
'add' => [
'#type' => 'markup',
'#markup' => $add_action
->toString(),
'#prefix' => '<div id="business_rule-add_buttons">',
'#suffix' => '</div>',
],
],
];
return $table;
}
/**
* Generate the item weight.
*
* @param string $settings_type
* The settings type: success|fail.
* @param int $weight
* The weight.
*
* @return int
* The generated weight
*/
private function generateItemWeight($settings_type, $weight) {
if (!isset($this->usedWeight[$settings_type])) {
$this->usedWeight[$settings_type][] = $weight;
return $weight;
}
if (!in_array($weight, $this->usedWeight[$settings_type])) {
$this->usedWeight[$settings_type][] = $weight;
return $weight;
}
else {
$weight++;
return $this
->generateItemWeight($settings_type, $weight);
}
}
/**
* {@inheritdoc}
*/
public function buildForm(array &$form, FormStateInterface $form_state) {
$action = $form_state
->get('business_rule_action');
if (!empty($action) && $action
->isNew()) {
$form['actions']['submit']['#value'] = t('Continue');
}
// We don't need variables for this action.
unset($form['variables']);
}
/**
* {@inheritdoc}
*/
public function processSettings(array $settings, ItemInterface $item) {
if (empty($settings['items'])) {
$settings['items'] = [];
}
else {
foreach ($settings['items'] as $key => $item) {
$settings['items'][$key]['id'] = $key;
}
}
return $settings;
}
/**
* {@inheritdoc}
*/
public function execute(ActionInterface $action, BusinessRulesEvent $event) {
$actions = $action
->getSettings('items');
$actions = BusinessRulesItemObject::itemsArrayToItemsObject($actions);
// Process items.
$this->processor
->processItems($actions, $event, $action
->id());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ActionSet:: |
protected | property | The entity type manager. | |
ActionSet:: |
private | property | We don't want to use the same wait two times for an item. | |
ActionSet:: |
public | function |
Form constructor. Overrides BusinessRulesItemPluginBase:: |
|
ActionSet:: |
private static | function | Do not show actionSets that already contains the main_action. | |
ActionSet:: |
public | function |
Execute the action. Overrides BusinessRulesActionPlugin:: |
|
ActionSet:: |
public | function | Provide the form fields for add Business Rule's Items. | |
ActionSet:: |
private | function | Generate the item weight. | |
ActionSet:: |
public static | function | Return all others actions with the same target Entity and Bundle. | |
ActionSet:: |
public | function |
Return the form array. Overrides BusinessRulesItemPluginBase:: |
|
ActionSet:: |
public | function |
Process the item settings before it's saved. Overrides BusinessRulesItemPluginBase:: |
|
ActionSet:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides BusinessRulesItemPluginBase:: |
|
BusinessRulesItemPluginBase:: |
protected | property | The business rules processor. | |
BusinessRulesItemPluginBase:: |
protected | property | The business rules util. | |
BusinessRulesItemPluginBase:: |
public | function |
Provide a description of the item. Overrides BusinessRulesItemPluginInterface:: |
|
BusinessRulesItemPluginBase:: |
public | function |
Get the redirect url for the item edit-form route. Overrides BusinessRulesItemPluginInterface:: |
|
BusinessRulesItemPluginBase:: |
public | function |
Provide the group of the item. Overrides BusinessRulesItemPluginInterface:: |
|
BusinessRulesItemPluginBase:: |
public | function |
Get the redirect url for the item collection route. Overrides BusinessRulesItemPluginInterface:: |
|
BusinessRulesItemPluginBase:: |
public | function |
Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginInterface:: |
9 |
BusinessRulesItemPluginBase:: |
public | function |
Extract the variables from the plugin settings. Overrides BusinessRulesItemPluginInterface:: |
|
BusinessRulesItemPluginBase:: |
private | function | Helper function to process tokens if the setting is an array. | |
BusinessRulesItemPluginBase:: |
public | function |
Process the tokens on the settings property for the item. Overrides BusinessRulesItemPluginInterface:: |
|
BusinessRulesItemPluginBase:: |
public | function |
Process the item replacing the variables by it's values. Overrides BusinessRulesItemPluginInterface:: |
1 |
BusinessRulesItemPluginBase:: |
public | function |
Plugin form validator. Overrides BusinessRulesItemPluginInterface:: |
11 |
BusinessRulesItemPluginInterface:: |
constant | |||
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |