You are here

trait WebformTermReferenceTrait in Webform 6.x

Same name in this branch
  1. 6.x src/Element/WebformTermReferenceTrait.php \Drupal\webform\Element\WebformTermReferenceTrait
  2. 6.x src/Plugin/WebformElement/WebformTermReferenceTrait.php \Drupal\webform\Plugin\WebformElement\WebformTermReferenceTrait
Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformTermReferenceTrait.php \Drupal\webform\Plugin\WebformElement\WebformTermReferenceTrait

Provides an 'term_reference' trait.

Hierarchy

File

src/Plugin/WebformElement/WebformTermReferenceTrait.php, line 11

Namespace

Drupal\webform\Plugin\WebformElement
View source
trait WebformTermReferenceTrait {
  use WebformEntityReferenceTrait;
  use WebformEntityOptionsTrait;

  /**
   * {@inheritdoc}
   */
  public function getRelatedTypes(array $element) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function preview() {
    if ($vocabularies = Vocabulary::loadMultiple()) {
      $vocabulary = reset($vocabularies);
      $vocabulary_id = $vocabulary
        ->id();
    }
    else {
      $vocabulary_id = 'tags';
    }

    // Make sure the vocabulary does not have more than 250 terms.
    // This will prevent a fatal memory error when
    // previewing term related elements.

    /** @var \Drupal\taxonomy\TermStorageInterface $taxonomy_storage */
    $taxonomy_storage = $this->entityTypeManager
      ->getStorage('taxonomy_term');
    $tree = $taxonomy_storage
      ->loadTree($vocabulary_id);
    if (count($tree) > 250) {
      $vocabulary_id = NULL;
    }
    return parent::preview() + [
      '#vocabulary' => $vocabulary_id,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['term_reference'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Term reference settings'),
      '#weight' => -40,
    ];
    $form['term_reference']['vocabulary'] = [
      '#type' => 'webform_entity_select',
      '#title' => $this
        ->t('Vocabulary'),
      '#target_type' => 'taxonomy_vocabulary',
      '#selection_handler' => 'default:taxonomy_vocabulary',
    ];
    $form['term_reference']['breadcrumb'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display term hierarchy using breadcrumbs'),
      '#return_value' => TRUE,
    ];
    $form['term_reference']['breadcrumb_delimiter'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Breadcrumb delimiter'),
      '#size' => 10,
      '#states' => [
        'visible' => [
          [
            ':input[name="properties[breadcrumb]"]' => [
              'checked' => TRUE,
            ],
          ],
          'or',
          [
            ':input[name="properties[format]"]' => [
              'value' => 'breadcrumb',
            ],
          ],
        ],
        'required' => [
          [
            ':input[name="properties[breadcrumb]"]' => [
              'checked' => TRUE,
            ],
          ],
          'or',
          [
            ':input[name="properties[format]"]' => [
              'value' => 'breadcrumb',
            ],
          ],
        ],
      ],
    ];
    $form['term_reference']['tree_delimiter'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Tree delimiter'),
      '#size' => 10,
      '#states' => [
        'visible' => [
          ':input[name="properties[breadcrumb]"]' => [
            'checked' => FALSE,
          ],
        ],
        'required' => [
          ':input[name="properties[breadcrumb]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['term_reference']['depth'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Limit term depth'),
      '#field_suffix' => $this
        ->t('level(s)'),
    ];
    $form['term_reference']['scroll'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow terms to be scrollable'),
      '#return_value' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Overrides: \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::validateConfigurationForm.
    parent::validateConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetType(array $element) {
    return 'taxonomy_term';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformAjaxElementTrait::buildAjaxElement public function Build an Ajax element.
WebformAjaxElementTrait::buildAjaxElementTrigger public function Build an Ajax element's trigger.
WebformAjaxElementTrait::buildAjaxElementUpdate public function Build an Ajax element's update (submit) button.
WebformAjaxElementTrait::buildAjaxElementWrapper public function Build an Ajax element's wrapper.
WebformAjaxElementTrait::getAjaxElementUpdateClass public function Get Ajax element update class.
WebformAjaxElementTrait::getAjaxElementUpdateName public function Get Ajax element update name.
WebformAjaxElementTrait::getAjaxElementWrapperId public function Get Ajax element wrapper id.
WebformAjaxElementTrait::getAjaxElementWrapperRecursive protected static function Get ajax element wrapper.
WebformAjaxElementTrait::submitAjaxElementCallback public static function Ajax element submit callback. 2
WebformAjaxElementTrait::updateAjaxElementCallback public static function Ajax element update callback.
WebformAjaxElementTrait::validateAjaxElementCallback public static function Ajax element validate callback.
WebformEntityOptionsTrait::defineDefaultProperties protected function 2
WebformEntityOptionsTrait::getElementSelectorInputsOptions protected function 1
WebformEntityOptionsTrait::getElementSelectorSourceValues public function
WebformEntityOptionsTrait::getExportDefaultOptions public function
WebformEntityOptionsTrait::prepare public function
WebformEntityReferenceTrait::$entityRepository protected property The entity repository.
WebformEntityReferenceTrait::$entityTypeRepository protected property The entity type repository.
WebformEntityReferenceTrait::$selectionManager protected property The selection plugin manager service.
WebformEntityReferenceTrait::buildAjaxElementTriggerRecursive protected function Build an ajax elements trigger.
WebformEntityReferenceTrait::buildExportHeader public function
WebformEntityReferenceTrait::buildExportOptionsForm public function
WebformEntityReferenceTrait::buildExportRecord public function
WebformEntityReferenceTrait::create public static function
WebformEntityReferenceTrait::formatHtmlItem protected function
WebformEntityReferenceTrait::formatHtmlItems protected function
WebformEntityReferenceTrait::formatTextItem protected function
WebformEntityReferenceTrait::formatTextItems protected function
WebformEntityReferenceTrait::getItemDefaultFormat public function
WebformEntityReferenceTrait::getItemFormats public function
WebformEntityReferenceTrait::getTargetEntities public function
WebformEntityReferenceTrait::getTargetEntity public function
WebformEntityReferenceTrait::getTestValues public function
WebformEntityReferenceTrait::isMultiline public function
WebformEntityReferenceTrait::setOptions protected function Get element options. 3
WebformEntityReferenceTrait::validateEntityReferenceFormat public static function Form API callback. Remove unchecked options from value array.
WebformTermReferenceTrait::form public function Overrides WebformEntityReferenceTrait::form
WebformTermReferenceTrait::getRelatedTypes public function Overrides WebformEntityReferenceTrait::getRelatedTypes
WebformTermReferenceTrait::getTargetType public function Overrides WebformEntityReferenceTrait::getTargetType
WebformTermReferenceTrait::preview public function Overrides WebformEntityReferenceTrait::preview
WebformTermReferenceTrait::validateConfigurationForm public function Overrides WebformEntityReferenceTrait::validateConfigurationForm