You are here

function book_node_links_alter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/book/book.module \book_node_links_alter()
  2. 10 core/modules/book/book.module \book_node_links_alter()

Implements hook_node_links_alter().

File

core/modules/book/book.module, line 92
Allows users to create and organize related content in an outline.

Code

function book_node_links_alter(array &$links, NodeInterface $node, array &$context) {
  if ($context['view_mode'] != 'rss') {
    $account = \Drupal::currentUser();
    if (isset($node->book['depth'])) {
      if ($context['view_mode'] == 'full' && node_is_page($node)) {
        $child_type = \Drupal::config('book.settings')
          ->get('child_type');
        $access_control_handler = \Drupal::entityTypeManager()
          ->getAccessControlHandler('node');
        if (($account
          ->hasPermission('add content to books') || $account
          ->hasPermission('administer book outlines')) && $access_control_handler
          ->createAccess($child_type) && $node->book['depth'] < BookManager::BOOK_MAX_DEPTH) {
          $book_links['book_add_child'] = [
            'title' => t('Add child page'),
            'url' => Url::fromRoute('node.add', [
              'node_type' => $child_type,
            ], [
              'query' => [
                'parent' => $node
                  ->id(),
              ],
            ]),
          ];
        }
        if ($account
          ->hasPermission('access printer-friendly version')) {
          $book_links['book_printer'] = [
            'title' => t('Printer-friendly version'),
            'url' => Url::fromRoute('book.export', [
              'type' => 'html',
              'node' => $node
                ->id(),
            ]),
            'attributes' => [
              'title' => t('Show a printer-friendly version of this book page and its sub-pages.'),
            ],
          ];
        }
      }
    }
    if (!empty($book_links)) {
      $links['book'] = [
        '#theme' => 'links__node__book',
        '#links' => $book_links,
        '#attributes' => [
          'class' => [
            'links',
            'inline',
          ],
        ],
      ];
    }
  }
}