View source
<?php
namespace Drupal\dashboards\Plugin\SectionStorage;
use Drupal\user\UserDataInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\dashboards\Entity\Dashboard;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Symfony\Component\Routing\RouteCollection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\layout_builder\TempStoreIdentifierInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\layout_builder\Entity\SampleEntityGeneratorInterface;
class UserDashboardSectionStorage extends DashboardSectionStorage implements TempStoreIdentifierInterface {
protected $userData;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('layout_builder.sample_entity_generator'), $container
->get('current_user'), $container
->get('user.data'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_bundle_info, SampleEntityGeneratorInterface $sample_entity_generator, AccountInterface $account, UserDataInterface $user_data) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_bundle_info, $sample_entity_generator, $account);
$this->userData = $user_data;
}
public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
return TRUE;
}
public function buildRoutes(RouteCollection $collection) {
$requirements = [];
$this
->buildLayoutRoutes($collection, $this
->getPluginDefinition(), 'dashboard/{dashboard}/override', [
'parameters' => [
'dashboard' => [
'type' => 'entity:dashboard',
],
],
'view_mode' => 'user',
], $requirements, [
'_admin_route' => FALSE,
], '', 'dashboard');
}
public function deriveContextsFromRoute($value, $definition, $name, array $defaults) {
$contexts = [];
$id = !empty($value) ? $value : (!empty($defaults['dashboard']) ? $defaults['dashboard'] : NULL);
if ($id && ($entity = $this->entityTypeManager
->getStorage('dashboard')
->load($id))) {
if ($entity
->isOverriden()) {
$entity
->loadOverrides();
}
$contexts[Dashboard::CONTEXT_TYPE] = EntityContext::fromEntity($entity);
}
return $contexts;
}
public function save() {
$sections = [];
foreach ($this
->getDashboard()
->get('sections') as $delta => $section) {
$sections[$delta] = $section
->toArray();
}
$this->userData
->set('dashboards', $this->account
->id(), $this
->getDashboard()
->id(), serialize($sections));
return SAVED_UPDATED;
}
public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
if (!$account) {
$account = $this->account;
}
$result = AccessResult::allowedIfHasPermission($account, 'administer dashboards')
->orIf(AccessResult::allowedIfHasPermission($account, 'can override ' . $this
->getDashboard()
->id() . ' dashboard'));
if ($return_as_object) {
return $result;
}
return $result
->isAllowed();
}
public function getTempstoreKey() {
return $this
->getDashboard()
->id() . '_' . $this->account
->id();
}
}