/** * Generates an action link to display at the top of the forum listing. * * @param string $vid * Vocabulary ID. * @param \Drupal\taxonomy\TermInterface $forum_term * The term for which the links are to be built. * * @return array * Render array containing the links. */ 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)) { $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])]]; 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'] = ['#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; }
/** * Asserts that node create access correctly grants or denies access. * * @param string $bundle * The node bundle to check access to. * @param bool $result * Whether access should be granted or not. * @param \Drupal\Core\Session\AccountInterface $account * The user account for which to check access. * @param string|null $langcode * (optional) The language code indicating which translation of the node * to check. If NULL, the untranslated (fallback) access is checked. */ function assertNodeCreateAccess($bundle, $result, AccountInterface $account, $langcode = NULL) { $this->assertEqual($result, $this->accessHandler->createAccess($bundle, $account, array('langcode' => $langcode)), $this->nodeAccessAssertMessage('create', $result, $langcode)); }