YamlFormRoles.php in YAML Form 8
File
src/Element/YamlFormRoles.php
View source
<?php
namespace Drupal\yamlform\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Checkboxes;
class YamlFormRoles extends Checkboxes {
public function getInfo() {
$info = parent::getInfo();
$class = get_class($this);
$info['#element_validate'] = [
[
$class,
'validateYamlFormRoles',
],
];
$info['#include_anonymous'] = TRUE;
return $info;
}
public static function processCheckboxes(&$element, FormStateInterface $form_state, &$complete_form) {
$element['#options'] = array_map('\\Drupal\\Component\\Utility\\Html::escape', user_role_names());
if (empty($element['#include_anonymous'])) {
unset($element['#options']['anonymous']);
}
$element['#attached']['library'][] = 'yamlform/yamlform.element.roles';
$element['#attributes']['class'][] = 'js-yamlform-roles-role';
return parent::processCheckboxes($element, $form_state, $complete_form);
}
public static function validateYamlFormRoles(array &$element, FormStateInterface $form_state, array &$complete_form) {
$value = $form_state
->getValue($element['#parents'], []);
$form_state
->setValueForElement($element, array_values(array_filter($value)));
}
}