Skip to content
Snippets Groups Projects
Commit 2b0d53bb authored by Simon Bäse's avatar Simon Bäse
Browse files

Issue #3400698 by artemboiko, simonbaese, beder, MegaKeegMan: Start level and active branch of tree

parent 4ea1bdf3
No related branches found
No related tags found
1 merge request!7Issue #3400698: Add start level configuration
Pipeline #63216 passed
......@@ -61,7 +61,7 @@ class ExpandBookManager extends BookManager {
/**
* {@inheritdoc}
*/
public function bookTreeAllData($bid, $link = NULL, $max_depth = NULL, $always_expand = 0) {
public function bookTreeAllData($bid, $link = NULL, $max_depth = NULL, $start_level = NULL, $always_expand = 0) {
$tree = &drupal_static(__METHOD__, []);
$language_interface = $this->languageManager->getCurrentLanguage();
......@@ -77,7 +77,7 @@ class ExpandBookManager extends BookManager {
if (!isset($tree[$cid])) {
// If the tree data was not in the static cache, build $tree_parameters.
$tree_parameters = [
'min_depth' => 1,
'min_depth' => $start_level ?? 1,
'max_depth' => $max_depth,
];
......@@ -95,6 +95,13 @@ class ExpandBookManager extends BookManager {
$tree_parameters['active_trail'][] = $nid;
}
if ($start_level && $start_level > 1) {
$book_link = $this->loadBookLink($nid);
if (!empty($book_link['p' . $start_level]) && $book_link['p' . $start_level] > 0) {
$tree_parameters['conditions']['p' . $start_level] = $book_link['p' . $start_level];
}
}
// Build the tree using the parameters; the resulting tree will be cached.
$tree[$cid] = $this->bookTreeBuild($bid, $tree_parameters);
}
......
......@@ -26,6 +26,7 @@ class CustomBookNavigationBlock extends BookNavigationBlock {
return [
'block_mode' => 'all pages',
'target_book' => '',
'start_level' => 1,
'max_levels' => '',
'always_expand' => 1,
];
......@@ -57,6 +58,14 @@ class CustomBookNavigationBlock extends BookNavigationBlock {
'#description' => $this->t('If set to zero, all levels will be shown.'),
];
$form['start_level'] = [
'#type' => 'number',
'#title' => $this->t('Start level'),
'#min' => 1,
'#default_value' => $this->configuration['start_level'],
'#description' => $this->t('Start level, filter tree, show only active branch.'),
];
$form['always_expand'] = [
'#type' => 'checkbox',
'#title' => $this->t('Always expand the menu'),
......@@ -75,6 +84,7 @@ class CustomBookNavigationBlock extends BookNavigationBlock {
$this->configuration['target_book'] = $form_state->getValue('target_book');
$this->configuration['max_levels'] = $form_state->getValue('max_levels');
$this->configuration['start_level'] = $form_state->getValue('start_level');
$this->configuration['always_expand'] = $form_state->getValue('always_expand');
}
......@@ -94,6 +104,7 @@ class CustomBookNavigationBlock extends BookNavigationBlock {
}
$max_levels = $this->configuration['max_levels'] ?: NULL;
$start_level = $this->configuration['start_level'] ?: NULL;
$target_book_id = $this->configuration['target_book'];
$always_expand = $this->configuration['always_expand'];
......@@ -121,7 +132,7 @@ class CustomBookNavigationBlock extends BookNavigationBlock {
}
else {
// Retrieve the full menu, to the specified depth.
$data = $book_manager->bookTreeAllData($book_id, $book, $max_levels, $always_expand);
$data = $book_manager->bookTreeAllData($book_id, $book, $max_levels, $start_level, $always_expand);
$book_menus[$book_id] = $book_manager->bookTreeOutput($data);
$book_menus[$book_id]['#items'][$book_id]['in_active_trail'] = FALSE;
if ($book_menus[$book_id]['#items'][$book_id]['original_link']['bid'] == $current_book_id) {
......@@ -157,12 +168,14 @@ class CustomBookNavigationBlock extends BookNavigationBlock {
// Only show the block if the user has view access for the top-level node.
if ($nid) {
$tree = $book_manager->bookTreeAllData($node->book['bid'], $node->book, $max_levels, $always_expand);
$tree = $book_manager->bookTreeAllData($node->book['bid'], $node->book, $max_levels, $start_level, $always_expand);
// There should only be one element at the top level.
$data = array_shift($tree);
$below = $book_manager->bookTreeOutput($data['below']);
if (!empty($below)) {
return $below;
if (!empty($data['below'])) {
$below = $book_manager->bookTreeOutput($data['below']);
if (!empty($below)) {
return $below;
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment