function ForumController::buildActionLinks

Same name in other branches
  1. 9 core/modules/forum/src/Controller/ForumController.php \Drupal\forum\Controller\ForumController::buildActionLinks()
  2. 8.9.x core/modules/forum/src/Controller/ForumController.php \Drupal\forum\Controller\ForumController::buildActionLinks()
  3. 11.x core/modules/forum/src/Controller/ForumController.php \Drupal\forum\Controller\ForumController::buildActionLinks()

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.

1 call to ForumController::buildActionLinks()
ForumController::build in core/modules/forum/src/Controller/ForumController.php
Returns a renderable forum index page array.

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] = [
                '#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.'),
            ];
        }
        else {
            $links['login'] = [
                '#theme' => 'menu_local_action',
                '#link' => [
                    'title' => $this->t('Log in to post new content in the forum.'),
                    'url' => Url::fromRoute('user.login', [], [
                        'query' => $this->getDestinationArray(),
                    ]),
                ],
                // Without this workaround, the action links will be rendered as <li>
                // with no wrapping <ul> element.
                // @todo Find a better way for this in https://www.drupal.org/node/3181052.
'#prefix' => '<ul class="action-links">',
                '#suffix' => '</ul>',
            ];
        }
    }
    else {
        // Without this workaround, the action links will be rendered as <li> with
        // no wrapping <ul> element.
        // @todo Find a better way for this in https://www.drupal.org/node/3181052.
        $links['#prefix'] = '<ul class="action-links">';
        $links['#suffix'] = '</ul>';
    }
    return $links;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.