class HideOnRole in Field Formatter Condition 8
The plugin for check empty fields.
Plugin annotation
@FieldFormatterCondition(
id = "hide_on_role",
label = @Translation("Hide when current user has role"),
dsFields = TRUE,
types = {
"all"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\fico\Plugin\FieldFormatterConditionBase implements FieldFormatterConditionInterface
- class \Drupal\fico\Plugin\Field\FieldFormatter\Condition\HideOnRole
- class \Drupal\fico\Plugin\FieldFormatterConditionBase implements FieldFormatterConditionInterface
Expanded class hierarchy of HideOnRole
File
- src/
Plugin/ Field/ FieldFormatter/ Condition/ HideOnRole.php, line 19
Namespace
Drupal\fico\Plugin\Field\FieldFormatter\ConditionView source
class HideOnRole extends FieldFormatterConditionBase {
/**
* {@inheritdoc}
*/
public function alterForm(&$form, $settings) {
$user_roles = [];
foreach (Role::loadMultiple() as $role) {
$user_roles[$role
->id()] = $role
->label();
}
$default_include = isset($settings['settings']['include_admin']) ? $settings['settings']['include_admin'] : NULL;
$default_roles = isset($settings['settings']['roles']) ? $settings['settings']['roles'] : NULL;
$form['include_admin'] = array(
'#type' => 'checkbox',
'#title' => t('Include the administrator'),
'#default_value' => $default_include,
);
$form['roles'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Select roles'),
'#options' => $user_roles,
'#default_value' => $default_roles,
);
}
/**
* {@inheritdoc}
*/
public function access(&$build, $field, $settings) {
if (array_intersect(\Drupal::currentUser()
->getRoles(), $settings['settings']['roles']) && (\Drupal::currentUser()
->id() != 1 || $settings['settings']['include_admin'] == 1)) {
$build[$field]['#access'] = FALSE;
}
}
/**
* {@inheritdoc}
*/
public function summary($settings) {
$roles = [];
foreach (Role::loadMultiple() as $role) {
if (in_array($role
->id(), $settings['settings']['roles'])) {
$roles[] = $role
->label();
}
}
return t("Condition: %condition (%settings)", [
"%condition" => t('Hide when current user has role'),
'%settings' => implode(', ', $roles),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FieldFormatterConditionBase:: |
public | function |
Creates a pre-configured instance of a plugin. Overrides FactoryInterface:: |
|
FieldFormatterConditionBase:: |
public | function |
Gets a specific plugin definition. Overrides DiscoveryInterface:: |
|
FieldFormatterConditionBase:: |
public | function |
Gets the definition of all plugins for this type. Overrides DiscoveryInterface:: |
|
FieldFormatterConditionBase:: |
protected | function | Check for entity in build. | |
FieldFormatterConditionBase:: |
protected | function | Load fields from a entity. | |
FieldFormatterConditionBase:: |
protected | function | Check for entity_type in build. | |
FieldFormatterConditionBase:: |
public | function |
Gets a preconfigured instance of a plugin. Overrides MapperInterface:: |
|
FieldFormatterConditionBase:: |
public | function |
Indicates if a specific plugin definition exists. Overrides DiscoveryInterface:: |
|
HideOnRole:: |
public | function |
Access control function. Overrides FieldFormatterConditionBase:: |
|
HideOnRole:: |
public | function |
Alter the condition form. Overrides FieldFormatterConditionBase:: |
|
HideOnRole:: |
public | function |
Return the summary string. Overrides FieldFormatterConditionBase:: |
|
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. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |