WebformPermissions.php in Webform 6.x
File
src/Element/WebformPermissions.php
View source
<?php
namespace Drupal\webform\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Select;
use Drupal\webform\Utility\WebformElementHelper;
class WebformPermissions extends Select {
public function getInfo() {
$info = parent::getInfo();
$class = get_class($this);
$info['#element_validate'] = [
[
$class,
'validateWebformPermissions',
],
];
return $info;
}
public static function processSelect(&$element, FormStateInterface $form_state, &$complete_form) {
$permission_handler = \Drupal::service('user.permissions');
$module_handler = \Drupal::service('module_handler');
$options = [];
$permissions = $permission_handler
->getPermissions();
foreach ($permissions as $perm => $perm_item) {
$provider = $perm_item['provider'];
$display_name = $module_handler
->getName($provider);
$options[$display_name][$perm] = strip_tags($perm_item['title']);
}
$element['#options'] = $options;
$element['#select2'] = TRUE;
$element['#type'] = 'select';
WebformElementHelper::process($element);
return parent::processSelect($element, $form_state, $complete_form);
}
public static function validateWebformPermissions(&$element, FormStateInterface $form_state, &$complete_form) {
if (!empty($element['#multiple'])) {
$value = array_values($form_state
->getValue($element['#parents'], []));
$element['#value'] = $value;
$form_state
->setValueForElement($element, $value);
}
}
}