You are here

public function WebformTermReferenceTrait::preview in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/WebformTermReferenceTrait.php \Drupal\webform\Plugin\WebformElement\WebformTermReferenceTrait::preview()

Overrides WebformEntityReferenceTrait::preview

File

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

Class

WebformTermReferenceTrait
Provides an 'term_reference' trait.

Namespace

Drupal\webform\Plugin\WebformElement

Code

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 = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  $tree = $taxonomy_storage
    ->loadTree($vocabulary_id);
  if (count($tree) > 250) {
    $vocabulary_id = NULL;
  }
  return parent::preview() + [
    '#vocabulary' => $vocabulary_id,
  ];
}