You are here

abstract class BusinessRulesItemPluginBase in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesItemPluginBase.php \Drupal\business_rules\Plugin\BusinessRulesItemPluginBase

Base Class for Business rules plugins.

@package Drupal\business_rules\Plugin

Hierarchy

Expanded class hierarchy of BusinessRulesItemPluginBase

File

src/Plugin/BusinessRulesItemPluginBase.php, line 18

Namespace

Drupal\business_rules\Plugin
View source
abstract class BusinessRulesItemPluginBase extends PluginBase implements BusinessRulesItemPluginInterface {

  /**
   * The business rules processor.
   *
   * @var \Drupal\business_rules\Util\BusinessRulesProcessor
   */
  protected $processor;

  /**
   * The business rules util.
   *
   * @var \Drupal\business_rules\Util\BusinessRulesUtil
   */
  protected $util;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->processor = \Drupal::service('business_rules.processor');
    $this->util = \Drupal::service('business_rules.util');
  }

  /**
   * {@inheritdoc}
   */
  public function getGroup() {
    return $this->pluginDefinition['group'];
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->pluginDefinition['description'];
  }

  /**
   * {@inheritdoc}
   */
  public abstract function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item);

  /**
   * {@inheritdoc}
   */
  public function buildForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function processSettings(array $settings, ItemInterface $item) {
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function getRedirectUrl(ItemInterface $item) {
    return $item
      ->urlInfo('collection');
  }

  /**
   * {@inheritdoc}
   */
  public function getEditUrl(ItemInterface $item) {
    return $item
      ->urlInfo('edit-form');
  }

  /**
   * {@inheritdoc}
   */
  public function pregMatch($string) {
    if (is_string($string)) {
      preg_match_all(self::VARIABLE_REGEX, $string, $variables);
      return $variables[1];
    }
    else {
      throw new \Exception('Only strings are acceptable to pregMatch variables');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getVariables(ItemInterface $item) {
    $variableSet = new VariablesSet();
    $settings = $item
      ->getSettings();
    foreach ($settings as $setting) {
      if (is_string($setting)) {
        $variables = $this
          ->pregMatch($setting);
        if (count($variables)) {
          foreach ($variables as $variable_id) {
            if (stristr($variable_id, '->') && !stristr($variable_id, '[')) {
              $arr_temp = explode('->', $variable_id);
              $variable_id = $arr_temp[0];
              $variable = Variable::load($variable_id);
              unset($arr_temp[0]);
            }
            elseif (stristr($variable_id, '[')) {
              $arr_temp = explode('[', $variable_id);
              $variable_id = $arr_temp[0];
              $variable = Variable::load($variable_id);
              unset($arr_temp[0]);
            }
            else {
              $variable = Variable::load($variable_id);
            }
            if (!empty($variable)) {
              $varObject = new VariableObject($variable_id, NULL, $variable
                ->getType());
              $variableSet
                ->append($varObject);
            }
          }
        }
      }
    }
    return $variableSet;
  }

  /**
   * {@inheritdoc}
   */
  public function processVariables($content, VariablesSet $event_variables) {

    /** @var \Drupal\business_rules\VariableObject $variable */
    if ($event_variables
      ->count()) {
      foreach ($event_variables
        ->getVariables() as $variable) {
        if (is_string($variable
          ->getValue()) || is_numeric($variable
          ->getValue())) {
          $content = str_replace('{{' . $variable
            ->getId() . '}}', $variable
            ->getValue(), $content);
        }
        elseif (is_array($variable
          ->getValue())) {
          if (preg_match_all(self::VARIABLE_REGEX, $content)) {
            if ($content == '{{' . $variable
              ->getId() . '}}') {
              $content = $variable
                ->getValue();
            }
            elseif (stristr($content, '{{' . $variable
              ->getId() . '}}')) {
              $value = implode(chr(10), $variable
                ->getValue());
              $content = str_replace('{{' . $variable
                ->getId() . '}}', $value, $content);
            }
          }
        }
        elseif (empty($variable
          ->getValue())) {
          $content = str_replace('{{' . $variable
            ->getId() . '}}', 'NULL', $content);
        }
      }
    }
    return $content;
  }

  /**
   * {@inheritdoc}
   */
  public function processTokens(ItemInterface &$item, BusinessRulesEvent $event) {
    $settings = $item
      ->getSettings();

    // Get the context entity to pass to token processor.
    try {
      $entity_type = $event
        ->getArgument('entity_type_id');
      $entity = $event
        ->getArgument('entity');
      $context = [
        $entity_type => $entity,
      ];
    } catch (\InvalidArgumentException $e) {
      $context = [];
    }
    foreach ($settings as $key => $setting) {
      if (is_string($setting)) {
        $variables = $event
          ->getArgument('variables');
        $setting = $this
          ->processVariables($setting, $variables);
        $settings[$key] = $this->util->token
          ->replace($setting, $context, [
          'clear' => TRUE,
        ]);
      }
      elseif (is_array($setting)) {
        $this
          ->processTokenArraySetting($settings[$key], $context, $event);
      }
    }
    foreach ($settings as $key => $setting) {
      $item
        ->setSetting($key, $setting);
    }
  }

  /**
   * Helper function to process tokens if the setting is an array.
   *
   * @param array $setting
   *   The setting array.
   * @param array $context
   *   The context to replace the tokens.
   * @param \Drupal\business_rules\Events\BusinessRulesEvent $event
   *   The Business Rules event.
   */
  private function processTokenArraySetting(array &$setting, array $context, BusinessRulesEvent $event) {
    if (count($setting)) {
      foreach ($setting as $key => $value) {
        if (is_string($value)) {
          $variables = $event
            ->getArgument('variables');
          $value = $this
            ->processVariables($setting[$key], $variables);
          $setting[$key] = $this->util->token
            ->replace($value, $context, [
            'clear' => TRUE,
          ]);
        }
        elseif (is_array($value)) {
          $this
            ->processTokenArraySetting($setting[$key], $context, $event);
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

}

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::getSettingsForm abstract public function Return the form array. Overrides BusinessRulesItemPluginInterface::getSettingsForm 43
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.