You are here

class UserRole in Fasttoggle 8.2

Abstract interface for settings. Plugin strings are used for quick filtering without the need to instantiate the class.

No attributes member in the annotation - calculated value

Plugin annotation


@Plugin(
  id = "user_roles",
  entityType = "user",
  name = "role",
  description = "Roles that may be granted to this user",
  group = "user_roles",
  weight = 100,
  default = false,
  base_formatter = "Drupal\fasttoggle\Plugin\Field\FieldFormatter\BooleanFormatter",
  labels = {
    FASTTOGGLE_LABEL_ACTION = {
      0 = "Grant '%s'",
      1 = "Revoke '%s'",
    },
    FASTTOGGLE_LABEL_STATUS = {
      0 = "Granted '%s'",
      1 = "'%s' not granted",
    },
  },
  description_template = "Toggle '@rolename' role",
  attributeWeight = 0,
)

Hierarchy

Expanded class hierarchy of UserRole

File

src/Plugin/Setting/UserRole.php, line 49
Fasttoggle User Status

Namespace

Drupal\fasttoggle\Plugin\Setting
View source
class UserRole extends UserRoles implements SettingInterface {
  use SettingTrait;

  /**
   * @var attributes - Array of attribute name => description pairs.
   */
  private $attributes;

  /**
   * Constructs a Drupal\Component\Plugin\PluginBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    $this
      ->trait_constructor($configuration, $plugin_id, $plugin_definition);
    $role_objects = Role::loadMultiple();
    unset($role_objects['anonymous']);
    unset($role_objects['authenticated']);
    $this->attributes = array_combine(array_keys($role_objects), array_map(function ($a) {
      return $a
        ->label();
    }, $role_objects));
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * Retrieve the list of roles that can be modified by this setting.
   *
   * @return array
   *   An array containing strings used to identify the attributes to get/set
   *   and the settings form.
   */
  protected function get_attributes() {
    return $this->attributes;
  }

  /**
   * Return the description for the attribute being displayed.
   *
   * @return TranslatableMarkup?
   *   The attribute description.
   */
  function attributeDescription($attributeName) {

    // Strip off "user_" from the front.
    $attribute = $this->attributes[substr($attributeName, 11)];
    $definition = $this
      ->getPluginDefinition();
    return t($definition['description_template'], [
      '@rolename' => $attribute,
    ]);
  }

  /**
   * Access control function.
   *
   * @param $user
   *   The user against which to check (un)publish permission.
   *
   * @return boolean
   *   Whether the user is allowed to (un)publish the user.
   */
  public function mayEditSetting() {
    $user = \Drupal::currentUser();
    return AccessResult::allowedIfHasPermission($user, "override user blocked option");
  }

  /**
   * Return the sitewide form element for this setting.
   *
   * @return array
   *   Form element for this setting.
   */
  public function settingForm($config, $attribute) {
    $sitewide_access = $config
      ->get($attribute);
    if (is_null($sitewide_access)) {
      $sitewide_access = $this->default;
    }
    $fieldArray = [
      '#type' => 'checkbox',
      '#default_value' => $sitewide_access,
      '#title' => $this
        ->attributeDescription($attribute),
      '#weight' => $this->attributeWeight,
    ];
    return $fieldArray;
  }

  /**
   * Return whether this setting matches the provided field definition.
   *
   * @param $definition
   *   The field definition for which a match is being sought.
   *
   * @return boolean
   *   Whether this plugin handles the definition.
   */
  public static function matches($definition) {
    $has_get = is_callable(array(
      $definition,
      'get',
    ));
    $entity = $has_get ? $definition
      ->get('entity_type') : $definition
      ->getProvider();
    return $entity == 'user' && $definition
      ->getName() == 'role';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractSettingObject::$object protected property The object being managed.
AbstractSettingObject::get_object public function Get the object instance. Overrides SettingObjectInterface::get_object
AbstractSettingObject::get_type public function Object subtype machine name. Overrides SettingObjectInterface::get_type 1
AbstractSettingObject::setObject public function Set an instance of the object. Overrides SettingObjectInterface::setObject 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
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.
SettingTrait::configSettingKeys public function Keys for config settings.
SettingTrait::formatter public function Get the markup we modify.
SettingTrait::getHumanReadableValueList public function Get a plain text list of human readable labels for the setting, in the order used.
SettingTrait::getSitewideSettingFormElements public static function Get an array of sitewide setting form elements for this object type.
SettingTrait::getValueList public function Get a list of actual values for the setting, in the order used. 1
SettingTrait::get_field function Retrieve the FieldItem for a setting.
SettingTrait::get_labels public function Get labels (fallback function if a template is used (eg roles)
SettingTrait::get_value function Retrieve the current value of the setting. 1
SettingTrait::mayEdit public function Write access check.
SettingTrait::nextValue public function Move to the next setting value.
SettingTrait::previousValue public function Move to the previous setting value and save it.
SettingTrait::render_array public function Return a render array for an instance of this setting.
SettingTrait::setField function Set a custom field name (eg fields on nodes).
SettingTrait::set_value function Modify the setting. 1
SettingTrait::trait_constructor public function Allow access to the trait constructor if the setting also implements one.
SettingTrait::__get public function Retrieve the object type that can be modified by this setting.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
user::get_id public function Object ID. Overrides AbstractSettingObject::get_id
user::get_title public function User name. Overrides AbstractSettingObject::get_title
user::mayEditEntity public function Access. Overrides AbstractSettingObject::mayEditEntity
user::objectMatches public function Matches an object? Overrides SettingObjectInterface::objectMatches
user::save public function Save function. Overrides AbstractSettingObject::save
UserRole::$attributes private property
UserRole::attributeDescription function Return the description for the attribute being displayed.
UserRole::get_attributes protected function Retrieve the list of roles that can be modified by this setting.
UserRole::matches public static function Return whether this setting matches the provided field definition. Overrides SettingInterface::matches
UserRole::mayEditSetting public function Access control function. Overrides SettingTrait::mayEditSetting
UserRole::settingForm public function Return the sitewide form element for this setting. Overrides SettingTrait::settingForm
UserRole::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides SettingTrait::__construct
UserRoles::groupMatches public static function Return whether this setting matches the provided field definition. Overrides SettingGroupInterface::groupMatches
UserRoles::mayEditGroup public function Access. Overrides SettingGroupInterface::mayEditGroup