Add options & state around expand/collapse
This commit is contained in:
@@ -3,3 +3,5 @@ archive_tree_styles:
|
|||||||
css:
|
css:
|
||||||
theme:
|
theme:
|
||||||
css/archive_tree.css: {}
|
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"),
|
* admin_label = @Translation("Archive tree"),
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
|
use Drupal\Core\Block\BlockForm;
|
||||||
|
use Drupal\Core\Form\FormStateInterface;
|
||||||
|
|
||||||
class ArchiveTreeBlock extends BlockBase {
|
class ArchiveTreeBlock extends BlockBase {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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() {
|
public function build() {
|
||||||
$storage = \Drupal::entityTypeManager()->getStorage('node');
|
$storage = \Drupal::entityTypeManager()->getStorage('node');
|
||||||
$query = $storage->getQuery()
|
$query = $storage->getQuery()
|
||||||
->condition('type', 'article')
|
->condition('type', 'article')
|
||||||
->condition('status', 1)
|
->condition('status', 1)
|
||||||
->sort('created', 'DESC')
|
->sort('created', 'DESC')
|
||||||
->accessCheck(TRUE)
|
->accessCheck(TRUE);
|
||||||
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
|
|
||||||
$nids = $query->execute();
|
$nids = $query->execute();
|
||||||
$nodes = $storage->loadMultiple($nids);
|
$nodes = $storage->loadMultiple($nids);
|
||||||
|
|
||||||
@@ -48,9 +72,11 @@ class ArchiveTreeBlock extends BlockBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$output = '';
|
$output = '';
|
||||||
|
$expand = !empty($this->configuration['expand_years']);
|
||||||
foreach ($tree as $year => $data) {
|
foreach ($tree as $year => $data) {
|
||||||
$year_url = '/archive-tree/' . $year;
|
$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 .= '<a href="' . $year_url . '">' . $year . '</a> (' . $data['count'] . ')';
|
||||||
$output .= '</summary>';
|
$output .= '</summary>';
|
||||||
foreach ($data['months'] as $month => $count) {
|
foreach ($data['months'] as $month => $count) {
|
||||||
|
|||||||
Reference in New Issue
Block a user