ForumForm::forumParentSelect

protected ForumForm::forumParentSelect($tid, $title)

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.

File

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

Class

ForumForm
Base form for forum term edit forms.

Namespace

Drupal\forum\Form

Code

protected function forumParentSelect($tid, $title) {
  $taxonomy_storage = $this->entityManager->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 array(
    '#type' => 'select',
    '#title' => $title,
    '#default_value' => $parent,
    '#options' => $options,
    '#description' => $description,
    '#required' => TRUE,
  );
}
doc_Drupal
2016-10-29 09:17:06
Comments
Leave a Comment

Please login to continue.