You are here

class UrlVariable in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesVariable/UrlVariable.php \Drupal\business_rules\Plugin\BusinessRulesVariable\UrlVariable

A variable representing the current url.

@package Drupal\business_rules\Plugin\BusinessRulesVariable

Plugin annotation


@BusinessRulesVariable(
  id = "url_variable",
  label = @Translation("Current Url"),
  group = @Translation("System"),
  description = @Translation("Variable the current url. Each part of the url can be used as {{variable_id->n}}, where n = the part number; starting from 0. ex. /admin/workflow/business_rules - 1 -> admin; 2 -> workflow; 3 -> business_rules"),
  isContextDependent = FALSE,
  hasTargetEntity = FALSE,
  hasTargetBundle = FALSE,
)

Hierarchy

Expanded class hierarchy of UrlVariable

File

src/Plugin/BusinessRulesVariable/UrlVariable.php, line 29

Namespace

Drupal\business_rules\Plugin\BusinessRulesVariable
View source
class UrlVariable extends BusinessRulesVariablePlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['help'] = [
      '#type' => 'markup',
      '#markup' => t('You only need one url variable in your site and this variable is created during the module installation. There is no necessity to create another one.'),
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function changeDetails(Variable $variable, array &$row) {

    // Show a link to a modal window which variable description.
    $header = [
      'variable' => t('Variable'),
      'field' => t('Field'),
      'type' => t('Type'),
    ];
    $url = $_SERVER['REQUEST_URI'];
    $fields = explode('/', $url);
    unset($fields[0]);
    $rows = [];
    $rows[] = [
      'variable' => [
        'data' => [
          '#markup' => '{{' . $variable
            ->id() . '}}',
        ],
      ],
      'field' => [
        'data' => [
          '#markup' => $url,
        ],
      ],
      'type' => [
        'data' => [
          '#markup' => t('String'),
        ],
      ],
    ];
    foreach ($fields as $key => $value) {
      $rows[] = [
        'variable' => [
          'data' => [
            '#markup' => '{{' . $variable
              ->id() . '->' . $key . '}}',
          ],
        ],
        'field' => [
          'data' => [
            '#markup' => $value,
          ],
        ],
        'type' => [
          'data' => [
            '#markup' => t('String'),
          ],
        ],
      ];
    }
    $content['description'] = [
      '#type' => 'markup',
      '#markup' => t('As an example, the current Url would return the following values:'),
    ];
    $content['variable_fields'] = [
      '#type' => 'table',
      '#rows' => $rows,
      '#header' => $header,
      '#sticky' => TRUE,
    ];
    $keyvalue = $this->util
      ->getKeyValueExpirable('url_variable');
    $keyvalue
      ->set('url_variable.' . $variable
      ->id(), $content);
    $details_link = Link::createFromRoute(t('Click here to see the entity fields'), 'business_rules.ajax.modal', [
      'method' => 'nojs',
      'title' => t('Entity fields'),
      'collection' => 'url_variable',
      'key' => 'url_variable.' . $variable
        ->id(),
    ], [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
    ])
      ->toString();
    $row['description']['data']['#markup'] .= '<br>' . $details_link;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array &$form, FormStateInterface $form_state) {
    unset($form['variables']);
    unset($form['tokens']);
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate(Variable $variable, BusinessRulesEvent $event) {
    $url = $_SERVER['REQUEST_URI'];
    $variableObject = new VariableObject($variable
      ->id(), $url, $variable
      ->getType());
    $variableSet = new VariablesSet();
    $variableSet
      ->append($variableObject);
    $parts = explode('/', $url);
    foreach ($parts as $key => $part) {
      $variableObject = new VariableObject($variable
        ->id() . '->' . $key, $part, $variable
        ->getType());
      $variableSet
        ->append($variableObject);
    }
    return $variableSet;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
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 2
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.
UrlVariable::buildForm public function Form constructor. Overrides BusinessRulesItemPluginBase::buildForm
UrlVariable::changeDetails public function Change the variable details box. Overrides BusinessRulesVariablePlugin::changeDetails
UrlVariable::evaluate public function Evaluate the variable. Overrides BusinessRulesVariablePlugin::evaluate
UrlVariable::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm