You are here

class RemoveNodeFromGroupAction in Business Rules 8

Same name and namespace in other branches
  1. 2.x modules/br_group/src/Plugin/BusinessRulesAction/RemoveNodeFromGroupAction.php \Drupal\br_group\Plugin\BusinessRulesAction\RemoveNodeFromGroupAction

Class RemoveNodeFromGroupAction.

@package Drupal\business_rules\Plugin\BusinessRulesAction

Plugin annotation


@BusinessRulesAction(
  id = "remove_node_from_group",
  label = @Translation("Group: Remove Node entity from a group"),
  group = @Translation("Group"),
  description = @Translation("Remove an Node entity from a group on Group module."),
  isContextDependent = FALSE,
  hasTargetEntity = FALSE,
  hasTargetBundle = FALSE,
  hasTargetField = FALSE,
)

Hierarchy

Expanded class hierarchy of RemoveNodeFromGroupAction

File

modules/br_group/src/Plugin/BusinessRulesAction/RemoveNodeFromGroupAction.php, line 29

Namespace

Drupal\br_group\Plugin\BusinessRulesAction
View source
class RemoveNodeFromGroupAction extends BusinessRulesActionPlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['node_id'] = [
      '#type' => 'textfield',
      '#title' => t('Node Id'),
      '#required' => TRUE,
      '#description' => t('The node id to be added to the group. You may use variable or token to fill this information'),
      '#default_value' => $item
        ->getSettings('node_id'),
    ];
    $settings['group_id'] = [
      '#type' => 'textfield',
      '#title' => t('Group Id'),
      '#required' => TRUE,
      '#description' => t('The group id to remove the node. You may use variable or token to fill this information'),
      '#default_value' => $item
        ->getSettings('group_id'),
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function execute(ActionInterface $action, BusinessRulesEvent $event) {
    $variables = $event
      ->getArgument('variables');
    $group_id = $action
      ->getSettings('group_id');
    $group_id = $this
      ->processVariables($group_id, $variables);
    $node_id = $action
      ->getSettings('node_id');
    $node_id = $this
      ->processVariables($node_id, $variables);
    $node = Node::load($node_id);
    $group = Group::load($group_id);
    if ($node instanceof Node) {
      $type = 'group_node:' . $node
        ->getType();
      $current_node = $group
        ->getContent($type, [
        'entity_id' => $node_id,
      ]);
      if (count($current_node)) {

        /** @var \Drupal\group\Entity\GroupContent $content */
        $content = array_values($current_node)[0];
        $content
          ->delete();
        $result = [
          '#type' => 'markup',
          '#markup' => t('Node %node has been removed from group %group.', [
            '%node' => $node
              ->label(),
            '%group' => $group
              ->label(),
          ]),
        ];
      }
      else {
        $result = [
          '#type' => 'markup',
          '#markup' => t('Node %node is not on group %group.', [
            '%node' => $node
              ->label(),
            '%group' => $group
              ->label(),
          ]),
        ];
      }
    }
    else {
      $result = [
        '#type' => 'markup',
        '#markup' => t('Node id %node could not be removed from group %group.', [
          '%node' => $node_id,
          '%group' => $group
            ->label(),
        ]),
      ];
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
BusinessRulesItemPluginBase::buildForm public function Form constructor. Overrides BusinessRulesItemPluginInterface::buildForm 11
BusinessRulesItemPluginBase::getDescription public function Provide a description of the item. Overrides BusinessRulesItemPluginInterface::getDescription
BusinessRulesItemPluginBase::getEditUrl public function Get the redirect url for the item edit-form route. Overrides BusinessRulesItemPluginInterface::getEditUrl
BusinessRulesItemPluginBase::getGroup public function Provide the group of the item. Overrides BusinessRulesItemPluginInterface::getGroup
BusinessRulesItemPluginBase::getRedirectUrl public function Get the redirect url for the item collection route. Overrides BusinessRulesItemPluginInterface::getRedirectUrl
BusinessRulesItemPluginBase::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginInterface::getVariables 9
BusinessRulesItemPluginBase::pregMatch public function Extract the variables from the plugin settings. Overrides BusinessRulesItemPluginInterface::pregMatch
BusinessRulesItemPluginBase::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginInterface::processSettings 19
BusinessRulesItemPluginBase::processTokenArraySetting private function Helper function to process tokens if the setting is an array.
BusinessRulesItemPluginBase::processTokens public function Process the tokens on the settings property for the item. Overrides BusinessRulesItemPluginInterface::processTokens
BusinessRulesItemPluginBase::processVariables public function Process the item replacing the variables by it's values. Overrides BusinessRulesItemPluginInterface::processVariables 1
BusinessRulesItemPluginBase::validateForm public function Plugin form validator. Overrides BusinessRulesItemPluginInterface::validateForm 11
BusinessRulesItemPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 11
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RemoveNodeFromGroupAction::execute public function Execute the action. Overrides BusinessRulesActionPlugin::execute
RemoveNodeFromGroupAction::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm