WebformTermCheckboxes.php in Webform 6.x
File
src/Element/WebformTermCheckboxes.php
View source
<?php
namespace Drupal\webform\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Checkboxes;
class WebformTermCheckboxes extends Checkboxes {
use WebformTermReferenceTrait;
public function getInfo() {
return [
'#vocabulary' => '',
'#tree_delimiter' => ' ',
'#breadcrumb' => FALSE,
'#breadcrumb_delimiter' => ' › ',
'#scroll' => TRUE,
'#depth' => NULL,
] + parent::getInfo();
}
public static function processCheckboxes(&$element, FormStateInterface $form_state, &$complete_form) {
static::setOptions($element);
$element = parent::processCheckboxes($element, $form_state, $complete_form);
if (!\Drupal::moduleHandler()
->moduleExists('taxonomy')) {
return $element;
}
$entity_repository = \Drupal::service('entity.repository');
$tree = static::loadTree($element['#vocabulary']);
if (empty($element['#breadcrumb'])) {
foreach ($tree as $item) {
$item = $entity_repository
->getTranslationFromContext($item);
$element[$item
->id()]['#title'] = $item
->label();
$element[$item
->id()]['#field_prefix'] = str_repeat($element['#tree_delimiter'], $item->depth);
}
}
$element['#attributes']['class'][] = 'js-webform-term-checkboxes';
$element['#attributes']['class'][] = 'webform-term-checkboxes';
if (!empty($element['#scroll'])) {
$element['#attributes']['class'][] = 'webform-term-checkboxes-scroll';
}
$element['#attached']['library'][] = 'webform/webform.element.term_checkboxes';
return $element;
}
protected static function getOptionsTree(array $element, $language) {
$element += [
'#tree_delimiter' => '-',
];
$entity_repository = \Drupal::service('entity.repository');
$tree = static::loadTree($element['#vocabulary']);
$options = [];
foreach ($tree as $item) {
$item = $entity_repository
->getTranslationFromContext($item);
if (!$item
->access('view')) {
continue;
}
if (!empty($element['#depth']) && $item->depth >= $element['#depth']) {
continue;
}
$options[$item
->id()] = $item
->getName();
}
return $options;
}
}