Provides a 'Book navigation' block.
@Plugin(
id = "book_navigation",
admin_label = @Translation("Book navigation"),
module = "book"
)
Expanded class hierarchy of BookNavigationBlock
class BookNavigationBlock extends BlockBase {
/**
* Overrides \Drupal\block\BlockBase::settings().
*/
public function settings() {
return array(
'cache' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE,
'block_mode' => "all pages",
);
}
/**
* Overrides \Drupal\block\BlockBase::blockForm()
*/
function blockForm($form, &$form_state) {
$options = array(
'all pages' => t('Show block on all pages'),
'book pages' => t('Show block only on book pages'),
);
$form['book_block_mode'] = array(
'#type' => 'radios',
'#title' => t('Book navigation block display'),
'#options' => $options,
'#default_value' => $this->configuration['block_mode'],
'#description' => 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."),
);
return $form;
}
/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, &$form_state) {
$this->configuration['block_mode'] = $form_state['values']['book_block_mode'];
}
/**
* {@inheritdoc}
*/
public function build() {
$current_bid = 0;
if ($node = menu_get_object()) {
$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,
),
);
foreach (book_get_books() as $book_id => $book) {
if ($book['bid'] == $current_bid) {
// If the current page is a node associated with a book, the menu
// needs to be retrieved.
$book_menus[$book_id] = menu_tree_output(menu_tree_all_data($node->book['menu_name'], $node->book));
}
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.
$book_node = node_load($book['nid']);
$book['access'] = node_access('view', $book_node);
$pseudo_tree[0]['link'] = $book;
$book_menus[$book_id] = menu_tree_output($pseudo_tree);
}
}
if ($book_menus) {
return array(
'#theme' => 'book_all_books_block',
$book_menus,
);
}
}
elseif ($current_bid) {
// Only display this block when the user is browsing a book.
$select = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('n.nid', $node->book['bid'])
->addTag('node_access');
$nid = $select
->execute()
->fetchField();
// Only show the block if the user has view access for the top-level node.
if ($nid) {
$tree = menu_tree_all_data($node->book['menu_name'], $node->book);
// There should only be one element at the top level.
$data = array_shift($tree);
$below = menu_tree_output($data['below']);
if (!empty($below)) {
$book_title_link = array(
'#theme' => 'book_title_link',
'#link' => $data['link'],
);
return array(
'#title' => drupal_render($book_title_link),
$below,
);
}
}
}
return array();
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockBase:: |
public | function |
Indicates whether block-specific criteria allow access to the block. Overrides BlockPluginInterface:: |
16 |
BlockBase:: |
public | function | Adds block type-specific validation for the block form. | |
BlockBase:: |
public | function |
Implements \Drupal\block\BlockPluginInterface::form(). Overrides BlockPluginInterface:: |
1 |
BlockBase:: |
public | function | Returns the configuration data for the block plugin. | |
BlockBase:: |
public | function | Sets a particular value in the block settings. | |
BlockBase:: |
public | function |
Implements \Drupal\block\BlockPluginInterface::submit(). Overrides BlockPluginInterface:: |
|
BlockBase:: |
public | function |
Implements \Drupal\block\BlockPluginInterface::validate(). Overrides BlockPluginInterface:: |
|
BlockBase:: |
public | function |
Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
BookNavigationBlock:: |
function |
Overrides \Drupal\block\BlockBase::blockForm() Overrides BlockBase:: |
||
BookNavigationBlock:: |
public | function |
Overrides \Drupal\block\BlockBase::blockSubmit(). Overrides BlockBase:: |
|
BookNavigationBlock:: |
public | function |
Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface:: |
|
BookNavigationBlock:: |
public | function |
Overrides \Drupal\block\BlockBase::settings(). Overrides BlockBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
public | function |
Returns the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function |
Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |