You are here

function _sitemap_books in Sitemap 8

Helper callback for the sitemap books.

Return value

array Returns a keyed array with variables for rendering the books sitemap box.

1 call to _sitemap_books()
template_preprocess_sitemap in ./sitemap.theme.inc
Preprocesses the variables for sitemap.html.twig.

File

./sitemap.module, line 98
Provides sitemap functionality.

Code

function _sitemap_books() {
  $config = \Drupal::config('sitemap.settings');
  $sitemap_box = [];
  $content = '';
  $options = [];
  $book_titles = [];
  $bids = array_filter($config
    ->get('show_books'));
  if (\Drupal::service('module_handler')
    ->moduleExists('book') && !empty($bids)) {
    $books_expanded = $config
      ->get('books_expanded');
    $title = t('Books');
    $description = '<div class="description">' . t('Books at %sn.', [
      '%sn' => \Drupal::config('system.site')
        ->get('name'),
    ]) . '</div>';
    foreach (\Drupal::service('book.manager')
      ->getAllBooks() as $book) {
      if (in_array($book['bid'], $bids)) {
        if ($books_expanded) {

          // Retrieve the expanded tree.
          // @TODO Ensure the outline is properly translated.
          // @see https://www.drupal.org/node/2615642
          $tree = \Drupal::service('book.manager')
            ->bookTreeAllData($book['bid']);
          $tree_output = \Drupal::service('book.manager')
            ->bookTreeOutput($tree);
          $content .= \Drupal::service('renderer')
            ->render($tree_output);
        }
        else {
          $book_titles[] = Link::fromTextAndUrl($book['title'], Url::fromRoute('entity.node.canonical', [
            'node' => $book['bid'],
          ]))
            ->toString();
        }
      }
    }
    \Drupal::service('sitemap.helper')
      ->setOption($options, 'show_titles', 1, 'show_titles', TRUE);
    if (!$books_expanded && !empty($book_titles)) {
      $book_titles = [
        '#theme' => 'item_list',
        '#items' => $book_titles,
      ];
      $content .= \Drupal::service('renderer')
        ->render($book_titles);
    }
    if (!empty($content)) {
      $attributes = new Attribute();
      $attributes
        ->addClass('sitemap-box-book');
      $sitemap_box = [
        'title' => $title,
        'content' => [
          '#markup' => $description . $content,
        ],
        'attributes' => $attributes,
        'options' => $options,
      ];
    }
  }
  return $sitemap_box;
}