You are here

public static function TermReferenceFancytree::processTree in Term Reference Fancytree 8

Same name and namespace in other branches
  1. 8.2 src/Element/TermReferenceFancytree.php \Drupal\term_reference_fancytree\Element\TermReferenceFancytree::processTree()

File

src/Element/TermReferenceFancytree.php, line 36

Class

TermReferenceFancytree
Term Reference Tree Form Element.

Namespace

Drupal\term_reference_fancytree\Element

Code

public static function processTree(&$element, FormStateInterface $form_state, &$complete_form) {
  if (!empty($element['#vocabulary'])) {

    // Get the ancestors of the selected items.
    // If we are processing input (submit) we want to pass the state with selected items.
    if ($form_state
      ->isProcessingInput() && $form_state
      ->getUserInput()[$element['#field_name']]) {
      $ancestors = TermReferenceFancytree::getSelectedAncestors($form_state
        ->getUserInput()[$element['#field_name']], TRUE);
    }
    else {
      $ancestors = TermReferenceFancytree::getSelectedAncestors($element['#default_value'], FALSE);
    }

    // Build a list of top level nodes, including children if containing
    // selected items.
    $list = TermReferenceFancytree::getTopLevelNodes($element, $ancestors, $form_state);

    // Attach our libary and settings.
    $element['#attached']['library'][] = 'term_reference_fancytree/tree';
    $element['#attached']['drupalSettings']['term_reference_fancytree'][$element['#id']]['tree'][] = [
      'id' => $element['#id'],
      'name' => $element['#name'],
      'source' => $list,
    ];

    // Create HTML wrappers.
    $element['tree'] = [];
    $element['tree']['#prefix'] = '<div id="' . $element['#id'] . '">';
    $element['tree']['#suffix'] = '</div>';
  }
  return $element;
}