From 78864925ad93a9ebbba7c820b5621bee625948a7 Mon Sep 17 00:00:00 2001 From: upchuk Date: Mon, 20 Jun 2022 10:36:47 +0200 Subject: [PATCH] EWPP-2371: Refactoring the list builder and execution manager to work with other config. --- src/ListBuilder.php | 40 ++++++++++--------- src/ListBuilderInterface.php | 26 ++++++------ src/ListExecutionManager.php | 6 ++- src/ListPageConfiguration.php | 30 ++++++++++++++ .../ExtraField/Display/ListPageFilters.php | 4 +- .../ExtraField/Display/ListPagePagerInfo.php | 4 +- .../ExtraField/Display/ListPageResults.php | 4 +- .../Display/ListPageSelectedFilters.php | 4 +- 8 files changed, 82 insertions(+), 36 deletions(-) diff --git a/src/ListBuilder.php b/src/ListBuilder.php index 2c802cec..0be342a5 100644 --- a/src/ListBuilder.php +++ b/src/ListBuilder.php @@ -166,21 +166,20 @@ public function __construct(ListExecutionManagerInterface $listExecutionManager, /** * {@inheritdoc} */ - public function buildList(ContentEntityInterface $entity): array { + public function buildList(ListPageConfiguration $configuration): array { $build = [ 'list' => [], ]; $cache = new CacheableMetadata(); - $cache->addCacheTags($entity->getEntityType()->getListCacheTags()); $cache->addCacheContexts(['url.query_args']); - $configuration = ListPageConfiguration::fromEntity($entity); $sort = $this->getSortFromUrl($configuration); // The sort could potentially be overridden from the URL. if ($sort) { $configuration->setSort($sort); } + $list_execution = $this->listExecutionManager->executeList($configuration); if (empty($list_execution)) { $cache->applyTo($build); @@ -194,16 +193,21 @@ public function buildList(ContentEntityInterface $entity): array { } $query = $list_execution->getQuery(); + $cache->addCacheableDependency($query); $result = $list_execution->getResults(); $configuration = $list_execution->getConfiguration(); - // Determine the view mode to render with and the sorting. + // Determine the view mode to render with and the sorting. We default to + // the view mode the entity view builder defaults to. + $view_mode = 'full'; $bundle_entity_type = $this->entityTypeManager->getDefinition($configuration->getEntityType())->getBundleEntityType(); - $storage = $this->entityTypeManager->getStorage($bundle_entity_type); - $bundle = $storage->load($configuration->getBundle()); - $view_mode = $bundle->getThirdPartySetting('oe_list_pages', 'default_view_mode', 'teaser'); - $cache->addCacheableDependency($query); - $cache->addCacheableDependency($bundle); + if ($bundle_entity_type) { + $storage = $this->entityTypeManager->getStorage($bundle_entity_type); + $bundle = $storage->load($configuration->getBundle()); + $view_mode = $bundle->getThirdPartySetting('oe_list_pages', 'default_view_mode', 'teaser'); + $cache->addCacheableDependency($bundle); + } + $cache->addCacheTags(['search_api_list:' . $query->getIndex()->id()]); $this->pager->createPager($result->getResultCount(), $query->getOption('limit')); @@ -240,10 +244,9 @@ public function buildList(ContentEntityInterface $entity): array { /** * {@inheritdoc} */ - public function buildFiltersForm(ContentEntityInterface $entity): array { + public function buildFiltersForm(ListPageConfiguration $configuration): array { $build = $ignored_filters = $exposed_filters = []; - $configuration = ListPageConfiguration::fromEntity($entity); $list_execution = $this->listExecutionManager->executeList($configuration); $list_source = $list_execution->getListSource(); @@ -259,9 +262,12 @@ public function buildFiltersForm(ContentEntityInterface $entity): array { } else { $bundle_entity_type = $this->entityTypeManager->getDefinition($configuration->getEntityType())->getBundleEntityType(); - $storage = $this->entityTypeManager->getStorage($bundle_entity_type); - $bundle = $storage->load($configuration->getBundle()); - $exposed_filters = $bundle->getThirdPartySetting('oe_list_pages', 'default_exposed_filters', []); + if ($bundle_entity_type) { + $storage = $this->entityTypeManager->getStorage($bundle_entity_type); + $bundle = $storage->load($configuration->getBundle()); + $exposed_filters = $bundle->getThirdPartySetting('oe_list_pages', 'default_exposed_filters', []); + } + } // By default ignore all filters. @@ -285,12 +291,11 @@ public function buildFiltersForm(ContentEntityInterface $entity): array { * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function buildSelectedFilters(ContentEntityInterface $entity): array { + public function buildSelectedFilters(ListPageConfiguration $configuration): array { $build = []; $cache = new CacheableMetadata(); $cache->addCacheContexts(['url']); - $configuration = ListPageConfiguration::fromEntity($entity); $active_filters = []; // First, determine all the active values for filters that have a "default @@ -397,12 +402,11 @@ public function buildSelectedFilters(ContentEntityInterface $entity): array { * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function buildPagerInfo(ContentEntityInterface $entity): array { + public function buildPagerInfo(ListPageConfiguration $configuration): array { $cache = new CacheableMetadata(); $cache->addCacheContexts(['url']); $build = []; - $configuration = ListPageConfiguration::fromEntity($entity); $list_execution = $this->listExecutionManager->executeList($configuration); $results = $list_execution->getResults(); if (!$results->getResultCount()) { diff --git a/src/ListBuilderInterface.php b/src/ListBuilderInterface.php index ecae660c..369e848c 100644 --- a/src/ListBuilderInterface.php +++ b/src/ListBuilderInterface.php @@ -17,24 +17,24 @@ interface ListBuilderInterface { /** * Builds the list content to be rendered. * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The content entity. + * @param \Drupal\oe_list_pages\ListPageConfiguration $configuration + * The list page configuration. * * @return array * The list render array. */ - public function buildList(ContentEntityInterface $entity): array; + public function buildList(ListPageConfiguration $configuration): array; /** * Builds the list page filters. * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The content entity page. + * @param \Drupal\oe_list_pages\ListPageConfiguration $configuration + * The list page configuration. * * @return array * The filters render array. */ - public function buildFiltersForm(ContentEntityInterface $entity): array; + public function buildFiltersForm(ListPageConfiguration $configuration): array; /** * Builds the list page selected filters. @@ -42,30 +42,30 @@ public function buildFiltersForm(ContentEntityInterface $entity): array; * These are the filters that have been selected in the form and are currently * filtering the list. * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The content entity page. + * @param \Drupal\oe_list_pages\ListPageConfiguration $configuration + * The list page configuration. * * @return array * The filters render array. */ - public function buildSelectedFilters(ContentEntityInterface $entity): array; + public function buildSelectedFilters(ListPageConfiguration $configuration): array; /** * Builds a descriptive pager information message. * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The content entity page. + * @param \Drupal\oe_list_pages\ListPageConfiguration $configuration + * The list page configuration. * * @return array * The pager info render array. */ - public function buildPagerInfo(ContentEntityInterface $entity): array; + public function buildPagerInfo(ListPageConfiguration $configuration): array; /** * Builds a link to the RSS representation of the list page. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The content entity page. + * The content entity. * * @return array * The RSS link render array. diff --git a/src/ListExecutionManager.php b/src/ListExecutionManager.php index 2da1ebe9..29e2f921 100644 --- a/src/ListExecutionManager.php +++ b/src/ListExecutionManager.php @@ -77,7 +77,7 @@ public function executeList(ListPageConfiguration $configuration): ?ListExecutio // The number of items to show on a page. $limit = $configuration->getLimit() ?? 10; - $list_source = $this->listSourceFactory->get($configuration->getEntityType(), $configuration->getBundle()); + $list_source = $configuration->getListSource() ?? $this->listSourceFactory->get($configuration->getEntityType(), $configuration->getBundle()); if (!$list_source) { $this->executedLists[$configuration->getId()] = NULL; return NULL; @@ -129,6 +129,10 @@ public function executeList(ListPageConfiguration $configuration): ?ListExecutio */ protected function getBundleDefaultSort(ListSourceInterface $list_source): array { $bundle_entity_type = $this->entityTypeManager->getDefinition($list_source->getEntityType())->getBundleEntityType(); + if (!$bundle_entity_type) { + // We can have entity types that have no bundles. + return []; + } $storage = $this->entityTypeManager->getStorage($bundle_entity_type); $bundle = $storage->load($list_source->getBundle()); return $bundle->getThirdPartySetting('oe_list_pages', 'default_sort', []); diff --git a/src/ListPageConfiguration.php b/src/ListPageConfiguration.php index 55e31257..5d8c8e46 100644 --- a/src/ListPageConfiguration.php +++ b/src/ListPageConfiguration.php @@ -98,6 +98,16 @@ class ListPageConfiguration { */ protected $extra = []; + /** + * An optional list source with which this configuration works. + * + * This is used in special cases in which the default list source factory + * is not used and a different list source is therefore needed. + * + * @var \Drupal\oe_list_pages\ListSourceInterface|null + */ + protected $listSource = NULL; + /** * ListPageConfiguration constructor. * @@ -408,4 +418,24 @@ public function toArray(): array { ]; } + /** + * Returns the optional list source. + * + * @return \Drupal\oe_list_pages\ListSourceInterface|null + * The list source if set. + */ + public function getListSource(): ?ListSourceInterface { + return $this->listSource; + } + + /** + * Sets the optional list source. + * + * @param \Drupal\oe_list_pages\ListSourceInterface $list_source + * The list source. + */ + public function setListSource(ListSourceInterface $list_source): void { + $this->listSource = $list_source; + } + } diff --git a/src/Plugin/ExtraField/Display/ListPageFilters.php b/src/Plugin/ExtraField/Display/ListPageFilters.php index 7bdc0da6..5e6cf644 100644 --- a/src/Plugin/ExtraField/Display/ListPageFilters.php +++ b/src/Plugin/ExtraField/Display/ListPageFilters.php @@ -5,6 +5,7 @@ namespace Drupal\oe_list_pages\Plugin\ExtraField\Display; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\oe_list_pages\ListPageConfiguration; /** * Extra field displaying the list page filters. @@ -28,7 +29,8 @@ public function getLabel() { * {@inheritdoc} */ public function view(ContentEntityInterface $entity) { - $form = $this->listBuilder->buildFiltersForm($entity); + $configuration = ListPageConfiguration::fromEntity($entity); + $form = $this->listBuilder->buildFiltersForm($configuration); if (!$form || !isset($form['facets'])) { // Return just the cache so we have an empty rendered value in case there // are no facets in the form. diff --git a/src/Plugin/ExtraField/Display/ListPagePagerInfo.php b/src/Plugin/ExtraField/Display/ListPagePagerInfo.php index 2fcd0a50..10379ee8 100644 --- a/src/Plugin/ExtraField/Display/ListPagePagerInfo.php +++ b/src/Plugin/ExtraField/Display/ListPagePagerInfo.php @@ -5,6 +5,7 @@ namespace Drupal\oe_list_pages\Plugin\ExtraField\Display; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\oe_list_pages\ListPageConfiguration; /** * Extra field displaying the list page pager. @@ -28,7 +29,8 @@ public function getLabel() { * {@inheritdoc} */ public function viewElements(ContentEntityInterface $entity) { - return [$this->listBuilder->buildPagerInfo($entity)]; + $configuration = ListPageConfiguration::fromEntity($entity); + return [$this->listBuilder->buildPagerInfo($configuration)]; } } diff --git a/src/Plugin/ExtraField/Display/ListPageResults.php b/src/Plugin/ExtraField/Display/ListPageResults.php index 23463ee3..a47e5ae3 100644 --- a/src/Plugin/ExtraField/Display/ListPageResults.php +++ b/src/Plugin/ExtraField/Display/ListPageResults.php @@ -5,6 +5,7 @@ namespace Drupal\oe_list_pages\Plugin\ExtraField\Display; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\oe_list_pages\ListPageConfiguration; /** * Extra field displaying the list page results. @@ -28,7 +29,8 @@ public function getLabel() { * {@inheritdoc} */ public function viewElements(ContentEntityInterface $entity) { - return [$this->listBuilder->buildList($entity)]; + $configuration = ListPageConfiguration::fromEntity($entity); + return [$this->listBuilder->buildList($configuration)]; } } diff --git a/src/Plugin/ExtraField/Display/ListPageSelectedFilters.php b/src/Plugin/ExtraField/Display/ListPageSelectedFilters.php index 6615f7f1..ccffbe58 100644 --- a/src/Plugin/ExtraField/Display/ListPageSelectedFilters.php +++ b/src/Plugin/ExtraField/Display/ListPageSelectedFilters.php @@ -5,6 +5,7 @@ namespace Drupal\oe_list_pages\Plugin\ExtraField\Display; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\oe_list_pages\ListPageConfiguration; /** * Extra field displaying the list page selected filters. @@ -28,7 +29,8 @@ public function getLabel() { * {@inheritdoc} */ public function viewElements(ContentEntityInterface $entity) { - return [$this->listBuilder->buildSelectedFilters($entity)]; + $configuration = ListPageConfiguration::fromEntity($entity); + return [$this->listBuilder->buildSelectedFilters($configuration)]; } }