Add options & state around expand/collapse
This commit is contained in:
@@ -3,3 +3,5 @@ archive_tree_styles:
|
||||
css:
|
||||
theme:
|
||||
css/archive_tree.css: {}
|
||||
js:
|
||||
js/archive_tree.js: {}
|
||||
|
||||
22
js/archive_tree.js
Normal file
22
js/archive_tree.js
Normal file
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
})();
|
||||
@@ -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 .= '<details><summary class="archive-tree-year">';
|
||||
$open = $expand ? ' open' : '';
|
||||
$output .= '<details' . $open . '><summary class="archive-tree-year">';
|
||||
$output .= '<a href="' . $year_url . '">' . $year . '</a> (' . $data['count'] . ')';
|
||||
$output .= '</summary>';
|
||||
foreach ($data['months'] as $month => $count) {
|
||||
|
||||
Reference in New Issue
Block a user