You are here

class GroupHasUserCondition in Business Rules 2.x

Same name and namespace in other branches
  1. 8 modules/br_group/src/Plugin/BusinessRulesCondition/GroupHasUserCondition.php \Drupal\br_group\Plugin\BusinessRulesCondition\GroupHasUserCondition

Class GroupHasUserCondition.

@package Drupal\business_rules\Plugin\BusinessRulesCondition

Plugin annotation


@BusinessRulesCondition(
  id = "group_has_user",
  label = @Translation("Check if an user is in a group"),
  group = @Translation("Group"),
  description = @Translation("Check if an user is member of a group."),
  isContextDependent = FALSE,
  reactsOnIds = {},
  hasTargetEntity = FALSE,
  hasTargetBundle = FALSE,
  hasTargetField = FALSE,
)

Hierarchy

Expanded class hierarchy of GroupHasUserCondition

File

modules/br_group/src/Plugin/BusinessRulesCondition/GroupHasUserCondition.php, line 31

Namespace

Drupal\br_group\Plugin\BusinessRulesCondition
View source
class GroupHasUserCondition extends BusinessRulesConditionPlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
    $settings['group_id'] = [
      '#type' => 'textfield',
      '#title' => t('Group Id'),
      '#required' => TRUE,
      '#description' => t('The group id. You may use variable or token to fill this information'),
      '#default_value' => $item
        ->getSettings('group_id'),
    ];
    $settings['user_key'] = [
      '#type' => 'radios',
      '#title' => t('Key to select the user'),
      '#default_value' => $item
        ->getSettings('user_key') ?: 'username',
      '#options' => [
        'username' => t('User Name'),
        'userid' => t('User Id'),
      ],
    ];
    $settings['user_name'] = [
      '#type' => 'textfield',
      '#title' => t('User Name'),
      '#description' => t('The user name. You may use variable or token to fill this information'),
      '#default_value' => $item
        ->getSettings('user_name'),
      '#states' => [
        'visible' => [
          ':input[name="user_key"]' => [
            'value' => 'username',
          ],
        ],
      ],
    ];
    $settings['user_id'] = [
      '#type' => 'textfield',
      '#title' => t('User Id'),
      '#description' => t('The user id. You may use variable or token to fill this information'),
      '#default_value' => $item
        ->getSettings('user_id'),
      '#states' => [
        'visible' => [
          ':input[name="user_key"]' => [
            'value' => 'userid',
          ],
        ],
      ],
    ];
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('user_key') == 'username' && $form_state
      ->getValue('user_name') == '') {
      $form_state
        ->setErrorByName('user_name', t('User name is required.'));
    }
    elseif ($form_state
      ->getValue('user_key') == 'userid' && $form_state
      ->getValue('user_id') == '') {
      $form_state
        ->setErrorByName('user_id', t('User id is required.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function processSettings(array $settings, ItemInterface $item) {
    if ($settings['user_key'] == 'username') {
      $settings['user_id'] = NULL;
    }
    elseif ($settings['user_key'] == 'userid') {
      $settings['user_name'] = NULL;
    }
    return parent::processSettings($settings, $item);
  }

  /**
   * {@inheritdoc}
   */
  public function process(ConditionInterface $condition, BusinessRulesEvent $event) {
    $variables = $event
      ->getArgument('variables');
    $group_id = $condition
      ->getSettings('group_id');
    $group_id = $this
      ->processVariables($group_id, $variables);
    $user_key = $condition
      ->getSettings('user_key');
    $user_name = $condition
      ->getSettings('user_name');
    $user_name = $this
      ->processVariables($user_name, $variables);
    $user_id = $condition
      ->getSettings('user_id');
    $user_id = $this
      ->processVariables($user_id, $variables);
    if ($user_key == 'userid') {
      $user = User::load($user_id);
    }
    else {
      $id = $this->util->container
        ->get('entity_type.manager')
        ->getStorage('user')
        ->getQuery()
        ->condition('name', $user_name)
        ->execute();
      $user = User::load(array_values($id)[0]);
    }
    $group = Group::load($group_id);
    $member = $group
      ->getMember($user);
    if ($member instanceof GroupMembership) {
      $result = TRUE;
    }
    else {
      $result = FALSE;
    }
    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::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::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::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 11
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
GroupHasUserCondition::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
GroupHasUserCondition::process public function Process the condition. Overrides BusinessRulesConditionPlugin::process
GroupHasUserCondition::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginBase::processSettings
GroupHasUserCondition::validateForm public function Plugin form validator. Overrides BusinessRulesItemPluginBase::validateForm
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.