Added a test for basic counting accuracy

This commit is contained in:
aaron.axvig committed 2026-01-24 21:44:29 -06:00
1 parent ae7961ee07
commit 782cd300da
3 files changed
+186 -59

No files matched your search

+19 -59
View File
@@ -20,59 +20,26 @@ use Drupal\node\Entity\NodeType;
class ArchiveTreeBlock extends BlockBase {
/**
* {@inheritdoc}
/**
* Build the archive tree block content.
*
* - Gets node IDs with access check for selected content types.
* - Fetches only nid and created fields for those nodes (efficient).
* - Loops through, increments year and month countersfor display.
* - Outputs accessible HTML with details/summary and lists.
* - Caches per user, language, and theme for performance.
*/
public function defaultConfiguration() {
return [
'expand_years' => FALSE,
'content_types' => [],
] + parent::defaultConfiguration();
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$types = NodeType::loadMultiple();
$options = [];
foreach ($types as $type) {
$options[$type->id()] = $type->label();
}
$form['content_types'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Content types to include'),
'#options' => $options,
'#default_value' => isset($this->configuration['content_types']) ? $this->configuration['content_types'] : [],
'#description' => $this->t('Select one or more content types to include in the archive tree.'),
'#required' => TRUE,
];
$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');
$selected_types = array_keys(array_filter($form_state->getValue('content_types')));
$this->configuration['content_types'] = $selected_types;
}
public function build(): array {
$storage = \Drupal::entityTypeManager()->getStorage('node');
$types = array_filter($this->configuration['content_types']);
// Show a message if no content types are selected.
if (empty($types)) {
// If no types selected, return empty block.
return [
'#markup' => $this->t('No content types selected.'),
];
}
// 1. Get node IDs with access check.
// 1. Get node IDs with access check (respects permissions).
$query = $storage->getQuery()
->condition('type', $types, 'IN')
->condition('status', 1)
@@ -82,7 +49,7 @@ class ArchiveTreeBlock extends BlockBase {
$tree = [];
if (!empty($nids)) {
// 2. Fetch only nid and created fields for those nodes.
// 2. Fetch only nid and created fields for those nodes (faster than loading full entities).
$connection = \Drupal::database();
$result = $connection->select('node_field_data', 'n')
->fields('n', ['nid', 'created'])
@@ -103,11 +70,13 @@ class ArchiveTreeBlock extends BlockBase {
}
}
krsort($tree); // Descending years
// Sort years and months in descending order for display.
krsort($tree);
foreach ($tree as $year => $data) {
krsort($tree[$year]['months']); // Descending months
krsort($tree[$year]['months']);
}
// Build accessible HTML output for the archive tree.
$output = '';
$expand = !empty($this->configuration['expand_years']);
$type_arg = implode(',', $types);
@@ -130,6 +99,7 @@ class ArchiveTreeBlock extends BlockBase {
$output .= '</details>';
}
// Return render array with proper cache settings and allowed tags.
return [
'#markup' => '<div class="archive-tree-block">' . $output . '</div>',
'#allowed_tags' => ['details', 'summary', 'a', 'div', 'ul', 'li'],
@@ -139,18 +109,8 @@ class ArchiveTreeBlock extends BlockBase {
],
],
'#cache' => [
// Cache per user, language, route, and theme for accuracy.
'contexts' => [
'user',
'languages',
'theme',
],
// Invalidate when any node changes or content type config changes.
'tags' => [
'node_list',
'config:node.type',
],
// Cache for 1 hour (3600 seconds) for performance, but not forever.
'contexts' => ['user', 'languages', 'theme'],
'tags' => ['node_list', 'config:node.type'],
'max-age' => 3600,
],
];