diff --git a/css/archive_tree.css b/css/archive_tree.css index 70ae630..a21d37c 100644 --- a/css/archive_tree.css +++ b/css/archive_tree.css @@ -7,7 +7,7 @@ padding-bottom: 0; } .archive-tree-year { - font-weight: bold; + font-weight: normal; } .archive-tree-month { margin-left: 2em; diff --git a/src/Plugin/Block/ArchiveTreeBlock.php b/src/Plugin/Block/ArchiveTreeBlock.php index 601ee02..5ad2ed0 100644 --- a/src/Plugin/Block/ArchiveTreeBlock.php +++ b/src/Plugin/Block/ArchiveTreeBlock.php @@ -72,27 +72,35 @@ class ArchiveTreeBlock extends BlockBase { '#markup' => $this->t('No content types selected.'), ]; } + // 1. Get node IDs with access check. $query = $storage->getQuery() ->condition('type', $types, 'IN') ->condition('status', 1) ->sort('created', 'DESC') ->accessCheck(TRUE); $nids = $query->execute(); - $nodes = $storage->loadMultiple($nids); $tree = []; - foreach ($nodes as $node) { - $created = $node->getCreatedTime(); - $year = date('Y', $created); - $month = date('m', $created); - if (!isset($tree[$year])) { - $tree[$year] = ['count' => 0, 'months' => []]; + if (!empty($nids)) { + // 2. Fetch only nid and created fields for those nodes. + $connection = \Drupal::database(); + $result = $connection->select('node_field_data', 'n') + ->fields('n', ['nid', 'created']) + ->condition('n.nid', $nids, 'IN') + ->execute(); + foreach ($result as $row) { + $created = $row->created; + $year = date('Y', $created); + $month = date('m', $created); + if (!isset($tree[$year])) { + $tree[$year] = ['count' => 0, 'months' => []]; + } + if (!isset($tree[$year]['months'][$month])) { + $tree[$year]['months'][$month] = ['count' => 0]; + } + $tree[$year]['count']++; + $tree[$year]['months'][$month]['count']++; } - if (!isset($tree[$year]['months'][$month])) { - $tree[$year]['months'][$month] = ['count' => 0]; - } - $tree[$year]['count']++; - $tree[$year]['months'][$month]['count']++; } krsort($tree); // Descending years