diff --git a/config/schema/page_manager.schema.yml b/config/schema/page_manager.schema.yml index 8c6f225..6ac5f83 100644 --- a/config/schema/page_manager.schema.yml +++ b/config/schema/page_manager.schema.yml @@ -1,94 +1,13 @@ page_manager.page.*: - type: config_entity + type: ctools.display_base label: 'Page' mapping: - id: - type: string - label: 'Machine-readable name' - label: - type: label - label: 'Label' use_admin_theme: type: boolean label: 'Whether the page is displayed using the admin theme or not' path: type: string label: 'Page path' - access_logic: - type: string - label: 'Access logic' - access_conditions: - type: sequence - label: 'Access Conditions' - sequence: - - type: condition.plugin.[id] - label: 'Access Condition' - parameters: - type: sequence - label: Parameter context list - sequence: - - type: mapping - label: Parameter context - mapping: - machine_name: - type: string - label: 'Machine-readable name' - label: - type: label - label: 'Label of the context' - type: - type: string - label: 'Context type' - -page_manager.page_variant.*: - type: config_entity - label: 'Page variant configuration' - mapping: - id: - type: string - label: 'ID' - label: - type: label - label: 'Label' - weight: - type: integer - label: 'Weight' - uuid: - type: string - label: 'UUID' - variant: - type: string - label: 'Display variant' - variant_settings: - type: display_variant.plugin.[%parent.variant] - page: - type: string - label: 'Parent page' - selection_criteria: - type: sequence - label: 'Selection criteria' - sequence: - - type: condition.plugin.[id] - label: 'Selection condition' - selection_logic: - type: string - label: 'Selection logic' - static_context: - type: sequence - label: Static context list - sequence: - - type: mapping - label: 'Static context' - mapping: - label: - type: label - label: 'Label of the context' - type: - type: string - label: 'Context type' - value: - type: string - label: 'Context value' page_manager.block_plugin.*: type: block.settings.[id] diff --git a/page_manager.module b/page_manager.module index 422f159..513ef4b 100644 --- a/page_manager.module +++ b/page_manager.module @@ -13,6 +13,7 @@ use Drupal\page_manager\Entity\PageVariantConfigMapper; * @todo Remove once https://www.drupal.org/node/2670712 is in. */ function page_manager_config_translation_info_alter(&$info) { + // @todo: Update this for the switch to generic display variants. if (isset($info['page_variant'])) { $info['page_variant']['class'] = PageVariantConfigMapper::class; } diff --git a/src/Entity/Page.php b/src/Entity/Page.php index 49c5a8a..0eabfa2 100644 --- a/src/Entity/Page.php +++ b/src/Entity/Page.php @@ -7,14 +7,11 @@ namespace Drupal\page_manager\Entity; -use Drupal\Component\Plugin\Context\ContextInterface; use Drupal\page_manager\Event\PageManagerContextEvent; use Drupal\page_manager\Event\PageManagerEvents; use Drupal\page_manager\PageInterface; -use Drupal\Core\Condition\ConditionPluginCollection; -use Drupal\Core\Config\Entity\ConfigEntityBase; +use Drupal\ctools\Entity\DisplayBase; use Drupal\Core\Entity\EntityStorageInterface; -use Drupal\page_manager\PageVariantInterface; /** * Defines a Page entity class. @@ -23,7 +20,7 @@ use Drupal\page_manager\PageVariantInterface; * id = "page", * label = @Translation("Page"), * handlers = { - * "access" = "Drupal\page_manager\Entity\PageAccess", + * "access" = "Drupal\ctools\Entity\DisplayAccess", * }, * admin_permission = "administer pages", * entity_keys = { @@ -42,22 +39,7 @@ use Drupal\page_manager\PageVariantInterface; * }, * ) */ -class Page extends ConfigEntityBase implements PageInterface { - - /** - * The ID of the page entity. - * - * @var string - */ - protected $id; - - /** - * The label of the page entity. - * - * @var string - */ - protected $label; - +class Page extends DisplayBase implements PageInterface { /** * The path of the page entity. * @@ -66,41 +48,6 @@ class Page extends ConfigEntityBase implements PageInterface { protected $path; /** - * The page variant entities. - * - * @var \Drupal\page_manager\PageVariantInterface[]. - */ - protected $variants; - - /** - * An array of collected contexts. - * - * @var \Drupal\Component\Plugin\Context\ContextInterface[] - */ - protected $contexts = []; - - /** - * The configuration of access conditions. - * - * @var array - */ - protected $access_conditions = []; - - /** - * Tracks the logic used to compute access, either 'and' or 'or'. - * - * @var string - */ - protected $access_logic = 'and'; - - /** - * The plugin collection that holds the access conditions. - * - * @var \Drupal\Component\Plugin\LazyPluginCollection - */ - protected $accessConditionCollection; - - /** * Indicates if this page should be displayed in the admin theme. * * @var bool @@ -108,19 +55,6 @@ class Page extends ConfigEntityBase implements PageInterface { protected $use_admin_theme; /** - * Parameter context configuration. - * - * An associative array keyed by parameter name, which contains associative - * arrays with the following keys: - * - machine_name: Machine-readable context name. - * - label: Human-readable context name. - * - type: Context type. - * - * @var array[] - */ - protected $parameters = []; - - /** * {@inheritdoc} */ public function getPath() { @@ -161,102 +95,6 @@ class Page extends ConfigEntityBase implements PageInterface { } /** - * Wraps the entity storage for page variants. - * - * @return \Drupal\Core\Entity\EntityStorageInterface - */ - protected function variantStorage() { - return \Drupal::service('entity_type.manager')->getStorage('page_variant'); - } - - /** - * {@inheritdoc} - */ - public function getPluginCollections() { - return [ - 'access_conditions' => $this->getAccessConditions(), - ]; - } - - /** - * {@inheritdoc} - */ - public function getAccessConditions() { - if (!$this->accessConditionCollection) { - $this->accessConditionCollection = new ConditionPluginCollection(\Drupal::service('plugin.manager.condition'), $this->get('access_conditions')); - } - return $this->accessConditionCollection; - } - - /** - * {@inheritdoc} - */ - public function addAccessCondition(array $configuration) { - $configuration['uuid'] = $this->uuidGenerator()->generate(); - $this->getAccessConditions()->addInstanceId($configuration['uuid'], $configuration); - return $configuration['uuid']; - } - - /** - * {@inheritdoc} - */ - public function getAccessCondition($condition_id) { - return $this->getAccessConditions()->get($condition_id); - } - - /** - * {@inheritdoc} - */ - public function removeAccessCondition($condition_id) { - $this->getAccessConditions()->removeInstanceId($condition_id); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getAccessLogic() { - return $this->access_logic; - } - - /** - * {@inheritdoc} - */ - public function getParameters() { - return $this->parameters; - } - - /** - * {@inheritdoc} - */ - public function getParameter($name) { - if (!isset($this->parameters[$name])) { - $this->setParameter($name, ''); - } - return $this->parameters[$name]; - } - - /** - * {@inheritdoc} - */ - public function setParameter($name, $type, $label = '') { - $this->parameters[$name] = [ - 'machine_name' => $name, - 'type' => $type, - 'label' => $label, - ]; - return $this; - } - - /** - * {@inheritdoc} - */ - public function removeParameter($name) { - unset($this->parameters[$name]); - return $this; - } - - /** * {@inheritdoc} */ public function getParameterNames() { @@ -292,13 +130,6 @@ class Page extends ConfigEntityBase implements PageInterface { /** * {@inheritdoc} */ - public function addContext($name, ContextInterface $value) { - $this->contexts[$name] = $value; - } - - /** - * {@inheritdoc} - */ public function getContexts() { if (!$this->contexts) { $this->eventDispatcher()->dispatch(PageManagerEvents::PAGE_CONTEXT, new PageManagerContextEvent($this)); @@ -307,62 +138,6 @@ class Page extends ConfigEntityBase implements PageInterface { } /** - * {@inheritdoc} - */ - public function addVariant(PageVariantInterface $variant) { - $this->variants[$variant->id()] = $variant; - return $this; - } - - /** - * {@inheritdoc} - */ - public function getVariant($variant_id) { - $variants = $this->getVariants(); - if (!isset($variants[$variant_id])) { - throw new \UnexpectedValueException('The requested variant does not exist or is not associated with this page'); - } - return $variants[$variant_id]; - } - - /** - * {@inheritdoc} - */ - public function removeVariant($variant_id) { - $this->getVariant($variant_id)->delete(); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getVariants() { - if (!isset($this->variants)) { - $this->variants = []; - /** @var \Drupal\page_manager\PageVariantInterface $variant */ - foreach ($this->variantStorage()->loadByProperties(['page' => $this->id()]) as $variant) { - $this->variants[$variant->id()] = $variant; - } - // Suppress errors because of https://bugs.php.net/bug.php?id=50688. - @uasort($this->variants, [$this, 'variantSortHelper']); - } - return $this->variants; - } - - /** - * {@inheritdoc} - */ - public function variantSortHelper($a, $b) { - $a_weight = $a->getWeight(); - $b_weight = $b->getWeight(); - if ($a_weight == $b_weight) { - return 0; - } - - return ($a_weight < $b_weight) ? -1 : 1; - } - - /** * Wraps the event dispatcher. * * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface @@ -372,31 +147,4 @@ class Page extends ConfigEntityBase implements PageInterface { return \Drupal::service('event_dispatcher'); } - /** - * {@inheritdoc} - */ - public function __sleep() { - $vars = parent::__sleep(); - - // Ensure any plugin collections are stored correctly before serializing. - // @todo Let https://www.drupal.org/node/2650588 handle this instead. - foreach ($this->getPluginCollections() as $plugin_config_key => $plugin_collection) { - $this->set($plugin_config_key, $plugin_collection->getConfiguration()); - } - - // Avoid serializing plugin collections and entities as they might contain - // references to a lot of objects including the container. - $unset_vars = [ - 'variants', - 'accessConditionCollection', - ]; - foreach ($unset_vars as $unset_var) { - if (!empty($this->{$unset_var})) { - unset($vars[array_search($unset_var, $vars)]); - } - } - - return $vars; - } - } diff --git a/src/Entity/PageAccess.php b/src/Entity/PageAccess.php deleted file mode 100644 index faac645..0000000 --- a/src/Entity/PageAccess.php +++ /dev/null @@ -1,89 +0,0 @@ -contextHandler = $context_handler; - } - - /** - * {@inheritdoc} - */ - public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { - return new static( - $entity_type, - $container->get('context.handler') - ); - } - - /** - * Wraps the context handler. - * - * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface - */ - protected function contextHandler() { - return $this->contextHandler; - } - - /** - * {@inheritdoc} - */ - protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { - /** @var \Drupal\page_manager\PageInterface $entity */ - if ($operation == 'view') { - if (!$entity->status()) { - return AccessResult::forbidden()->addCacheableDependency($entity); - } - - $contexts = $entity->getContexts(); - $conditions = $entity->getAccessConditions(); - foreach ($conditions as $condition) { - if ($condition instanceof ContextAwarePluginInterface) { - $this->contextHandler()->applyContextMapping($condition, $contexts); - } - } - return AccessResult::allowedIf($this->resolveConditions($conditions, $entity->getAccessLogic())); - } - return parent::checkAccess($entity, $operation, $account); - } - -} diff --git a/src/Entity/PageVariant.php b/src/Entity/PageVariant.php deleted file mode 100644 index f6b729c..0000000 --- a/src/Entity/PageVariant.php +++ /dev/null @@ -1,436 +0,0 @@ -getCacheTagsToInvalidate()); - } - - /** - * {@inheritdoc} - */ - protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) { - parent::invalidateTagsOnDelete($entity_type, $entities); - - // The parent doesn't invalidate the entity cache tags on delete because the - // config system will invalidate them, but since we're using the parent - // page's cache tags, we need to invalidate them special. - $tags = []; - foreach ($entities as $entity) { - $tags = Cache::mergeTags($tags, $entity->getCacheTagsToInvalidate()); - } - Cache::invalidateTags($tags); - } - - /** - * {@inheritdoc} - */ - public function getCacheTagsToInvalidate() { - // We use the same cache tags as the parent page. - return $this->getPage()->getCacheTagsToInvalidate(); - } - - /** - * {@inheritdoc} - */ - public function calculateDependencies() { - parent::calculateDependencies(); - - $this->addDependency('config', $this->getPage()->getConfigDependencyName()); - - foreach ($this->getSelectionConditions() as $instance) { - $this->calculatePluginDependencies($instance); - } - - return $this->getDependencies(); - } - - /** - * {@inheritdoc} - */ - public function getPluginCollections() { - return [ - 'selection_criteria' => $this->getSelectionConditions(), - 'variant_settings' => $this->getVariantPluginCollection(), - ]; - } - - /** - * Get the plugin collection that holds the single variant plugin instance. - * - * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection - * The plugin collection that holds the single variant plugin instance. - */ - protected function getVariantPluginCollection() { - if (!$this->variantPluginCollection) { - $this->variantPluginCollection = new DefaultSingleLazyPluginCollection(\Drupal::service('plugin.manager.display_variant'), $this->variant, $this->variant_settings); - } - return $this->variantPluginCollection; - } - - /** - * {@inheritdoc} - */ - public function getVariantPlugin() { - return $this->getVariantPluginCollection()->get($this->variant); - } - - /** - * {@inheritdoc} - */ - public function getVariantPluginId() { - return $this->variant; - } - - /** - * {@inheritdoc} - */ - public function getPage() { - if (!$this->page) { - throw new \UnexpectedValueException('The page variant has no associated page'); - } - return $this->getPageStorage()->load($this->page); - } - - /** - * {@inheritdoc} - */ - public function getContexts() { - if (is_null($this->contexts)) { - $static_contexts = $this->getContextMapper()->getContextValues($this->getStaticContexts()); - $page_contexts = $this->getPage()->getContexts(); - $this->contexts = array_merge($static_contexts, $page_contexts); - } - return $this->contexts; - } - - /** - * Resets the collected contexts. - * - * @return $this - */ - protected function resetCollectedContexts() { - $this->contexts = NULL; - return $this; - } - - /** - * {@inheritdoc} - */ - public function getWeight() { - return $this->weight; - } - - /** - * {@inheritdoc} - */ - public function setWeight($weight) { - $this->weight = $weight; - return $this; - } - - /** - * {@inheritdoc} - */ - public function getSelectionLogic() { - return $this->get('selection_logic'); - } - - /** - * {@inheritdoc} - */ - protected function getSelectionConfiguration() { - return $this->get('selection_criteria'); - } - - /** - * {@inheritdoc} - */ - public function getSelectionConditions() { - if (!$this->selectionConditionCollection) { - $this->selectionConditionCollection = new ConditionPluginCollection($this->getConditionManager(), $this->getSelectionConfiguration()); - } - return $this->selectionConditionCollection; - } - - /** - * {@inheritdoc} - */ - public function addSelectionCondition(array $configuration) { - $configuration['uuid'] = $this->uuidGenerator()->generate(); - $this->getSelectionConditions()->addInstanceId($configuration['uuid'], $configuration); - return $configuration['uuid']; - } - - /** - * {@inheritdoc} - */ - public function getSelectionCondition($condition_id) { - return $this->getSelectionConditions()->get($condition_id); - } - - /** - * {@inheritdoc} - */ - public function removeSelectionCondition($condition_id) { - $this->getSelectionConditions()->removeInstanceId($condition_id); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getStaticContexts() { - return $this->static_context; - } - - /** - * {@inheritdoc} - */ - public function getStaticContext($name) { - if (isset($this->static_context[$name])) { - return $this->static_context[$name]; - } - return []; - } - - /** - * {@inheritdoc} - */ - public function setStaticContext($name, $configuration) { - $this->static_context[$name] = $configuration; - $this->resetCollectedContexts(); - return $this; - } - - /** - * {@inheritdoc} - */ - public function removeStaticContext($name) { - unset($this->static_context[$name]); - $this->resetCollectedContexts(); - return $this; - } - - /** - * {@inheritdoc} - */ - protected function urlRouteParameters($rel) { - $parameters = parent::urlRouteParameters($rel); - $parameters['page'] = $this->get('page'); - return $parameters; - } - - /** - * {@inheritdoc} - */ - public function postSave(EntityStorageInterface $storage, $update = TRUE) { - parent::postSave($storage, $update); - static::routeBuilder()->setRebuildNeeded(); - } - - /** - * {@inheritdoc} - */ - public static function postDelete(EntityStorageInterface $storage, array $entities) { - parent::postDelete($storage, $entities); - static::routeBuilder()->setRebuildNeeded(); - } - - /** - * Wraps the route builder. - * - * @return \Drupal\Core\Routing\RouteBuilderInterface - * An object for state storage. - */ - protected static function routeBuilder() { - return \Drupal::service('router.builder'); - } - - /** - * Wraps the condition plugin manager. - * - * @return \Drupal\Core\Condition\ConditionManager - */ - protected function getConditionManager() { - return \Drupal::service('plugin.manager.condition'); - } - - /** - * Wraps the context mapper. - * - * @return \Drupal\page_manager\ContextMapperInterface - */ - protected function getContextMapper() { - return \Drupal::service('page_manager.context_mapper'); - } - - /** - * Wraps the page entity storage. - * - * @return \Drupal\Core\Entity\EntityStorageInterface - */ - protected function getPageStorage() { - return \Drupal::entityTypeManager()->getStorage('page'); - } - - /** - * {@inheritdoc} - */ - public function __sleep() { - $vars = parent::__sleep(); - // Gathered contexts objects should not be serialized. - unset($vars[array_search('contexts', $vars)]); - return $vars; - } - -} diff --git a/src/Entity/PageVariantAccess.php b/src/Entity/PageVariantAccess.php deleted file mode 100644 index 3893b6d..0000000 --- a/src/Entity/PageVariantAccess.php +++ /dev/null @@ -1,85 +0,0 @@ -contextHandler = $context_handler; - } - - /** - * {@inheritdoc} - */ - public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { - return new static( - $entity_type, - $container->get('context.handler') - ); - } - - /** - * Wraps the context handler. - * - * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface - */ - protected function contextHandler() { - return $this->contextHandler; - } - - /** - * {@inheritdoc} - */ - protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { - /** @var \Drupal\page_manager\PageVariantInterface $entity */ - if ($operation === 'view') { - $contexts = $entity->getContexts(); - $conditions = $entity->getSelectionConditions(); - foreach ($conditions as $condition) { - if ($condition instanceof ContextAwarePluginInterface) { - $this->contextHandler()->applyContextMapping($condition, $contexts); - } - } - return AccessResult::allowedIf($this->resolveConditions($conditions, $entity->getSelectionLogic())); - } - return parent::checkAccess($entity, $operation, $account); - } - -} diff --git a/src/Entity/PageVariantViewBuilder.php b/src/Entity/PageVariantViewBuilder.php deleted file mode 100644 index c9b74d0..0000000 --- a/src/Entity/PageVariantViewBuilder.php +++ /dev/null @@ -1,83 +0,0 @@ -getVariantPlugin(); - if ($variant_plugin instanceof ContextAwareVariantInterface) { - $variant_plugin->setContexts($entity->getContexts()); - } - if ($variant_plugin instanceof RefinableCacheableDependencyInterface) { - $variant_plugin->addCacheableDependency($entity); - } - return $variant_plugin->build(); - } - - /** - * {@inheritdoc} - */ - public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) { - $build = []; - foreach ($entities as $key => $entity) { - $build[$key] = $this->view($entity, $view_mode, $langcode); - } - return $build; - } - - /** - * {@inheritdoc} - */ - public function resetCache(array $entities = NULL) { - // Intentionally empty. - } - - /** - * {@inheritdoc} - */ - public function getCacheTags() { - // Intentionally empty. - } - - /** - * {@inheritdoc} - */ - public function buildComponents(array &$build, array $entities, array $displays, $view_mode) { - throw new \LogicException(); - } - - /** - * {@inheritdoc} - */ - public function viewField(FieldItemListInterface $items, $display_options = array()) { - throw new \LogicException(); - } - - /** - * {@inheritdoc} - */ - public function viewFieldItem(FieldItemInterface $item, $display_options = array()) { - throw new \LogicException(); - } - -} diff --git a/src/EventSubscriber/RouteParamContext.php b/src/EventSubscriber/RouteParamContext.php index d7d279c..ef88467 100644 --- a/src/EventSubscriber/RouteParamContext.php +++ b/src/EventSubscriber/RouteParamContext.php @@ -66,7 +66,7 @@ class RouteParamContext implements EventSubscriberInterface { if ($route && $route_contexts = $route->getOption('parameters')) { foreach ($route_contexts as $route_context_name => $route_context) { // Skip this parameter. - if ($route_context_name == 'page_manager_page_variant' || $route_context_name == 'page_manager_page') { + if ($route_context_name == 'page_manager_display_variant' || $route_context_name == 'page_manager_page') { continue; } diff --git a/src/PageInterface.php b/src/PageInterface.php index 96eaab2..e6fd4c9 100644 --- a/src/PageInterface.php +++ b/src/PageInterface.php @@ -7,22 +7,12 @@ namespace Drupal\page_manager; -use Drupal\Component\Plugin\Context\ContextInterface; -use Drupal\Core\Config\Entity\ConfigEntityInterface; -use Drupal\Core\Entity\EntityWithPluginCollectionInterface; +use Drupal\ctools\Entity\DisplayInterface; /** * Provides an interface for page entities. */ -interface PageInterface extends ConfigEntityInterface, EntityWithPluginCollectionInterface { - - /** - * Returns whether the page entity is enabled. - * - * @return bool - * Whether the page entity is enabled or not. - */ - public function status(); +interface PageInterface extends DisplayInterface { /** * Returns the path for the page entity. @@ -41,160 +31,10 @@ interface PageInterface extends ConfigEntityInterface, EntityWithPluginCollectio public function usesAdminTheme(); /** - * Adds a variant to this page. - * - * @param \Drupal\page_manager\PageVariantInterface $variant - * A page variant entity. - * - * @return $this - */ - public function addVariant(PageVariantInterface $variant); - - /** - * Retrieves a specific variant. - * - * @param string $variant_id - * The variant ID. - * - * @return \Drupal\page_manager\PageVariantInterface - * The variant object. - */ - public function getVariant($variant_id); - - /** - * Removes a specific variant. - * - * @param string $variant_id - * The variant ID. - * - * @return $this - */ - public function removeVariant($variant_id); - - /** - * Returns the variants available for the entity. - * - * @return \Drupal\page_manager\PageVariantInterface[] - * An array of the variants. - */ - public function getVariants(); - - /** - * Returns the conditions used for determining access for this page entity. - * - * @return \Drupal\Core\Condition\ConditionInterface[]|\Drupal\Core\Condition\ConditionPluginCollection - * An array of configured condition plugins. - */ - public function getAccessConditions(); - - /** - * Adds a new access condition to the page entity. - * - * @param array $configuration - * An array of configuration for the new access condition. - * - * @return string - * The access condition ID. - */ - public function addAccessCondition(array $configuration); - - /** - * Retrieves a specific access condition. - * - * @param string $condition_id - * The access condition ID. - * - * @return \Drupal\Core\Condition\ConditionInterface - * The access condition object. - */ - public function getAccessCondition($condition_id); - - /** - * Removes a specific access condition. - * - * @param string $condition_id - * The access condition ID. - * - * @return $this - */ - public function removeAccessCondition($condition_id); - - /** - * Returns the logic used to compute access, either 'and' or 'or'. - * - * @return string - * The string 'and', or the string 'or'. - */ - public function getAccessLogic(); - - /** - * Returns the parameter context value objects for this page entity. - * - * @return array[] - * An array of parameter context arrays, keyed by parameter name. - */ - public function getParameters(); - - /** - * Retrieves a specific parameter context. - * - * @param string $name - * The parameter context's unique name. - * - * @return array - * The parameter context array. - */ - public function getParameter($name); - - /** - * Adds/updates a given parameter context. - * - * @param string $name - * The parameter context name. - * @param string $type - * The parameter context type. - * @param string $label - * (optional) The parameter context label. - * - * @return $this - */ - public function setParameter($name, $type, $label = ''); - - /** - * Removes a specific parameter context. - * - * @param string $name - * The parameter context's unique machine name. - * - * @return $this - */ - public function removeParameter($name); - - /** * Gets the names of all parameters for this page. * * @return string[] */ public function getParameterNames(); - /** - * Gets the values for all defined contexts. - * - * @return \Drupal\Core\Plugin\Context\ContextInterface[] - * An array of set context values, keyed by context name. - */ - public function getContexts(); - - /** - * Sets the context for a given name. - * - * @param string $name - * The name of the context. - * @param \Drupal\Component\Plugin\Context\ContextInterface $value - * The context to add. - * - * @return $this - */ - public function addContext($name, ContextInterface $value); - } diff --git a/src/PageVariantInterface.php b/src/PageVariantInterface.php deleted file mode 100644 index 9705d8c..0000000 --- a/src/PageVariantInterface.php +++ /dev/null @@ -1,151 +0,0 @@ - 'page_manager_page_variant', + '_entity_view' => 'display_variant', '_title' => $entity->label(), - 'page_manager_page_variant' => $variant_id, + 'page_manager_display_variant' => $variant_id, 'page_manager_page' => $page_id, - 'page_manager_page_variant_weight' => $variant->getWeight(), + 'page_manager_display_variant_weight' => $variant->getWeight(), // When adding multiple variants, the variant ID is added to the // route name. In order to convey the base route name for this set // of variants, add it as a parameter. diff --git a/src/Routing/VariantRouteFilter.php b/src/Routing/VariantRouteFilter.php index 6d2abcc..5dcef9e 100644 --- a/src/Routing/VariantRouteFilter.php +++ b/src/Routing/VariantRouteFilter.php @@ -28,11 +28,11 @@ class VariantRouteFilter implements RouteFilterInterface { use RouteEnhancerCollectorTrait; /** - * The page variant storage. + * The display variant storage. * * @var \Drupal\Core\Entity\EntityStorageInterface */ - protected $pageVariantStorage; + protected $displayVariantStorage; /** * The current path stack. @@ -50,7 +50,7 @@ class VariantRouteFilter implements RouteFilterInterface { * The current path stack. */ public function __construct(EntityTypeManagerInterface $entity_type_manager, CurrentPathStack $current_path) { - $this->pageVariantStorage = $entity_type_manager->getStorage('page_variant'); + $this->displayVariantStorage = $entity_type_manager->getStorage('display_variant'); $this->currentPath = $current_path; } @@ -59,7 +59,7 @@ class VariantRouteFilter implements RouteFilterInterface { */ public function applies(Route $route) { $parameters = $route->getOption('parameters'); - return !empty($parameters['page_manager_page_variant']); + return !empty($parameters['page_manager_display_variant']); } /** @@ -90,8 +90,8 @@ class VariantRouteFilter implements RouteFilterInterface { $attributes = $this->getRequestAttributes($route, $name, $request); // Add the enhanced attributes to the request. $request->attributes->add($attributes); - if ($page_variant_id = $route->getDefault('page_manager_page_variant')) { - if ($this->checkPageVariantAccess($page_variant_id)) { + if ($display_variant_id = $route->getDefault('page_manager_display_variant')) { + if ($this->checkDisplayVariantAccess($display_variant_id)) { // Access granted, use this route. Do not restore request attributes // but keep those from this route by breaking out. $accessible_route_name = $name; @@ -108,7 +108,8 @@ class VariantRouteFilter implements RouteFilterInterface { // Because the sort order of $routes is unreliable for a route without a // variant weight, rely on the original order of $collection here. foreach ($collection as $name => $route) { - if ($route->getDefault('page_manager_page_variant')) { + /* @var \Symfony\Component\Routing\Route $route */ + if ($route->getDefault('page_manager_display_variant')) { if ($accessible_route_name !== $name) { // Remove all other page manager routes. $collection->remove($name); @@ -128,8 +129,8 @@ class VariantRouteFilter implements RouteFilterInterface { * Sort callback for routes based on the variant weight. */ protected function routeWeightSort(Route $a, Route $b) { - $a_weight = $a->getDefault('page_manager_page_variant_weight'); - $b_weight = $b->getDefault('page_manager_page_variant_weight'); + $a_weight = $a->getDefault('page_manager_display_variant_weight'); + $b_weight = $b->getDefault('page_manager_display_variant_weight'); if ($a_weight === $b_weight) { return 0; } @@ -146,15 +147,15 @@ class VariantRouteFilter implements RouteFilterInterface { /** * Checks access of a page variant. * - * @param string $page_variant_id + * @param string $display_variant_id * The page variant ID. * * @return bool * TRUE if the route is valid, FALSE otherwise. */ - protected function checkPageVariantAccess($page_variant_id) { - /** @var \Drupal\page_manager\PageVariantInterface $variant */ - $variant = $this->pageVariantStorage->load($page_variant_id); + protected function checkDisplayVariantAccess($display_variant_id) { + /** @var \Drupal\ctools\Entity\DisplayVariantInterface $variant */ + $variant = $this->displayVariantStorage->load($display_variant_id); try { $access = $variant && $variant->access('view'); diff --git a/src/Tests/PageConfigSchemaTest.php b/src/Tests/PageConfigSchemaTest.php index 2af9051..5c9cf50 100644 --- a/src/Tests/PageConfigSchemaTest.php +++ b/src/Tests/PageConfigSchemaTest.php @@ -10,7 +10,7 @@ namespace Drupal\page_manager\Tests; use Drupal\config\Tests\SchemaCheckTestTrait; use Drupal\KernelTests\KernelTestBase; use Drupal\page_manager\Entity\Page; -use Drupal\page_manager\Entity\PageVariant; +use Drupal\ctools\Entity\DisplayVariant; /** * Ensures that page entities have valid config schema. @@ -24,7 +24,7 @@ class PageConfigSchemaTest extends KernelTestBase { /** * {@inheritdoc} */ - public static $modules = ['page_manager', 'block', 'node', 'user']; + public static $modules = ['page_manager', 'panels', 'block', 'node', 'user']; /** * {@inheritdoc} @@ -32,6 +32,7 @@ class PageConfigSchemaTest extends KernelTestBase { protected function setUp() { parent::setUp(); $this->installConfig(['page_manager']); + $this->installConfig(['panels']); } /** @@ -39,7 +40,7 @@ class PageConfigSchemaTest extends KernelTestBase { */ public function testValidPageConfigSchema() { $id = 'node_view'; - /** @var \Drupal\page_manager\PageInterface $page */ + /** @var \Drupal\ctools\Entity\DisplayInterface $page */ $page = Page::load($id); // Add an access condition. @@ -55,21 +56,22 @@ class PageConfigSchemaTest extends KernelTestBase { ]); $page->save(); - $page_variant_id = 'block_page'; + $display_variant_id = 'block_page'; // Add a block variant. - $page_variant = PageVariant::create([ + $display_variant = DisplayVariant::create([ 'variant' => 'block_display', - 'id' => $page_variant_id, + 'id' => $display_variant_id, 'label' => 'Block page', - 'page' => $page->id(), + 'display_entity_id' => $page->id(), + 'display_entity_type' => 'page', ]); - $page_variant->save(); - $page->addVariant($page_variant); + $display_variant->save(); + $page->addVariant($display_variant); /** @var \Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant $variant_plugin */ - $variant_plugin = $page_variant->getVariantPlugin(); + $variant_plugin = $display_variant->getVariantPlugin(); // Add a selection condition. - $page_variant->addSelectionCondition([ + $display_variant->addSelectionCondition([ 'id' => 'node_type', 'bundles' => [ 'page' => 'page', @@ -87,12 +89,12 @@ class PageConfigSchemaTest extends KernelTestBase { 'label_display' => 'visible', 'view_mode' => 'default', ]); - $page_variant->save(); + $display_variant->save(); $page_config = \Drupal::config("page_manager.page.$id"); $this->assertSame($page_config->get('id'), $id); - $variant_config = \Drupal::config("page_manager.page_variant.$page_variant_id"); - $this->assertSame($variant_config->get('id'), $page_variant_id); + $variant_config = \Drupal::config("ctools.display_variant.$display_variant_id"); + $this->assertSame($variant_config->get('id'), $display_variant_id); $this->assertConfigSchema(\Drupal::service('config.typed'), $page_config->getName(), $page_config->get()); $this->assertConfigSchema(\Drupal::service('config.typed'), $variant_config->getName(), $variant_config->get()); diff --git a/src/Tests/PageManagerConfigTranslationTest.php b/src/Tests/PageManagerConfigTranslationTest.php index 0627d38..39b6041 100644 --- a/src/Tests/PageManagerConfigTranslationTest.php +++ b/src/Tests/PageManagerConfigTranslationTest.php @@ -8,7 +8,7 @@ namespace Drupal\page_manager\Tests; use Drupal\language\Entity\ConfigurableLanguage; -use Drupal\page_manager\Entity\PageVariant; +use Drupal\ctools\Entity\DisplayVariant; use Drupal\simpletest\WebTestBase; /** @@ -36,11 +36,12 @@ class PageManagerConfigTranslationTest extends WebTestBase { $this->drupalLogin($this->drupalCreateUser(['administer site configuration', 'translate configuration'])); - PageVariant::create([ + DisplayVariant::create([ 'variant' => 'http_status_code', 'label' => 'HTTP status code', 'id' => 'http_status_code', - 'page' => 'node_view', + 'display_entity_id' => 'node_view', + 'display_entity_type' => 'page', ])->save(); } @@ -50,7 +51,7 @@ class PageManagerConfigTranslationTest extends WebTestBase { public function testTranslation() { $this->drupalGet('admin/config/regional/config-translation'); $this->assertLinkByHref('admin/config/regional/config-translation/page'); - $this->assertLinkByHref('admin/config/regional/config-translation/page_variant'); + $this->assertLinkByHref('admin/config/regional/config-translation/display_variant'); $this->drupalGet('admin/config/regional/config-translation/page'); $this->assertText('Node view'); @@ -58,7 +59,7 @@ class PageManagerConfigTranslationTest extends WebTestBase { $this->clickLink('Add'); $this->assertField('translation[config_names][page_manager.page.node_view][label]'); - $this->drupalGet('admin/config/regional/config-translation/page_variant'); + $this->drupalGet('admin/config/regional/config-translation/display_variant'); $this->assertText('HTTP status code'); $this->clickLink('Translate'); $this->clickLink('Add'); diff --git a/src/Tests/PageManagerTranslationIntegrationTest.php b/src/Tests/PageManagerTranslationIntegrationTest.php index 7ba06dc..4c94baa 100644 --- a/src/Tests/PageManagerTranslationIntegrationTest.php +++ b/src/Tests/PageManagerTranslationIntegrationTest.php @@ -8,7 +8,7 @@ namespace Drupal\page_manager\Tests; use Drupal\content_translation\Tests\ContentTranslationTestBase; -use Drupal\page_manager\Entity\PageVariant; +use Drupal\ctools\Entity\DisplayVariant; /** * Tests that overriding the entity page does not affect content translation. @@ -64,11 +64,12 @@ class PageManagerTranslationIntegrationTest extends ContentTranslationTestBase { $this->assertResponse(200); // Create a new variant. - $http_status_variant = PageVariant::create([ + $http_status_variant = DisplayVariant::create([ 'variant' => 'http_status_code', 'label' => 'HTTP status code', 'id' => 'http_status_code', - 'page' => 'node_view', + 'display_entity_type' => 'page', + 'display_entity_id' => 'node_view', ]); $http_status_variant->getVariantPlugin()->setConfiguration(['status_code' => 200]); $http_status_variant->save(); diff --git a/src/Tests/PageNodeAccessTest.php b/src/Tests/PageNodeAccessTest.php index b5dae3e..8f80028 100644 --- a/src/Tests/PageNodeAccessTest.php +++ b/src/Tests/PageNodeAccessTest.php @@ -8,7 +8,7 @@ namespace Drupal\page_manager\Tests; use Drupal\page_manager\Entity\Page; -use Drupal\page_manager\Entity\PageVariant; +use Drupal\ctools\Entity\DisplayVariant; use Drupal\simpletest\WebTestBase; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; @@ -62,15 +62,16 @@ class PageNodeAccessTest extends WebTestBase { $this->assertTitle($node1->label() . ' | Drupal'); // Add a variant and an access condition. - /** @var \Drupal\page_manager\Entity\PageVariant $page_variant */ - $page_variant = PageVariant::create([ + /** @var \Drupal\ctools\Entity\DisplayVariant $display_variant */ + $display_variant = DisplayVariant::create([ 'variant' => 'block_display', 'id' => 'block_page', 'label' => 'Block page', - 'page' => $this->page->id(), + 'display_entity_id' => $this->page->id(), + 'display_entity_type' => 'page', ]); - $page_variant->getVariantPlugin()->setConfiguration(['page_title' => 'The overridden page']); - $page_variant->save(); + $display_variant->getVariantPlugin()->setConfiguration(['page_title' => 'The overridden page']); + $display_variant->save(); $this->page->addAccessCondition([ 'id' => 'user_role', diff --git a/src/Tests/PageNodeSelectionTest.php b/src/Tests/PageNodeSelectionTest.php index e1816da..93a1220 100644 --- a/src/Tests/PageNodeSelectionTest.php +++ b/src/Tests/PageNodeSelectionTest.php @@ -7,7 +7,7 @@ namespace Drupal\page_manager\Tests; -use Drupal\page_manager\Entity\PageVariant; +use Drupal\ctools\Entity\DisplayVariant; use Drupal\simpletest\WebTestBase; /** @@ -57,11 +57,12 @@ class PageNodeSelectionTest extends WebTestBase { // Create a new variant to always return 404, the node_view page exists by // default. - $http_status_variant = PageVariant::create([ + $http_status_variant = DisplayVariant::create([ 'variant' => 'http_status_code', 'label' => 'HTTP status code', 'id' => 'http_status_code', - 'page' => 'node_view', + 'display_entity_id' => 'node_view', + 'display_entity_type' => 'page', ]); $http_status_variant->getVariantPlugin()->setConfiguration(['status_code' => 404]); $http_status_variant->save(); @@ -76,14 +77,15 @@ class PageNodeSelectionTest extends WebTestBase { $this->assertNoText($node2->label()); // Add a new variant. - /** @var \Drupal\page_manager\PageVariantInterface $block_page_variant */ - $block_page_variant = PageVariant::create([ + /** @var \Drupal\ctools\Entity\DisplayVariantInterface $block_display_variant */ + $block_display_variant = DisplayVariant::create([ 'variant' => 'block_display', 'id' => 'block_page_first', 'label' => 'First', - 'page' => 'node_view', + 'display_entity_id' => 'node_view', + 'display_entity_type' => 'page', ]); - $block_page_plugin = $block_page_variant->getVariantPlugin(); + $block_page_plugin = $block_display_variant->getVariantPlugin(); $block_page_plugin->setConfiguration(['page_title' => '[node:title]']); /** @var \Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant $block_page_plugin */ $block_page_plugin->addBlock([ @@ -96,7 +98,7 @@ class PageNodeSelectionTest extends WebTestBase { 'entity' => 'node', ], ]); - $block_page_variant->addSelectionCondition([ + $block_display_variant->addSelectionCondition([ 'id' => 'node_type', 'bundles' => [ 'article' => 'article', @@ -105,8 +107,8 @@ class PageNodeSelectionTest extends WebTestBase { 'node' => 'node', ], ]); - $block_page_variant->setWeight(-10); - $block_page_variant->save(); + $block_display_variant->setWeight(-10); + $block_display_variant->save(); $this->triggerRouterRebuild(); // The page node will 404, but the article node will display the variant. diff --git a/src/Tests/PagePlaceholderTest.php b/src/Tests/PagePlaceholderTest.php index 9f940e3..e27c6e3 100644 --- a/src/Tests/PagePlaceholderTest.php +++ b/src/Tests/PagePlaceholderTest.php @@ -8,7 +8,7 @@ namespace Drupal\page_manager\Tests; use Drupal\page_manager\Entity\Page; -use Drupal\page_manager\Entity\PageVariant; +use Drupal\ctools\Entity\DisplayVariant; use Drupal\simpletest\WebTestBase; /** @@ -53,11 +53,12 @@ class PagePlaceholderTest extends WebTestBase { $page->save(); // Create a new variant. - $http_status_variant = PageVariant::create([ + $http_status_variant = DisplayVariant::create([ 'variant' => 'http_status_code', 'label' => 'HTTP status code', 'id' => 'http_status_code', - 'page' => 'placeholder', + 'display_entity_id' => 'placeholder', + 'display_entity_type' => 'page', ]); $http_status_variant->getVariantPlugin()->setConfiguration(['status_code' => 200]); $http_status_variant->save(); diff --git a/tests/src/Unit/DisplayVariantTest.php b/tests/src/Unit/DisplayVariantTest.php new file mode 100644 index 0000000..02b2b67 --- /dev/null +++ b/tests/src/Unit/DisplayVariantTest.php @@ -0,0 +1,113 @@ +displayVariant = new DisplayVariant(['id' => 'the_display_variant', 'display_entity_type' => 'page', 'display_entity_id' => 'the_page'], 'display_variant'); + $this->page = $this->prophesize(PageInterface::class); + + $entity_storage = $this->prophesize(EntityStorageInterface::class); + $entity_storage->load('the_page')->willReturn($this->page->reveal()); + + $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class); + $entity_type_manager->getStorage('page')->willReturn($entity_storage); + + $this->contextMapper = $this->prophesize(ContextMapperInterface::class); + + $container = new ContainerBuilder(); + $container->set('entity_type.manager', $entity_type_manager->reveal()); + $container->set('page_manager.context_mapper', $this->contextMapper->reveal()); + \Drupal::setContainer($container); + } + + /** + * @covers ::getContexts + * @dataProvider providerTestGetContexts + */ + public function testGetContexts($static_contexts, $page_contexts, $expected) { + $this->contextMapper->getContextValues([])->willReturn($static_contexts)->shouldBeCalledTimes(1); + $this->page->getContexts()->willReturn($page_contexts)->shouldBeCalledTimes(1); + + $contexts = $this->displayVariant->getContexts(); + $this->assertSame($expected, $contexts); + $contexts = $this->displayVariant->getContexts(); + $this->assertSame($expected, $contexts); + } + + public function providerTestGetContexts() { + $data = []; + $data['empty'] = [ + [], + [], + [], + ]; + $data['additive'] = [ + ['static' => 'static'], + ['page' => 'page'], + ['static' => 'static', 'page' => 'page'], + ]; + $data['conflicting'] = [ + ['foo' => 'static'], + ['foo' => 'page'], + ['foo' => 'page'], + ]; + return $data; + } + + /** + * @covers ::getContexts + * @covers ::removeStaticContext + */ + public function testGetContextsAfterReset() { + $this->contextMapper->getContextValues([])->willReturn([])->shouldBeCalledTimes(2); + $this->page->getContexts()->willReturn([])->shouldBeCalledTimes(2); + + $expected = []; + $contexts = $this->displayVariant->getContexts(); + $this->assertSame($expected, $contexts); + $this->displayVariant->removeStaticContext('anything'); + $contexts = $this->displayVariant->getContexts(); + $this->assertSame($expected, $contexts); + } + +} diff --git a/tests/src/Unit/PageAccessTest.php b/tests/src/Unit/PageAccessTest.php index f3ce5ed..4c9b054 100644 --- a/tests/src/Unit/PageAccessTest.php +++ b/tests/src/Unit/PageAccessTest.php @@ -14,7 +14,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Plugin\Context\ContextHandlerInterface; use Drupal\Core\Session\AccountInterface; -use Drupal\page_manager\Entity\PageAccess; +use Drupal\ctools\Entity\DisplayAccess; use Drupal\page_manager\PageInterface; use Drupal\Tests\UnitTestCase; use Prophecy\Argument; @@ -22,7 +22,7 @@ use Prophecy\Argument; /** * Tests access for Page entities. * - * @coversDefaultClass \Drupal\page_manager\Entity\PageAccess + * @coversDefaultClass \Drupal\ctools\Entity\DisplayAccess * * @group PageManager */ @@ -61,7 +61,7 @@ class PageAccessTest extends UnitTestCase { $module_handler = $this->prophesize(ModuleHandlerInterface::class); $module_handler->invokeAll(Argument::cetera())->willReturn([]); - $this->pageAccess = new PageAccess($this->entityType->reveal(), $this->contextHandler->reveal()); + $this->pageAccess = new DisplayAccess($this->entityType->reveal(), $this->contextHandler->reveal()); $this->pageAccess->setModuleHandler($module_handler->reveal()); $this->cacheContextsManager = $this->prophesize(CacheContextsManager::class); diff --git a/tests/src/Unit/PageManagerRoutesTest.php b/tests/src/Unit/PageManagerRoutesTest.php index fe6f005..231a78e 100644 --- a/tests/src/Unit/PageManagerRoutesTest.php +++ b/tests/src/Unit/PageManagerRoutesTest.php @@ -12,7 +12,7 @@ use Drupal\Core\Config\Entity\ConfigEntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Routing\RouteBuildEvent; use Drupal\page_manager\PageInterface; -use Drupal\page_manager\PageVariantInterface; +use Drupal\ctools\Entity\DisplayVariantInterface; use Drupal\page_manager\Routing\PageManagerRoutes; use Drupal\Tests\UnitTestCase; use Symfony\Component\Routing\Route; @@ -88,7 +88,7 @@ class PageManagerRoutesTest extends UnitTestCase { ->shouldBeCalled(); $page1->id()->willReturn('page1'); - $variant1 = $this->prophesize(PageVariantInterface::class); + $variant1 = $this->prophesize(DisplayVariantInterface::class); $variant1->getWeight()->willReturn(0); $page1->getVariants() ->willReturn(['variant1' => $variant1->reveal()]); @@ -129,11 +129,11 @@ class PageManagerRoutesTest extends UnitTestCase { $this->assertSame(1, $collection->count()); $route = $collection->get('page_manager.page_view_page1'); $expected_defaults = [ - '_entity_view' => 'page_manager_page_variant', + '_entity_view' => 'display_variant', '_title' => 'Page label', - 'page_manager_page_variant' => 'variant1', + 'page_manager_display_variant' => 'variant1', 'page_manager_page' => 'page1', - 'page_manager_page_variant_weight' => 0, + 'page_manager_display_variant_weight' => 0, 'base_route_name' => 'page_manager.page_view_page1', ]; $expected_requirements = [ @@ -142,8 +142,8 @@ class PageManagerRoutesTest extends UnitTestCase { $expected_options = [ 'compiler_class' => 'Symfony\Component\Routing\RouteCompiler', 'parameters' => [ - 'page_manager_page_variant' => [ - 'type' => 'entity:page_variant', + 'page_manager_display_variant' => [ + 'type' => 'entity:display_variant', ], 'page_manager_page' => [ 'type' => 'entity:page', @@ -176,7 +176,7 @@ class PageManagerRoutesTest extends UnitTestCase { $page->getParameters() ->willReturn([]) ->shouldBeCalled(); - $variant1 = $this->prophesize(PageVariantInterface::class); + $variant1 = $this->prophesize(DisplayVariantInterface::class); $variant1->getWeight()->willReturn(0); $page->getVariants() ->willReturn(['variant1' => $variant1->reveal()]); @@ -202,11 +202,11 @@ class PageManagerRoutesTest extends UnitTestCase { $route = $collection->get($route_name); $expected_defaults = [ - '_entity_view' => 'page_manager_page_variant', + '_entity_view' => 'display_variant', '_title' => NULL, - 'page_manager_page_variant' => 'variant1', + 'page_manager_display_variant' => 'variant1', 'page_manager_page' => 'page1', - 'page_manager_page_variant_weight' => 0, + 'page_manager_display_variant_weight' => 0, 'base_route_name' => $route_name, ]; $expected_requirements = $requirements + ['_page_access' => 'page_manager_page.view']; @@ -214,8 +214,8 @@ class PageManagerRoutesTest extends UnitTestCase { 'compiler_class' => 'Symfony\Component\Routing\RouteCompiler', 'parameters' => [ 'foo' => ['type' => 'bar'], - 'page_manager_page_variant' => [ - 'type' => 'entity:page_variant', + 'page_manager_display_variant' => [ + 'type' => 'entity:display_variant', ], 'page_manager_page' => [ 'type' => 'entity:page', @@ -241,8 +241,8 @@ class PageManagerRoutesTest extends UnitTestCase { * @covers ::alterRoutes */ public function testAlterRoutesMultipleVariantsDifferentRequirements() { - $variant1 = $this->prophesize(PageVariantInterface::class); - $variant2 = $this->prophesize(PageVariantInterface::class); + $variant1 = $this->prophesize(DisplayVariantInterface::class); + $variant2 = $this->prophesize(DisplayVariantInterface::class); $variant1->getWeight()->willReturn(0); $page1 = $this->prophesize(PageInterface::class); @@ -275,11 +275,11 @@ class PageManagerRoutesTest extends UnitTestCase { 'test_route' => [ 'path' => '/test_route1', 'defaults' => [ - '_entity_view' => 'page_manager_page_variant', + '_entity_view' => 'display_variant', '_title' => 'Page 1', - 'page_manager_page_variant' => 'variant1', + 'page_manager_display_variant' => 'variant1', 'page_manager_page' => 'page1', - 'page_manager_page_variant_weight' => 0, + 'page_manager_display_variant_weight' => 0, 'base_route_name' => 'test_route', ], 'requirements' => [ @@ -289,8 +289,8 @@ class PageManagerRoutesTest extends UnitTestCase { 'options' => [ 'compiler_class' => 'Symfony\Component\Routing\RouteCompiler', 'parameters' => [ - 'page_manager_page_variant' => [ - 'type' => 'entity:page_variant', + 'page_manager_display_variant' => [ + 'type' => 'entity:display_variant', ], 'page_manager_page' => [ 'type' => 'entity:page', @@ -302,11 +302,11 @@ class PageManagerRoutesTest extends UnitTestCase { 'page_manager.page_view_page2' => [ 'path' => '/test_route2', 'defaults' => [ - '_entity_view' => 'page_manager_page_variant', + '_entity_view' => 'display_variant', '_title' => 'Page 2', - 'page_manager_page_variant' => 'variant2', + 'page_manager_display_variant' => 'variant2', 'page_manager_page' => 'page2', - 'page_manager_page_variant_weight' => 0, + 'page_manager_display_variant_weight' => 0, 'base_route_name' => 'page_manager.page_view_page2', ], 'requirements' => [ @@ -315,8 +315,8 @@ class PageManagerRoutesTest extends UnitTestCase { 'options' => [ 'compiler_class' => 'Symfony\Component\Routing\RouteCompiler', 'parameters' => [ - 'page_manager_page_variant' => [ - 'type' => 'entity:page_variant', + 'page_manager_display_variant' => [ + 'type' => 'entity:display_variant', ], 'page_manager_page' => [ 'type' => 'entity:page', @@ -351,7 +351,7 @@ class PageManagerRoutesTest extends UnitTestCase { $page->usesAdminTheme()->willReturn(FALSE); $page->getParameters()->willReturn(['foo' => ['machine_name' => 'foo', 'type' => 'integer', 'label' => 'Foo'], 'test_route' => ['machine_name' => 'test_route', 'type' => '', 'label' => '']]); - $variant1 = $this->prophesize(PageVariantInterface::class); + $variant1 = $this->prophesize(DisplayVariantInterface::class); $variant1->getWeight()->willReturn(0); $page->getVariants()->willReturn(['variant1' => $variant1->reveal()]); @@ -363,11 +363,11 @@ class PageManagerRoutesTest extends UnitTestCase { $this->routeSubscriber->onAlterRoutes($route_event); $expected_defaults = [ - '_entity_view' => 'page_manager_page_variant', + '_entity_view' => 'display_variant', '_title' => NULL, - 'page_manager_page_variant' => 'variant1', + 'page_manager_display_variant' => 'variant1', 'page_manager_page' => 'page1', - 'page_manager_page_variant_weight' => 0, + 'page_manager_display_variant_weight' => 0, 'base_route_name' => $route_name, ]; $expected_requirements = $requirements + ['_page_access' => 'page_manager_page.view']; @@ -378,8 +378,8 @@ class PageManagerRoutesTest extends UnitTestCase { 'bar' => 'bar', 'type' => 'integer', ], - 'page_manager_page_variant' => [ - 'type' => 'entity:page_variant', + 'page_manager_display_variant' => [ + 'type' => 'entity:display_variant', ], 'page_manager_page' => [ 'type' => 'entity:page', diff --git a/tests/src/Unit/PageTest.php b/tests/src/Unit/PageTest.php index 55713fc..eefdc35 100644 --- a/tests/src/Unit/PageTest.php +++ b/tests/src/Unit/PageTest.php @@ -15,7 +15,8 @@ use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\page_manager\Entity\Page; use Drupal\page_manager\Event\PageManagerContextEvent; use Drupal\page_manager\Event\PageManagerEvents; -use Drupal\page_manager\PageVariantInterface; +use Drupal\ctools\Entity\DisplayVariantInterface; +use Drupal\ctools\Entity\DisplayInterface; use Drupal\Tests\UnitTestCase; use Prophecy\Argument; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -47,21 +48,23 @@ class PageTest extends UnitTestCase { * @covers ::getVariants */ public function testGetVariants() { - $variant1 = $this->prophesize(PageVariantInterface::class); + $variant1 = $this->prophesize(DisplayVariantInterface::class); $variant1->id()->willReturn('variant1'); $variant1->getWeight()->willReturn(0); - $variant2 = $this->prophesize(PageVariantInterface::class); + $variant1->setDisplayEntity($this->page)->willReturn($this->page); + $variant2 = $this->prophesize(DisplayVariantInterface::class); $variant2->id()->willReturn('variant2'); $variant2->getWeight()->willReturn(-10); + $variant2->setDisplayEntity($this->page)->willReturn($this->page); $entity_storage = $this->prophesize(EntityStorageInterface::class); $entity_storage - ->loadByProperties(['page' => 'the_page']) + ->loadByProperties(['display_entity_id' => 'the_page']) ->willReturn(['variant1' => $variant1->reveal(), 'variant2' => $variant2->reveal()]) ->shouldBeCalledTimes(1); $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class); - $entity_type_manager->getStorage('page_variant')->willReturn($entity_storage); + $entity_type_manager->getStorage('display_variant')->willReturn($entity_storage); $container = new ContainerBuilder(); $container->set('entity_type.manager', $entity_type_manager->reveal()); diff --git a/tests/src/Unit/PageVariantTest.php b/tests/src/Unit/PageVariantTest.php deleted file mode 100644 index cfe8ead..0000000 --- a/tests/src/Unit/PageVariantTest.php +++ /dev/null @@ -1,113 +0,0 @@ -pageVariant = new PageVariant(['id' => 'the_page_variant', 'page' => 'the_page'], 'page_variant'); - $this->page = $this->prophesize(PageInterface::class); - - $entity_storage = $this->prophesize(EntityStorageInterface::class); - $entity_storage->load('the_page')->willReturn($this->page->reveal()); - - $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class); - $entity_type_manager->getStorage('page')->willReturn($entity_storage); - - $this->contextMapper = $this->prophesize(ContextMapperInterface::class); - - $container = new ContainerBuilder(); - $container->set('entity_type.manager', $entity_type_manager->reveal()); - $container->set('page_manager.context_mapper', $this->contextMapper->reveal()); - \Drupal::setContainer($container); - } - - /** - * @covers ::getContexts - * @dataProvider providerTestGetContexts - */ - public function testGetContexts($static_contexts, $page_contexts, $expected) { - $this->contextMapper->getContextValues([])->willReturn($static_contexts)->shouldBeCalledTimes(1); - $this->page->getContexts()->willReturn($page_contexts)->shouldBeCalledTimes(1); - - $contexts = $this->pageVariant->getContexts(); - $this->assertSame($expected, $contexts); - $contexts = $this->pageVariant->getContexts(); - $this->assertSame($expected, $contexts); - } - - public function providerTestGetContexts() { - $data = []; - $data['empty'] = [ - [], - [], - [], - ]; - $data['additive'] = [ - ['static' => 'static'], - ['page' => 'page'], - ['static' => 'static', 'page' => 'page'], - ]; - $data['conflicting'] = [ - ['foo' => 'static'], - ['foo' => 'page'], - ['foo' => 'page'], - ]; - return $data; - } - - /** - * @covers ::getContexts - * @covers ::removeStaticContext - */ - public function testGetContextsAfterReset() { - $this->contextMapper->getContextValues([])->willReturn([])->shouldBeCalledTimes(2); - $this->page->getContexts()->willReturn([])->shouldBeCalledTimes(2); - - $expected = []; - $contexts = $this->pageVariant->getContexts(); - $this->assertSame($expected, $contexts); - $this->pageVariant->removeStaticContext('anything'); - $contexts = $this->pageVariant->getContexts(); - $this->assertSame($expected, $contexts); - } - -} diff --git a/tests/src/Unit/VariantRouteFilterTest.php b/tests/src/Unit/VariantRouteFilterTest.php index dbcf0f3..8334ec0 100644 --- a/tests/src/Unit/VariantRouteFilterTest.php +++ b/tests/src/Unit/VariantRouteFilterTest.php @@ -11,7 +11,7 @@ use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Core\Config\Entity\ConfigEntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Path\CurrentPathStack; -use Drupal\page_manager\PageVariantInterface; +use Drupal\ctools\Entity\DisplayVariantInterface; use Drupal\page_manager\Routing\VariantRouteFilter; use Drupal\Tests\UnitTestCase; use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface; @@ -37,7 +37,7 @@ class VariantRouteFilterTest extends UnitTestCase { * * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface|\Prophecy\Prophecy\ProphecyInterface */ - protected $pageVariantStorage; + protected $displayVariantStorage; /** * The mocked current path stack. @@ -57,11 +57,11 @@ class VariantRouteFilterTest extends UnitTestCase { * {@inheritdoc} */ protected function setUp() { - $this->pageVariantStorage = $this->prophesize(ConfigEntityStorageInterface::class); + $this->displayVariantStorage = $this->prophesize(ConfigEntityStorageInterface::class); $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class); - $this->entityTypeManager->getStorage('page_variant') - ->willReturn($this->pageVariantStorage); + $this->entityTypeManager->getStorage('display_variant') + ->willReturn($this->displayVariantStorage); $this->currentPath = $this->prophesize(CurrentPathStack::class); $this->routeFilter = new VariantRouteFilter($this->entityTypeManager->reveal(), $this->currentPath->reveal()); @@ -81,7 +81,7 @@ class VariantRouteFilterTest extends UnitTestCase { public function providerTestApplies() { $data = []; $data['no_options'] = [[], FALSE]; - $data['with_options'] = [['parameters' => ['page_manager_page_variant' => TRUE]], TRUE]; + $data['with_options'] = [['parameters' => ['page_manager_display_variant' => TRUE]], TRUE]; return $data; } @@ -102,20 +102,20 @@ class VariantRouteFilterTest extends UnitTestCase { /** * @covers ::filter - * @covers ::checkPageVariantAccess + * @covers ::checkDisplayVariantAccess */ public function testFilterContextException() { $route_collection = new RouteCollection(); $request = new Request(); - $route = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'a_variant']); + $route = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'a_variant']); $route_collection->add('a_route', $route); - $page_variant = $this->prophesize(PageVariantInterface::class); - $page_variant->access('view')->willThrow(new ContextException()); + $display_variant = $this->prophesize(DisplayVariantInterface::class); + $display_variant->access('view')->willThrow(new ContextException()); $this->currentPath->getPath($request)->willReturn(''); - $this->pageVariantStorage->load('a_variant')->willReturn($page_variant->reveal()); + $this->displayVariantStorage->load('a_variant')->willReturn($display_variant->reveal()); $result = $this->routeFilter->filter($route_collection, $request); $expected = []; @@ -143,20 +143,20 @@ class VariantRouteFilterTest extends UnitTestCase { /** * @covers ::filter - * @covers ::checkPageVariantAccess + * @covers ::checkDisplayVariantAccess */ public function testFilterDeniedAccess() { $route_collection = new RouteCollection(); $request = new Request(); - $route = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'a_variant']); + $route = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'a_variant']); $route_collection->add('a_route', $route); - $page_variant = $this->prophesize(PageVariantInterface::class); - $page_variant->access('view')->willReturn(FALSE); + $display_variant = $this->prophesize(DisplayVariantInterface::class); + $display_variant->access('view')->willReturn(FALSE); $this->currentPath->getPath($request)->willReturn(''); - $this->pageVariantStorage->load('a_variant')->willReturn($page_variant->reveal()); + $this->displayVariantStorage->load('a_variant')->willReturn($display_variant->reveal()); $result = $this->routeFilter->filter($route_collection, $request); $expected = []; @@ -166,26 +166,26 @@ class VariantRouteFilterTest extends UnitTestCase { /** * @covers ::filter - * @covers ::checkPageVariantAccess + * @covers ::checkDisplayVariantAccess */ public function testFilterAllowedAccess() { $route_collection = new RouteCollection(); $request = new Request(); - $route = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'a_variant']); + $route = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'a_variant']); $route_collection->add('a_route', $route); - $page_variant = $this->prophesize(PageVariantInterface::class); - $page_variant->access('view')->willReturn(TRUE); + $display_variant = $this->prophesize(DisplayVariantInterface::class); + $display_variant->access('view')->willReturn(TRUE); $this->currentPath->getPath($request)->willReturn(''); - $this->pageVariantStorage->load('a_variant')->willReturn($page_variant->reveal()); + $this->displayVariantStorage->load('a_variant')->willReturn($display_variant->reveal()); $result = $this->routeFilter->filter($route_collection, $request); $expected = ['a_route' => $route]; $this->assertSame($expected, $result->all()); $expected_attributes = [ - 'page_manager_page_variant' => 'a_variant', + 'page_manager_display_variant' => 'a_variant', '_route_object' => $route, '_route' => 'a_route', ]; @@ -200,24 +200,24 @@ class VariantRouteFilterTest extends UnitTestCase { $request = new Request(); // Add route2 first to ensure that the routes are sorted by weight. - $route1 = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'variant_1', 'page_manager_page_variant_weight' => 1]); - $route2 = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'variant_2', 'page_manager_page_variant_weight' => 2]); + $route1 = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'variant_1', 'page_manager_display_variant_weight' => 1]); + $route2 = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'variant_2', 'page_manager_display_variant_weight' => 2]); $route_collection->add('route_2', $route2); $route_collection->add('route_1', $route1); - $page_variant = $this->prophesize(PageVariantInterface::class); - $page_variant->access('view')->willReturn(TRUE); + $display_variant = $this->prophesize(DisplayVariantInterface::class); + $display_variant->access('view')->willReturn(TRUE); $this->currentPath->getPath($request)->willReturn(''); - $this->pageVariantStorage->load('variant_1')->willReturn($page_variant->reveal()); - $this->pageVariantStorage->load('variant_2')->shouldNotBeCalled(); + $this->displayVariantStorage->load('variant_1')->willReturn($display_variant->reveal()); + $this->displayVariantStorage->load('variant_2')->shouldNotBeCalled(); $result = $this->routeFilter->filter($route_collection, $request); $expected = ['route_1' => $route1]; $this->assertSame($expected, $result->all()); $expected_attributes = [ - 'page_manager_page_variant' => 'variant_1', - 'page_manager_page_variant_weight' => 1, + 'page_manager_display_variant' => 'variant_1', + 'page_manager_display_variant_weight' => 1, '_route_object' => $route1, '_route' => 'route_1', ]; @@ -232,26 +232,26 @@ class VariantRouteFilterTest extends UnitTestCase { $request = new Request(); // Add route2 first to ensure that the routes are sorted by weight. - $route1 = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'variant_1', 'page_manager_page_variant_weight' => 1]); - $route2 = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'variant_2', 'page_manager_page_variant_weight' => 2]); + $route1 = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'variant_1', 'page_manager_display_variant_weight' => 1]); + $route2 = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'variant_2', 'page_manager_display_variant_weight' => 2]); $route_collection->add('route_2', $route2); $route_collection->add('route_1', $route1); - $page_variant1 = $this->prophesize(PageVariantInterface::class); - $page_variant1->access('view')->willReturn(FALSE); - $page_variant2 = $this->prophesize(PageVariantInterface::class); - $page_variant2->access('view')->willReturn(TRUE); + $display_variant1 = $this->prophesize(DisplayVariantInterface::class); + $display_variant1->access('view')->willReturn(FALSE); + $display_variant2 = $this->prophesize(DisplayVariantInterface::class); + $display_variant2->access('view')->willReturn(TRUE); $this->currentPath->getPath($request)->willReturn(''); - $this->pageVariantStorage->load('variant_1')->willReturn($page_variant1->reveal())->shouldBeCalled(); - $this->pageVariantStorage->load('variant_2')->willReturn($page_variant2->reveal())->shouldBeCalled(); + $this->displayVariantStorage->load('variant_1')->willReturn($display_variant1->reveal())->shouldBeCalled(); + $this->displayVariantStorage->load('variant_2')->willReturn($display_variant2->reveal())->shouldBeCalled(); $result = $this->routeFilter->filter($route_collection, $request); $expected = ['route_2' => $route2]; $this->assertSame($expected, $result->all()); $expected_attributes = [ - 'page_manager_page_variant' => 'variant_2', - 'page_manager_page_variant_weight' => 2, + 'page_manager_display_variant' => 'variant_2', + 'page_manager_display_variant_weight' => 2, '_route_object' => $route2, '_route' => 'route_2', ]; @@ -271,28 +271,28 @@ class VariantRouteFilterTest extends UnitTestCase { // Add routes in different order to test sorting. $route1 = new Route('/path/with/{slug}'); - $route2 = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'variant1', 'page_manager_page_variant_weight' => 1]); - $route3 = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'variant2', 'page_manager_page_variant_weight' => 2]); + $route2 = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'variant1', 'page_manager_display_variant_weight' => 1]); + $route3 = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'variant2', 'page_manager_display_variant_weight' => 2]); $route4 = new Route('/path/with/{slug}'); $route_collection->add('route_3', $route3); $route_collection->add('route_2', $route2); $route_collection->add('route_1', $route1); $route_collection->add('route_4', $route4); - $page_variant1 = $this->prophesize(PageVariantInterface::class); - $page_variant1->access('view')->willReturn(TRUE); - $page_variant2 = $this->prophesize(PageVariantInterface::class); - $page_variant2->access('view')->willReturn(FALSE); + $display_variant1 = $this->prophesize(DisplayVariantInterface::class); + $display_variant1->access('view')->willReturn(TRUE); + $display_variant2 = $this->prophesize(DisplayVariantInterface::class); + $display_variant2->access('view')->willReturn(FALSE); $this->currentPath->getPath($request)->willReturn(''); - $this->pageVariantStorage->load('variant1')->willReturn($page_variant1->reveal())->shouldBeCalled(); + $this->displayVariantStorage->load('variant1')->willReturn($display_variant1->reveal())->shouldBeCalled(); $result = $this->routeFilter->filter($route_collection, $request); $expected = ['route_2' => $route2, 'route_1' => $route1, 'route_4' => $route4]; $this->assertSame($expected, $result->all()); $expected_attributes = [ - 'page_manager_page_variant' => 'variant1', - 'page_manager_page_variant_weight' => 1, + 'page_manager_display_variant' => 'variant1', + 'page_manager_display_variant_weight' => 1, '_route_object' => $route2, '_route' => 'route_2', ]; @@ -306,21 +306,21 @@ class VariantRouteFilterTest extends UnitTestCase { $route_collection = new RouteCollection(); $request = new Request([], [], ['foo' => 'bar', 'slug' => 2]); - $route = new Route('/path/with/{slug}', ['page_manager_page_variant' => 'a_variant']); + $route = new Route('/path/with/{slug}', ['page_manager_display_variant' => 'a_variant']); $route_collection->add('a_route', $route); - $page_variant = $this->prophesize(PageVariantInterface::class); - $page_variant->access('view')->willReturn(TRUE); + $display_variant = $this->prophesize(DisplayVariantInterface::class); + $display_variant->access('view')->willReturn(TRUE); $this->currentPath->getPath($request)->willReturn('/path/with/1'); - $this->pageVariantStorage->load('a_variant')->willReturn($page_variant->reveal()); + $this->displayVariantStorage->load('a_variant')->willReturn($display_variant->reveal()); $route_enhancer = $this->prophesize(RouteEnhancerInterface::class); $this->routeFilter->addRouteEnhancer($route_enhancer->reveal()); $result_enhance_attributes = $expected_enhance_attributes = [ 'foo' => 'bar', 'slug' => '1', - 'page_manager_page_variant' => 'a_variant', + 'page_manager_display_variant' => 'a_variant', '_route_object' => $route, '_route' => 'a_route', ]; @@ -333,7 +333,7 @@ class VariantRouteFilterTest extends UnitTestCase { $expected_attributes = [ 'foo' => 'bar', 'slug' => 'slug 1', - 'page_manager_page_variant' => 'a_variant', + 'page_manager_display_variant' => 'a_variant', '_route_object' => $route, '_route' => 'a_route', ];