View source
<?php
namespace Drupal\panelizer\Plugin\PanelsStorage;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\ctools\Context\AutomaticContext;
use Drupal\panelizer\Exception\PanelizerException;
use Drupal\panelizer\PanelizerInterface;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Drupal\panels\Storage\PanelsStorageBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelizerFieldPanelsStorage extends PanelsStorageBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $panelizer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PanelizerInterface $panelizer) {
$this->entityTypeManager = $entity_type_manager;
$this->panelizer = $panelizer;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
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('panelizer'));
}
protected function loadEntity($id) {
list($entity_type, $id, , $revision_id) = array_pad(explode(':', $id), 4, NULL);
$storage = $this->entityTypeManager
->getStorage($entity_type);
if ($revision_id) {
$entity = $storage
->loadRevision($revision_id);
}
else {
$entity = $storage
->load($id);
}
return $entity;
}
protected function getEntityContext($entity_type_id, EntityInterface $entity) {
return new AutomaticContext(EntityContextDefinition::fromEntityTypeId($entity_type_id), $entity);
}
public function load($id) {
if ($entity = $this
->loadEntity($id)) {
list($entity_type_id, , $view_mode) = explode(':', $id);
if ($panels_display = $this->panelizer
->getPanelsDisplay($entity, $view_mode)) {
$contexts = [
'@panelizer.entity_context:entity' => $this
->getEntityContext($entity_type_id, $entity),
];
$panels_display
->setContexts($contexts);
return $panels_display;
}
}
}
public function save(PanelsDisplayVariant $panels_display) {
$id = $panels_display
->getStorageId();
if ($entity = $this
->loadEntity($id)) {
list(, , $view_mode) = explode(':', $id);
if ($entity instanceof FieldableEntityInterface) {
$default = NULL;
if ($entity
->hasField('panelizer') && $entity->panelizer
->first()) {
foreach ($entity->panelizer as $item) {
if ($item->view_mode == $view_mode) {
$default = $item->default;
break;
}
}
}
try {
$this->panelizer
->setPanelsDisplay($entity, $view_mode, $default, $panels_display);
} catch (PanelizerException $e) {
throw new \Exception($e
->getMessage());
}
}
}
else {
throw new \Exception("Couldn't find entity to store Panels display on");
}
}
public function access($id, $op, AccountInterface $account) {
if ($entity = $this
->loadEntity($id)) {
$access = AccessResult::neutral()
->addCacheableDependency($account);
$entity_operations = [
'read' => 'view',
'update' => 'update',
'delete' => 'delete',
'change layout' => 'update',
];
$access
->orIf(isset($entity_operations[$op]) ? $entity
->access($entity_operations[$op], $account, TRUE) : AccessResult::forbidden());
if (!$access
->isForbidden() && $entity instanceof FieldableEntityInterface) {
list(, , $view_mode) = explode(':', $id);
if ($op == 'change layout') {
if ($this->panelizer
->hasEntityPermission('change layout', $entity, $view_mode, $account)) {
return $access
->orIf(AccessResult::allowed());
}
}
else {
if ($op == 'read' || $this->panelizer
->hasEntityPermission('change content', $entity, $view_mode, $account)) {
return $access
->orIf(AccessResult::allowed());
}
}
}
}
return AccessResult::forbidden();
}
}