You are here

class UserVariable in Business Rules 8

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

A variable representing one user account.

@package Drupal\business_rules\Plugin\BusinessRulesVariable

Plugin annotation


@BusinessRulesVariable(
  id = "user_variable",
  label = @Translation("User variable"),
  group = @Translation("User"),
  description = @Translation("Variable representing one user account."),
  isContextDependent = FALSE,
  hasTargetEntity = TRUE,
  hasTargetBundle = TRUE,
)

Hierarchy

Expanded class hierarchy of UserVariable

File

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

Namespace

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

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['current_or_defined'] = [
      '#type' => 'select',
      '#title' => t('Current user or defined user?'),
      '#description' => t('Current user or load user by user id.'),
      '#required' => TRUE,
      '#options' => [
        'current' => t('Current'),
        'defined' => t('Defined'),
      ],
      '#default_value' => $item
        ->getSettings('current_or_defined'),
    ];
    $settings['user_id'] = [
      '#type' => 'textfield',
      '#title' => t('User id. You may use a variable to set this value.'),
      '#description' => t('The numeric value for the user id.'),
      '#default_value' => $item
        ->getSettings('user_id'),
      '#states' => [
        'visible' => [
          'select[name="current_or_defined"]' => [
            'value' => 'defined',
          ],
        ],
      ],
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array &$form, FormStateInterface $form_state) {
    $form['settings']['context']['target_entity_type']['#value'] = 'user';
    $form['settings']['context']['target_entity_type']['#options'] = [
      'user' => $form['settings']['context']['target_entity_type']['#options']['user'],
    ];
    $form['settings']['context']['target_entity_type']['#disabled'] = TRUE;
    $form['settings']['context']['target_bundle']['#options'] = [
      'user' => t('User'),
    ];
    $form['settings']['context']['target_bundle']['#value'] = 'user';
    $form['settings']['context']['target_bundle']['#disabled'] = TRUE;
  }

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

    // Show a link to a modal window which all fields from the Entity Variable.
    $content = $this->util
      ->getVariableFieldsModalInfo($variable);
    $keyvalue = $this->util
      ->getKeyValueExpirable('user_variable');
    $keyvalue
      ->set('variableFields.' . $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' => 'user_variable',
      'key' => 'variableFields.' . $variable
        ->id(),
    ], [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
    ])
      ->toString();
    $row['description']['data']['#markup'] .= '<br>' . $details_link;
  }

  /**
   * {@inheritdoc}
   */
  public function processSettings(array $settings, ItemInterface $item) {
    if ($settings['current_or_defined'] == 'current') {
      unset($settings['user_id']);
    }
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate(Variable $variable, BusinessRulesEvent $event) {
    $user = NULL;
    if ($variable
      ->getSettings('current_or_defined') == 'current') {

      // Get the current user.
      $account = $this->util->container
        ->get('current_user');
      $user = User::load($account
        ->id());
    }
    elseif ($variable
      ->getSettings('current_or_defined') == 'defined') {

      // Load user by id.
      $user_id = $variable
        ->getSettings('user_id');
      $user_id = $this
        ->processVariables($user_id, $event
        ->getArgument('variables'));
      $user = User::load($user_id);

      // Add log error if user id not found.
      if (empty($user)) {
        $this->util->logger
          ->error('User id: $id not found. Variable: %variable', [
          '%id' => $user_id,
          '%variable' => $variable
            ->id(),
        ]);
      }
    }
    $variableSet = new VariablesSet();

    // Prepare the user fields to be used as variables.
    if ($user instanceof User) {
      $variableObject = new VariableObject($variable
        ->id(), $user, $variable
        ->getType());
      $variableSet
        ->append($variableObject);
      $fields = $this->util->entityFieldManager
        ->getFieldDefinitions($variable
        ->getTargetEntityType(), $variable
        ->getTargetBundle());
      foreach ($fields as $field_name => $field_storage) {
        $variableObject = new VariableObject($variable
          ->id() . '->' . $field_name, $user
          ->get($field_name)->value, $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::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.
UserVariable::buildForm public function Form constructor. Overrides BusinessRulesItemPluginBase::buildForm
UserVariable::changeDetails public function Change the variable details box. Overrides BusinessRulesVariablePlugin::changeDetails
UserVariable::evaluate public function Evaluate the variable. Overrides BusinessRulesVariablePlugin::evaluate
UserVariable::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
UserVariable::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginBase::processSettings