Performance fixes
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
padding-bottom: 0;
|
padding-bottom: 0;
|
||||||
}
|
}
|
||||||
.archive-tree-year {
|
.archive-tree-year {
|
||||||
font-weight: bold;
|
font-weight: normal;
|
||||||
}
|
}
|
||||||
.archive-tree-month {
|
.archive-tree-month {
|
||||||
margin-left: 2em;
|
margin-left: 2em;
|
||||||
|
|||||||
@@ -72,27 +72,35 @@ class ArchiveTreeBlock extends BlockBase {
|
|||||||
'#markup' => $this->t('No content types selected.'),
|
'#markup' => $this->t('No content types selected.'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
// 1. Get node IDs with access check.
|
||||||
$query = $storage->getQuery()
|
$query = $storage->getQuery()
|
||||||
->condition('type', $types, 'IN')
|
->condition('type', $types, 'IN')
|
||||||
->condition('status', 1)
|
->condition('status', 1)
|
||||||
->sort('created', 'DESC')
|
->sort('created', 'DESC')
|
||||||
->accessCheck(TRUE);
|
->accessCheck(TRUE);
|
||||||
$nids = $query->execute();
|
$nids = $query->execute();
|
||||||
$nodes = $storage->loadMultiple($nids);
|
|
||||||
|
|
||||||
$tree = [];
|
$tree = [];
|
||||||
foreach ($nodes as $node) {
|
if (!empty($nids)) {
|
||||||
$created = $node->getCreatedTime();
|
// 2. Fetch only nid and created fields for those nodes.
|
||||||
$year = date('Y', $created);
|
$connection = \Drupal::database();
|
||||||
$month = date('m', $created);
|
$result = $connection->select('node_field_data', 'n')
|
||||||
if (!isset($tree[$year])) {
|
->fields('n', ['nid', 'created'])
|
||||||
$tree[$year] = ['count' => 0, 'months' => []];
|
->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
|
krsort($tree); // Descending years
|
||||||
|
|||||||
Reference in New Issue
Block a user