diff --git a/page_manager.services.yml b/page_manager.services.yml index 7381c31..39bdb42 100644 --- a/page_manager.services.yml +++ b/page_manager.services.yml @@ -34,5 +34,6 @@ services: arguments: ['@current_route_match'] page_manager.panels_storage: class: Drupal\page_manager\PanelsStorage + arguments: ['@entity_type.manager'] tags: - { name: panels_storage } diff --git a/src/PanelsStorage.php b/src/PanelsStorage.php index 37564b9..4124296 100644 --- a/src/PanelsStorage.php +++ b/src/PanelsStorage.php @@ -1,4 +1,5 @@ entityTypeManager = $entity_type_manager; + } + + /** + * Load a page variant entity. + * + * @param string $id + * The page variant entity's id. + * + * @return \Drupal\page_manager\PageVariantInterface + */ + protected function loadPageVariant($id) { + return $this->entityTypeManager->getStorage('page_variant')->load($id); + } + + /** * {@inheritdoc} */ public function save(PanelsDisplayVariant $panels_display) { $id = $panels_display->getStorageId(); - - /** @var \Drupal\page_manager\Entity\PageVariant $page_variant */ - // @todo: inject the proper service to load these.. - $page_variant = PageVariant::load($id); - $page_variant->getVariantPlugin()->setConfiguration($panels_display->getConfiguration()); - $page_variant->save(); + if ($id && ($page_variant = $this->loadPageVariant($id))) { + $variant_plugin = $page_variant->getVariantPlugin(); + if (!($variant_plugin instanceof PanelsDisplayVariant)) { + throw new \Exception("Page variant doesn't use a Panels display variant"); + } + $variant_plugin->setConfiguration($panels_display->getConfiguration()); + $page_variant->save(); + } + else { + throw new \Exception("Couuldn't find page variant to store Panels display"); + } } /** * {@inheritdoc} */ public function load($id) { - /** @var \Drupal\page_manager\Entity\PageVariant $page_variant */ - // @todo: inject the proper service to load these.. - $page_variant = PageVariant::load($id); - /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display */ - $panels_display = $page_variant->getVariantPlugin(); - $panels_display->setContexts($page_variant->getContexts()); - return $panels_display; + if ($page_variant = $this->loadPageVariant($id)) { + /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display */ + $panels_display = $page_variant->getVariantPlugin(); + + // If this page variant doesn't have a Panels display on it, then we treat + // it the same as if there was no such page variant. + if (!($panels_display instanceof PanelsDisplayVariant)) { + return; + } + + // Pass down the contexts because the display has no other way to get them + // from the variant. + $panels_display->setContexts($page_variant->getContexts()); + + return $panels_display; + } } /** * {@inheritdoc} */ public function access($id, $op, AccountInterface $account) { - /** @var \Drupal\page_manager\Entity\PageVariant $page_variant */ - // @todo: inject the proper service to load these.. - $page_variant = PageVariant::load($id); - return $page_variant->access($op, $account, TRUE); + if ($page_variant = $this->loadPageVariant($id)) { + return $page_variant->access($op, $account, TRUE); + } + + return AccessResult::forbidden(); } } \ No newline at end of file