You are here

function _site_map_books in Site map 8

Same name and namespace in other branches
  1. 5 site_map.module \_site_map_books()
  2. 6.2 site_map.module \_site_map_books()
  3. 6 site_map.module \_site_map_books()
  4. 7 site_map.module \_site_map_books()

Render the latest maps for books.

Return value

string Returns HTML string of site map for books.

1 call to _site_map_books()
template_preprocess_site_map in ./site_map.theme.inc
Preprocesses the variables for site-map.html.twig.

File

./site_map.module, line 314
Provides a site map functionality.

Code

function _site_map_books() {
  $config = \Drupal::config('site_map.settings');
  $output = '';
  $options = array();
  $book_titles = array();
  $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.', array(
      '%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.
          $tree = \Drupal::service('book.manager')
            ->bookTreeAllData($book['bid']);
          if (\Drupal::service('module_handler')
            ->moduleExists('i18n_menu')) {
            $tree = i18n_menu_localize_tree($tree, $GLOBALS['language']->language);
          }
          $tree_output = \Drupal::service('book.manager')
            ->bookTreeOutput($tree);
          $output .= drupal_render($tree_output);
        }
        else {
          $book_titles[] = \Drupal::l($book['title'], Url::fromRoute('entity.node.canonical', array(
            'node' => $book['bid'],
          )));
        }
      }
    }
    \Drupal::service('site_map.helper')
      ->setOption($options, 'show_titles', 1, 'show_titles', TRUE);
    if (!$books_expanded && !empty($book_titles)) {
      $book_titles = array(
        '#theme' => 'item_list',
        '#items' => $book_titles,
      );
      $output .= drupal_render($book_titles);
    }
    if (!empty($output)) {
      $attributes = array(
        'class' => array(
          'site-map-box-book',
        ),
      );
      $site_map_box = array(
        '#theme' => 'site_map_box',
        '#title' => $title,
        '#content' => array(
          '#markup' => $description . $output,
        ),
        '#attributes' => $attributes,
        '#options' => $options,
      );
      $output = $site_map_box;
    }
  }
  return $output;
}