You are here

public static function TaxonomyManagerTree::processTree in Taxonomy Manager 2.0.x

Same name and namespace in other branches
  1. 8 src/Element/TaxonomyManagerTree.php \Drupal\taxonomy_manager\Element\TaxonomyManagerTree::processTree()

File

src/Element/TaxonomyManagerTree.php, line 35

Class

TaxonomyManagerTree
Taxonomy Manager Tree Form Element.

Namespace

Drupal\taxonomy_manager\Element

Code

public static function processTree(&$element, FormStateInterface $form_state, &$complete_form) {
  $element['#tree'] = TRUE;
  if (!empty($element['#vocabulary'])) {
    $taxonomy_vocabulary = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_vocabulary')
      ->load($element['#vocabulary']);
    $pager_size = isset($element['#pager_size']) ? $element['#pager_size'] : -1;
    $terms = TaxonomyManagerTree::loadTerms($taxonomy_vocabulary, 0, $pager_size);
    $list = TaxonomyManagerTree::getNestedListJsonArray($terms);

    // Expand tree to given terms.
    if (isset($element['#terms_to_expand'])) {
      $terms_to_expand = is_array($element['#terms_to_expand']) ? $element['#terms_to_expand'] : [
        $element['#terms_to_expand'],
      ];
      foreach ($terms_to_expand as $term_to_expand) {
        TaxonomyManagerTree:
        self::getFirstPath($term_to_expand, $list);
      }
    }
    $element['#attached']['library'][] = 'taxonomy_manager/tree';
    $element['#attached']['drupalSettings']['taxonomy_manager']['tree'][] = [
      'id' => $element['#id'],
      'name' => $element['#name'],
      'source' => $list,
    ];
    $element['tree'] = [];
    $element['tree']['#prefix'] = '<div id="' . $element['#id'] . '">';
    $element['tree']['#suffix'] = '</div>';
  }
  return $element;
}