You are here

function theme_term_tree_list in Taxonomy Term Reference Tree Widget 8

Same name and namespace in other branches
  1. 7.2 term_reference_tree.widget.inc \theme_term_tree_list()
  2. 7 term_reference_tree.widget.inc \theme_term_tree_list()

Themes the term tree display (as opposed to the select widget).

1 string reference to 'theme_term_tree_list'
term_reference_tree_theme in ./term_reference_tree.module
Implements hook_theme().
1 theme call to theme_term_tree_list()
TermReferenceTree::viewElements in src/Plugin/Field/FieldFormatter/TermReferenceTree.php
Builds a renderable array for a field value.

File

./term_reference_tree.module, line 404

Code

function theme_term_tree_list($variables) {
  $element =& $variables['element'];
  $data =& $element['#data'];
  $tree = [];

  // For each selected term.
  foreach ($data as $item) {

    // Loop if the term ID is not zero.
    $values = [];
    $tid = $item['target_id'];
    $original_tid = $tid;
    while ($tid != 0) {

      // Unshift the term onto an array.
      array_unshift($values, $tid);

      // Repeat with parent term.
      $tid = _term_reference_tree_get_parent($tid);
    }
    $current =& $tree;

    // For each term in the above array.
    foreach ($values as $tid) {

      // current[children][term_id] = new array.
      if (!isset($current['children'][$tid])) {
        $current['children'][$tid] = [
          'selected' => FALSE,
        ];
      }

      // If this is the last value in the array,
      // tree[children][term_id][selected] = true.
      if ($tid == $original_tid) {
        $current['children'][$tid]['selected'] = TRUE;
      }
      $current['children'][$tid]['tid'] = $tid;
      $current =& $current['children'][$tid];
    }
  }
  $output = '<div class="term-tree-list">';
  $output .= _term_reference_tree_output_list_level($element, $tree);
  $output .= '</div>';
  return $output;
}