RolesConditionCreator.php in Block Visibility Groups 8
File
block_visibility_groups_admin/src/Plugin/ConditionCreator/RolesConditionCreator.php
View source
<?php
namespace Drupal\block_visibility_groups_admin\Plugin\ConditionCreator;
use Drupal\block_visibility_groups_admin\Plugin\ConditionCreatorBase;
class RolesConditionCreator extends ConditionCreatorBase {
public function getNewConditionLabel() {
return $this
->t('Roles');
}
public function createConditionElements() {
$elements['condition_config'] = [
'#tree' => TRUE,
];
$elements['condition_config']['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('When the user has the following roles'),
'#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', user_role_names()),
];
return $elements;
}
public function itemSelected($condition_info) {
$roles = $condition_info['condition_config']['roles'];
return !empty(array_filter($roles));
}
public function createConditionConfig($plugin_info) {
$config = parent::createConditionConfig($plugin_info);
$config['roles'] = array_filter($config['roles']);
$config['context_mapping'] = [
'user' => '@user.current_user_context:current_user',
];
return $config;
}
}