You are here

class EntityFieldVariable in Business Rules 2.x

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

Class EntityValue.

@package Drupal\business_rules\Plugin\BusinessRulesVarthoughtt

Plugin annotation


@BusinessRulesVariable(
  id = "entity_filed_variable",
  label = @Translation("Value from Entity field"),
  group = @Translation("Entity"),
  description = @Translation("Set an variable value with a value from entity field."),
  reactsOnIds = {},
  isContextDependent = TRUE,
  hasTargetEntity = TRUE,
  hasTargetBundle = TRUE,
  hasTargetField = TRUE,
)

Hierarchy

Expanded class hierarchy of EntityFieldVariable

File

src/Plugin/BusinessRulesVariable/EntityFieldVariable.php, line 30

Namespace

Drupal\business_rules\Plugin\BusinessRulesVariable
View source
class EntityFieldVariable extends BusinessRulesVariablePlugin {
  const CURRENT_DATA = 'current_data';
  const ORIGINAL_DATA = 'original_data';

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['data'] = [
      '#type' => 'select',
      '#title' => t('Data'),
      '#required' => TRUE,
      '#options' => [
        '' => t('- Select -'),
        self::CURRENT_DATA => t('Current value'),
        self::ORIGINAL_DATA => t('Original value'),
      ],
      '#description' => t('Current value is the value that is being saved.') . '<br>' . t('Original value is the previous saved value.'),
      '#default_value' => empty($item
        ->getSettings('data')) ? '' : $item
        ->getSettings('data'),
    ];
    return $settings;
  }

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

    // Show a link to a help modal window.
    $content = '<ul>';
    $content .= '<li>' . t('To access a particular multi-value field such as target id, you can use <code>{{@variable_id[delta]}}</code> where "delta" is the delta value to get a one value or <code>{{@variable_id}}</code> to get an array of values;', [
      '@variable_id' => $variable
        ->id(),
    ]) . '</li>';
    $content .= '<li>' . t('To access a particular multi-value field label you can use <code>{{@variable_id[delta]->label}}</code> where "delta" is the delta value to get one label or <code>{{@variable_id->label}}</code> to get an array of labels;', [
      '@variable_id' => $variable
        ->id(),
    ]) . '</li>';
    $content .= '<li>' . t('If you are looping through this field and this field is NOT an entity reference, you can access the current value using <code>{{@variable_id->current}}</code>;', [
      '@variable_id' => $variable
        ->id(),
    ]) . '</li>';
    $content .= '<li>' . t('If you are looping through this field and this field IS an entity reference, you can use <code>{{@variable_id->current->id}}</code> to get the entity reference target id or <code>{{@variable_id->current->label}}</code> to get the entity reference label.', [
      '@variable_id' => $variable
        ->id(),
    ]) . '</li>';
    $content .= '</ul>';
    $keyvalue = $this->util
      ->getKeyValueExpirable('entity_filed_variable');
    $keyvalue
      ->set($variable
      ->id(), [
      '#markup' => $content,
    ]);
    $details_link = Link::createFromRoute(t('Click here to see how to use this variable.'), 'business_rules.ajax.modal', [
      'method' => 'nojs',
      'title' => t('Entity Field Variable Help'),
      'collection' => 'entity_filed_variable',
      'key' => $variable
        ->id(),
    ], [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
    ])
      ->toString();
    $row['description']['data']['#markup'] .= '<br>' . $details_link;
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate(Variable $variable, BusinessRulesEvent $event) {
    $field_name = $variable
      ->getSettings('field');
    $data = $variable
      ->getSettings('data');
    $variableSet = new VariablesSet();
    switch ($data) {
      case self::CURRENT_DATA:
        $entity = $event
          ->getArgument('entity');
        break;
      case self::ORIGINAL_DATA:
        $entity = $event
          ->getArgument('entity_unchanged');
        break;
    }
    try {
      $value = $entity
        ->get($field_name)
        ->getValue();

      // Check if value is a entity reference.

      /** @var \Drupal\field\Entity\FieldConfig $field_definition */
      $field_definition = $entity
        ->getFieldDefinition($field_name);
      if ($field_definition
        ->getType() == 'entity_reference') {
        $entity_references = $entity
          ->get($field_name)
          ->referencedEntities();
        foreach ($entity_references as $key => $item) {
          $value[$key]['entity_reference_label'] = $item
            ->label();
        }
      }
    } catch (\Exception $e) {
      throw $e;
    }
    $arr_label = [];
    if (count($value) === 1) {
      if (isset($value[0]['value'])) {
        $value = $value[0]['value'];
      }
      elseif (isset($value[0]['target_id'])) {
        $value = $value[0]['target_id'];
      }
      else {
        $value = NULL;
      }
    }
    else {
      $arr_value = [];
      foreach ($value as $key => $item) {
        if (isset($item['value'])) {
          $arr_value[] = $item['value'];
          $multi_val = new VariableObject($variable
            ->id() . "[{$key}]", $item['value'], $variable
            ->getType());
        }
        elseif (isset($item['target_id'])) {
          $arr_value[] = $item['target_id'];
          $multi_val = new VariableObject($variable
            ->id() . "[{$key}]", $item['target_id'], $variable
            ->getType());
          $title = new VariableObject($variable
            ->id() . "[{$key}]->label", $item['entity_reference_label'], $variable
            ->getType());
          $variableSet
            ->append($title);
          $arr_label[] = $item['entity_reference_label'];
        }
        else {
          $arr_value[] = NULL;
          $multi_val = new VariableObject($variable
            ->id() . "[{$key}]", NULL, $variable
            ->getType());
        }
        $variableSet
          ->append($multi_val);
      }
      $value = $arr_value;
    }
    $variableObject = new VariableObject($variable
      ->id(), $value, $variable
      ->getType());
    $variableSet
      ->append($variableObject);
    if (count($arr_label)) {
      $variableObject = new VariableObject($variable
        ->id() . '->label', $arr_label, $variable
        ->getType());
      $variableSet
        ->append($variableObject);
    }
    return $variableSet;
  }

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

}

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
EntityFieldVariable::buildForm public function Form constructor. Overrides BusinessRulesItemPluginBase::buildForm
EntityFieldVariable::changeDetails public function Change the variable details box. Overrides BusinessRulesVariablePlugin::changeDetails
EntityFieldVariable::CURRENT_DATA constant
EntityFieldVariable::evaluate public function Evaluate the variable. Overrides BusinessRulesVariablePlugin::evaluate
EntityFieldVariable::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
EntityFieldVariable::ORIGINAL_DATA 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.