diff --git a/archive_tree.libraries.yml b/archive_tree.libraries.yml index b6823db..58817bf 100644 --- a/archive_tree.libraries.yml +++ b/archive_tree.libraries.yml @@ -3,3 +3,5 @@ archive_tree_styles: css: theme: css/archive_tree.css: {} + js: + js/archive_tree.js: {} diff --git a/js/archive_tree.js b/js/archive_tree.js new file mode 100644 index 0000000..42d1cd9 --- /dev/null +++ b/js/archive_tree.js @@ -0,0 +1,22 @@ +(function () { + // Wait for DOM ready + document.addEventListener('DOMContentLoaded', function () { + document.querySelectorAll('.archive-tree-year').forEach(function (summary) { + var details = summary.parentElement; + var year = summary.querySelector('a')?.textContent?.trim(); + if (!year) return; + var key = 'archive_tree_year_' + year; + // Restore state + var open = localStorage.getItem(key); + if (open === 'true') { + details.setAttribute('open', ''); + } else if (open === 'false') { + details.removeAttribute('open'); + } + // Listen for toggle + details.addEventListener('toggle', function () { + localStorage.setItem(key, details.open ? 'true' : 'false'); + }); + }); + }); +})(); diff --git a/src/Plugin/Block/ArchiveTreeBlock.php b/src/Plugin/Block/ArchiveTreeBlock.php index f57c250..d9b9ae3 100644 --- a/src/Plugin/Block/ArchiveTreeBlock.php +++ b/src/Plugin/Block/ArchiveTreeBlock.php @@ -12,18 +12,42 @@ use Drupal\Core\Block\BlockBase; * admin_label = @Translation("Archive tree"), * ) */ +use Drupal\Core\Block\BlockForm; +use Drupal\Core\Form\FormStateInterface; + class ArchiveTreeBlock extends BlockBase { /** * {@inheritdoc} */ + public function defaultConfiguration() { + return [ + 'expand_years' => FALSE, + ] + parent::defaultConfiguration(); + } + + public function blockForm($form, FormStateInterface $form_state) { + $form = parent::blockForm($form, $form_state); + $form['expand_years'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Expand all years by default'), + '#default_value' => $this->configuration['expand_years'], + '#description' => $this->t('If checked, all years will be expanded by default.'), + ]; + return $form; + } + + public function blockSubmit($form, FormStateInterface $form_state) { + parent::blockSubmit($form, $form_state); + $this->configuration['expand_years'] = $form_state->getValue('expand_years'); + } + public function build() { $storage = \Drupal::entityTypeManager()->getStorage('node'); $query = $storage->getQuery() ->condition('type', 'article') ->condition('status', 1) ->sort('created', 'DESC') - ->accessCheck(TRUE) - ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT'); + ->accessCheck(TRUE); $nids = $query->execute(); $nodes = $storage->loadMultiple($nids); @@ -48,9 +72,11 @@ class ArchiveTreeBlock extends BlockBase { } $output = ''; + $expand = !empty($this->configuration['expand_years']); foreach ($tree as $year => $data) { $year_url = '/archive-tree/' . $year; - $output .= '
'; + $open = $expand ? ' open' : ''; + $output .= ''; $output .= '' . $year . ' (' . $data['count'] . ')'; $output .= ''; foreach ($data['months'] as $month => $count) {