Performance fixes
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
padding-bottom: 0;
|
||||
}
|
||||
.archive-tree-year {
|
||||
font-weight: bold;
|
||||
font-weight: normal;
|
||||
}
|
||||
.archive-tree-month {
|
||||
margin-left: 2em;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user