DashboardStorage.php in Dashboards with Layout Builder 8
File
src/Entity/DashboardStorage.php
View source
<?php
namespace Drupal\dashboards\Entity;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityInterface;
use Drupal\layout_builder\Section;
class DashboardStorage extends ConfigEntityStorage {
public function loadMultipleOrderedByWeight(?array $ids = NULL) {
$entites = parent::loadMultiple($ids);
usort($entites, function ($a, $b) {
return $a
->get('weight') <=> $b
->get('weight');
});
return $entites;
}
protected function mapToStorageRecord(EntityInterface $entity) {
$record = parent::mapToStorageRecord($entity);
foreach ($record['sections'] as $delta => $section) {
$record['sections'][$delta] = $section
->toArray();
}
return $record;
}
protected function mapFromStorageRecords(array $records) {
foreach ($records as &$record) {
if (!empty($record['sections'])) {
$sections =& $record['sections'];
$sections = array_map([
Section::class,
'fromArray',
], $sections);
}
}
return parent::mapFromStorageRecords($records);
}
}