You are here

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

Same name and namespace in other branches
  1. 8 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
      ->isRebuilding() && $form_state
      ->getValue($element['#parents'])) {
      $ancestors = TermReferenceFancytree::getSelectedAncestors($form_state
        ->getValue($element['#parents']), 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,
      'select_all' => $element['#select_all'],
      'select_children' => $element['#select_children'],
    ];
    if ($element['#select_all']) {
      $element['#markup'] = '<a href="#" class="selectAll">Select all</a>';
    }

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