Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-2371: Refactoring the list builder and execution manager to work with other config. #147

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/ListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'));
Expand Down Expand Up @@ -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();

Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
26 changes: 13 additions & 13 deletions src/ListBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,55 @@ 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.
*
* 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.
Expand Down
6 changes: 5 additions & 1 deletion src/ListExecutionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', []);
Expand Down
30 changes: 30 additions & 0 deletions src/ListPageConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

}
4 changes: 3 additions & 1 deletion src/Plugin/ExtraField/Display/ListPageFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/Plugin/ExtraField/Display/ListPagePagerInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)];
}

}
4 changes: 3 additions & 1 deletion src/Plugin/ExtraField/Display/ListPageResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)];
}

}
4 changes: 3 additions & 1 deletion src/Plugin/ExtraField/Display/ListPageSelectedFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)];
}

}