BookNavigationBlock.php 6.31 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\book\Plugin\Block\BookNavigationBlock.
6 7
 */

8
namespace Drupal\book\Plugin\Block;
9

10
use Drupal\Core\Block\BlockBase;
11
use Drupal\book\BookManagerInterface;
12
use Drupal\Core\Form\FormStateInterface;
13 14
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Drupal\Core\Entity\EntityStorageInterface;
17 18 19 20

/**
 * Provides a 'Book navigation' block.
 *
21
 * @Block(
22
 *   id = "book_navigation",
23 24
 *   admin_label = @Translation("Book navigation"),
 *   category = @Translation("Menus")
25 26
 * )
 */
27 28 29 30 31
class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The request object.
   *
32
   * @var \Symfony\Component\HttpFoundation\RequestStack
33
   */
34
  protected $requestStack;
35

36 37 38 39 40 41 42
  /**
   * The book manager.
   *
   * @var \Drupal\book\BookManagerInterface
   */
  protected $bookManager;

43 44 45 46 47 48 49
  /**
   * The node storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $nodeStorage;

50 51 52 53 54 55 56
  /**
   * Constructs a new BookNavigationBlock instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
57
   * @param mixed $plugin_definition
58
   *   The plugin implementation definition.
59 60
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack object.
61 62
   * @param \Drupal\book\BookManagerInterface $book_manager
   *   The book manager.
63 64
   * @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
   *   The node storage.
65
   */
66
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack, BookManagerInterface $book_manager, EntityStorageInterface $node_storage) {
67 68
    parent::__construct($configuration, $plugin_id, $plugin_definition);

69
    $this->requestStack = $request_stack;
70
    $this->bookManager = $book_manager;
71
    $this->nodeStorage = $node_storage;
72 73 74 75 76
  }

  /**
   * {@inheritdoc}
   */
77
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
78 79 80 81
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
82
      $container->get('request_stack'),
83 84
      $container->get('book.manager'),
      $container->get('entity.manager')->getStorage('node')
85 86
    );
  }
87 88

  /**
89
   * {@inheritdoc}
90
   */
91
  public function defaultConfiguration() {
92 93 94 95 96 97
    return array(
      'block_mode' => "all pages",
    );
  }

  /**
98
   * {@inheritdoc}
99
   */
100
  function blockForm($form, FormStateInterface $form_state) {
101
    $options = array(
102 103
      'all pages' => $this->t('Show block on all pages'),
      'book pages' => $this->t('Show block only on book pages'),
104 105 106
    );
    $form['book_block_mode'] = array(
      '#type' => 'radios',
107
      '#title' => $this->t('Book navigation block display'),
108 109
      '#options' => $options,
      '#default_value' => $this->configuration['block_mode'],
110
      '#description' => $this->t("If <em>Show block on all pages</em> is selected, the block will contain the automatically generated menus for all of the site's books. If <em>Show block only on book pages</em> is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The <em>Page specific visibility settings</em> or other visibility settings can be used in addition to selectively display this block."),
111 112 113 114 115 116
      );

    return $form;
  }

  /**
117
   * {@inheritdoc}
118
   */
119
  public function blockSubmit($form, FormStateInterface $form_state) {
120
    $this->configuration['block_mode'] = $form_state->getValue('book_block_mode');
121 122 123
  }

  /**
124
   * {@inheritdoc}
125
   */
126
  public function build() {
127
    $current_bid = 0;
128

129
    if ($node = $this->requestStack->getCurrentRequest()->get('node')) {
130 131 132 133 134
      $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
    }
    if ($this->configuration['block_mode'] == 'all pages') {
      $book_menus = array();
      $pseudo_tree = array(0 => array('below' => FALSE));
135
      foreach ($this->bookManager->getAllBooks() as $book_id => $book) {
136 137 138
        if ($book['bid'] == $current_bid) {
          // If the current page is a node associated with a book, the menu
          // needs to be retrieved.
139 140
          $data = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
          $book_menus[$book_id] = $this->bookManager->bookTreeOutput($data);
141 142 143 144 145 146
        }
        else {
          // Since we know we will only display a link to the top node, there
          // is no reason to run an additional menu tree query for each book.
          $book['in_active_trail'] = FALSE;
          // Check whether user can access the book link.
147
          $book_node = $this->nodeStorage->load($book['nid']);
148
          $book['access'] = $book_node->access('view');
149
          $pseudo_tree[0]['link'] = $book;
150
          $book_menus[$book_id] = $this->bookManager->bookTreeOutput($pseudo_tree);
151
        }
152 153 154
        $book_menus[$book_id] += array(
          '#book_title' => $book['title'],
        );
155 156 157 158
      }
      if ($book_menus) {
        return array(
          '#theme' => 'book_all_books_block',
159
        ) + $book_menus;
160 161 162 163
      }
    }
    elseif ($current_bid) {
      // Only display this block when the user is browsing a book.
164 165 166
      $query = \Drupal::entityQuery('node');
      $nid = $query->condition('nid', $node->book['bid'], '=')->execute();

167
      // Only show the block if the user has view access for the top-level node.
168
      if ($nid) {
169
        $tree = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
170 171
        // There should only be one element at the top level.
        $data = array_shift($tree);
172
        $below = $this->bookManager->bookTreeOutput($data['below']);
173
        if (!empty($below)) {
174
          return $below;
175 176 177 178 179 180
        }
      }
    }
    return array();
  }

181 182 183
  /**
   * {@inheritdoc}
   */
184
  public function getCacheContexts() {
185 186 187 188
    // The "Book navigation" block must be cached per role and book navigation
    // context.
    return [
      'user.roles',
189
      'route.book_navigation',
190
    ];
191 192
  }

193 194 195 196 197 198 199 200 201
  /**
   * {@inheritdoc}
   *
   * @todo Make cacheable as part of https://drupal.org/node/1805054
   */
  public function getCacheMaxAge() {
    return 0;
  }

202
}