Add options & state around expand/collapse
This commit is contained in:
@@ -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