You are here

protected function ForumForm::forumParentSelect in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/forum/src/Form/ForumForm.php \Drupal\forum\Form\ForumForm::forumParentSelect()

Returns a select box for available parent terms.

Parameters

int $tid: ID of the term that is being added or edited.

string $title: Title for the select box.

Return value

array A select form element.

1 call to ForumForm::forumParentSelect()
ForumForm::form in core/modules/forum/src/Form/ForumForm.php
Gets the actual form array to be built.

File

core/modules/forum/src/Form/ForumForm.php, line 128

Class

ForumForm
Base form for forum term edit forms.

Namespace

Drupal\forum\Form

Code

protected function forumParentSelect($tid, $title) {
  $taxonomy_storage = $this->entityTypeManager
    ->getStorage('taxonomy_term');
  $parents = $taxonomy_storage
    ->loadParents($tid);
  if ($parents) {
    $parent = array_shift($parents);
    $parent = $parent
      ->id();
  }
  else {
    $parent = 0;
  }
  $vid = $this
    ->config('forum.settings')
    ->get('vocabulary');
  $children = $taxonomy_storage
    ->loadTree($vid, $tid, NULL, TRUE);

  // A term can't be the child of itself, nor of its children.
  foreach ($children as $child) {
    $exclude[] = $child->tid;
  }
  $exclude[] = $tid;
  $tree = $taxonomy_storage
    ->loadTree($vid, 0, NULL, TRUE);
  $options[0] = '<' . $this
    ->t('root') . '>';
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term
        ->id(), $exclude)) {
        $options[$term
          ->id()] = str_repeat(' -- ', $term->depth) . $term
          ->getName();
      }
    }
  }
  $description = $this
    ->t('Forums may be placed at the top (root) level, or inside another container or forum.');
  return [
    '#type' => 'select',
    '#title' => $title,
    '#default_value' => $parent,
    '#options' => $options,
    '#description' => $description,
    '#required' => TRUE,
  ];
}