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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\fasttoggle\Plugin\SettingObject\AbstractSettingObject implements SettingObjectInterface
- class \Drupal\fasttoggle\Plugin\SettingObject\user implements SettingObjectInterface
- class \Drupal\fasttoggle\Plugin\SettingGroup\UserRoles implements SettingGroupInterface
- class \Drupal\fasttoggle\Plugin\Setting\UserRole implements SettingInterface uses SettingTrait
- class \Drupal\fasttoggle\Plugin\SettingGroup\UserRoles implements SettingGroupInterface
- class \Drupal\fasttoggle\Plugin\SettingObject\user implements SettingObjectInterface
- class \Drupal\fasttoggle\Plugin\SettingObject\AbstractSettingObject implements SettingObjectInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of UserRole
File
- src/
Plugin/ Setting/ UserRole.php, line 49 - Fasttoggle User Status
Namespace
Drupal\fasttoggle\Plugin\SettingView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AbstractSettingObject:: |
protected | property | The object being managed. | |
AbstractSettingObject:: |
public | function |
Get the object instance. Overrides SettingObjectInterface:: |
|
AbstractSettingObject:: |
public | function |
Object subtype machine name. Overrides SettingObjectInterface:: |
1 |
AbstractSettingObject:: |
public | function |
Set an instance of the object. Overrides SettingObjectInterface:: |
1 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SettingTrait:: |
public | function | Keys for config settings. | |
SettingTrait:: |
public | function | Get the markup we modify. | |
SettingTrait:: |
public | function | Get a plain text list of human readable labels for the setting, in the order used. | |
SettingTrait:: |
public static | function | Get an array of sitewide setting form elements for this object type. | |
SettingTrait:: |
public | function | Get a list of actual values for the setting, in the order used. | 1 |
SettingTrait:: |
function | Retrieve the FieldItem for a setting. | ||
SettingTrait:: |
public | function | Get labels (fallback function if a template is used (eg roles) | |
SettingTrait:: |
function | Retrieve the current value of the setting. | 1 | |
SettingTrait:: |
public | function | Write access check. | |
SettingTrait:: |
public | function | Move to the next setting value. | |
SettingTrait:: |
public | function | Move to the previous setting value and save it. | |
SettingTrait:: |
public | function | Return a render array for an instance of this setting. | |
SettingTrait:: |
function | Set a custom field name (eg fields on nodes). | ||
SettingTrait:: |
function | Modify the setting. | 1 | |
SettingTrait:: |
public | function | Allow access to the trait constructor if the setting also implements one. | |
SettingTrait:: |
public | function | Retrieve the object type that can be modified by this setting. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
user:: |
public | function |
Object ID. Overrides AbstractSettingObject:: |
|
user:: |
public | function |
User name. Overrides AbstractSettingObject:: |
|
user:: |
public | function |
Access. Overrides AbstractSettingObject:: |
|
user:: |
public | function |
Matches an object? Overrides SettingObjectInterface:: |
|
user:: |
public | function |
Save function. Overrides AbstractSettingObject:: |
|
UserRole:: |
private | property | ||
UserRole:: |
function | Return the description for the attribute being displayed. | ||
UserRole:: |
protected | function | Retrieve the list of roles that can be modified by this setting. | |
UserRole:: |
public static | function |
Return whether this setting matches the provided field definition. Overrides SettingInterface:: |
|
UserRole:: |
public | function |
Access control function. Overrides SettingTrait:: |
|
UserRole:: |
public | function |
Return the sitewide form element for this setting. Overrides SettingTrait:: |
|
UserRole:: |
public | function |
Constructs a Drupal\Component\Plugin\PluginBase object. Overrides SettingTrait:: |
|
UserRoles:: |
public static | function |
Return whether this setting matches the provided field definition. Overrides SettingGroupInterface:: |
|
UserRoles:: |
public | function |
Access. Overrides SettingGroupInterface:: |