ForumController::buildActionLinks

protected ForumController::buildActionLinks($vid, TermInterface $forum_term = NULL)

Generates an action link to display at the top of the forum listing.

Parameters

string $vid: Vocabulary ID.

\Drupal\taxonomy\TermInterface $forum_term: The term for which the links are to be built.

Return value

array Render array containing the links.

File

core/modules/forum/src/Controller/ForumController.php, line 288

Class

ForumController
Controller routines for forum routes.

Namespace

Drupal\forum\Controller

Code

protected function buildActionLinks($vid, TermInterface $forum_term = NULL) {
  $user = $this->currentUser();

  $links = [];
  // Loop through all bundles for forum taxonomy vocabulary field.
  foreach ($this->fieldMap['node']['taxonomy_forums']['bundles'] as $type) {
    if ($this->nodeAccess->createAccess($type)) {
      $node_type = $this->nodeTypeStorage->load($type);
      $links[$type] = [
        '#attributes' => ['class' => ['action-links']],
        '#theme' => 'menu_local_action',
        '#link' => [
          'title' => $this->t('Add new @node_type', [
            '@node_type' => $this->nodeTypeStorage->load($type)->label(),
          ]),
          'url' => Url::fromRoute('node.add', ['node_type' => $type]),
        ],
        '#cache' => [
          'tags' => $node_type->getCacheTags(),
        ],
      ];
      if ($forum_term && $forum_term->bundle() == $vid) {
        // We are viewing a forum term (specific forum), append the tid to
        // the url.
        $links[$type]['#link']['localized_options']['query']['forum_id'] = $forum_term->id();
      }
    }
  }
  if (empty($links)) {
    // Authenticated user does not have access to create new topics.
    if ($user->isAuthenticated()) {
      $links['disallowed'] = [
        '#markup' => $this->t('You are not allowed to post new content in the forum.'),
      ];
    }
    // Anonymous user does not have access to create new topics.
    else {
      $links['login'] = [
        '#attributes' => ['class' => ['action-links']],
        '#theme' => 'menu_local_action',
        '#link' => array(
          'title' => $this->t('Log in to post new content in the forum.'),
          'url' => Url::fromRoute('user.login', [], ['query' => $this->getDestinationArray()]),
        ),
      ];
    }
  }
  return $links;
}
doc_Drupal
2016-10-29 09:17:02
Comments
Leave a Comment

Please login to continue.