You are here

class UserVariableHasRole in Business Rules 8

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

Class UserVariableHasRole.

@package Drupal\business_rules\Plugin\BusinessRulesCondition

Plugin annotation


@BusinessRulesCondition(
  id = "user_variable_has_role",
  label = @Translation("User variable has role"),
  group = @Translation("Variable"),
  description = @Translation("Check if variable user has role."),
)

Hierarchy

Expanded class hierarchy of UserVariableHasRole

1 file declares its use of UserVariableHasRole
ConfigEntityCalculateDependencyTest.php in tests/src/Unit/ConfigEntityCalculateDependencyTest.php

File

src/Plugin/BusinessRulesCondition/UserVariableHasRole.php, line 27

Namespace

Drupal\business_rules\Plugin\BusinessRulesCondition
View source
class UserVariableHasRole extends BusinessRulesConditionPlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['user_variable'] = [
      '#type' => 'select',
      '#required' => TRUE,
      '#title' => t('User variable'),
      '#description' => t('Select the variable that represents the user.'),
      '#options' => $this->util
        ->getVariablesOptions([
        'user_variable',
        'entity_empty_variable',
      ], [
        'user',
      ], [
        'user',
      ]),
      '#default_value' => $item
        ->getSettings('user_variable'),
    ];
    $settings['roles'] = [
      '#type' => 'checkboxes',
      '#title' => t('Roles'),
      '#required' => TRUE,
      '#options' => $this->util
        ->getUserRolesOptions(),
      '#default_value' => is_array($item
        ->getSettings('roles')) ? $item
        ->getSettings('roles') : [],
    ];
    $settings['criteria'] = [
      '#type' => 'select',
      '#title' => t('Match criteria'),
      '#description' => t('The condition will check if current user has all selected roles or at least one role?'),
      '#required' => TRUE,
      '#default_value' => $item
        ->getSettings('criteria'),
      '#options' => [
        'all' => t('All roles'),
        'one' => t('At least one role'),
      ],
    ];
    return $settings;
  }

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

  /**
   * {@inheritdoc}
   */
  public function processSettings(array $settings, ItemInterface $item) {
    foreach ($settings['roles'] as $key => $role) {
      if ($role === 0) {
        unset($settings['roles'][$key]);
      }
    }
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function getVariables(ItemInterface $item) {
    $variable_id = $item
      ->getSettings('user_variable');
    $variable = Variable::load($variable_id);
    $variableSet = new VariablesSet();
    $variableObject = new VariableObject($variable_id, NULL, $variable
      ->getType());
    $variableSet
      ->append($variableObject);
    return $variableSet;
  }

  /**
   * {@inheritdoc}
   */
  public function process(ConditionInterface $condition, BusinessRulesEvent $event) {
    $roles = $condition
      ->getSettings('roles');
    $criteria = $condition
      ->getSettings('criteria');
    $user_variable_id = $condition
      ->getSettings('user_variable');

    /** @var \Drupal\business_rules\VariablesSet $event_variables */
    $event_variables = $event
      ->getArgument('variables');

    /** @var \Drupal\user\Entity\User $user_variable */
    $user_variable = $event_variables
      ->getVariable($user_variable_id)
      ->getValue();

    // Check if $user_variable is defined.
    if (!$user_variable instanceof User) {
      $this->util->logger
        ->error('Trying to check user roles in a null user object. Condition: %condition', [
        '%condition' => $condition
          ->label() . ' [' . $condition
          ->id() . ']',
      ]);
      return FALSE;
    }
    $user_roles = array_values($user_variable
      ->getRoles());
    $result = FALSE;
    if ($criteria == 'all') {
      $result = count(array_intersect($roles, $user_roles)) == count($roles);
    }
    elseif ($criteria == 'one') {
      $result = !empty(array_intersect($user_roles, $roles));
    }
    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::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::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.
UserVariableHasRole::buildForm public function Form constructor. Overrides BusinessRulesItemPluginBase::buildForm
UserVariableHasRole::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
UserVariableHasRole::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginBase::getVariables
UserVariableHasRole::process public function Process the condition. Overrides BusinessRulesConditionPlugin::process
UserVariableHasRole::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginBase::processSettings