Skip to content

Commit

Permalink
Merge pull request #123 from openeuropa/update-EPIC-EWPP-1552-default…
Browse files Browse the repository at this point in the history
…-related-lists

Update epic EWPP-1552: Default related lists
  • Loading branch information
upchuk authored Jan 12, 2022
2 parents b352fa4 + ac216b3 commit ec16ece
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 7 deletions.
110 changes: 110 additions & 0 deletions src/Form/ListPageConfigurationSubForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Drupal\oe_list_pages\ListPageConfiguration;
use Drupal\oe_list_pages\ListPageConfigurationSubformInterface;
use Drupal\oe_list_pages\ListPageEvents;
use Drupal\oe_list_pages\ListPageSortAlterEvent;
use Drupal\oe_list_pages\ListPageSourceAlterEvent;
use Drupal\oe_list_pages\DefaultFilterConfigurationBuilder;
use Drupal\oe_list_pages\ListSourceFactoryInterface;
Expand All @@ -24,6 +25,8 @@

/**
* Default list page configuration subform.
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class ListPageConfigurationSubForm implements ListPageConfigurationSubformInterface {

Expand Down Expand Up @@ -114,6 +117,13 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
$entity_type_options = $this->getEntityTypeOptions();
$entity_type_id = $this->configuration->getEntityType();
$entity_type_bundle = $this->configuration->getBundle();
$configuration_sort = $this->configuration->getSort();
if ($configuration_sort) {
$configuration_sort = static::generateSortMachineName($configuration_sort);
}
else {
$configuration_sort = NULL;
}

$ajax_wrapper_id = 'list-page-configuration-' . ($form['#parents'] ? '-' . implode('-', $form['#parents']) : '');

Expand All @@ -126,6 +136,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {

$selected_entity_type = $form_state->has('entity_type') ? $form_state->get('entity_type') : $entity_type_id;
$selected_bundle = $form_state->has('bundle') ? $form_state->get('bundle') : $entity_type_bundle;
$selected_sort = $form_state->has('sort') ? $form_state->get('sort') : $configuration_sort;
if (!$form_state->has('entity_type')) {
$form_state->set('entity_type', $selected_entity_type);
}
Expand Down Expand Up @@ -192,6 +203,23 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
// Try to get the list source for the selected entity type and bundle.
$list_source = $this->listSourceFactory->get($selected_entity_type, $selected_bundle);

if ($list_source) {
$sort_options = $this->getSortOptions($list_source);
$form_state->set('default_bundle_sort', $this->getBundleDefaultSort($list_source));
if ($selected_sort && !isset($sort_options[$selected_sort])) {
// In case we no longer have the sort option, we should not show
// any default value.
$selected_sort = NULL;
}
$form['wrapper']['sort'] = [
'#type' => 'select',
'#options' => $sort_options,
'#title' => $this->t('Sort'),
'#default_value' => $selected_sort,
'#access' => count($sort_options) > 1,
];
}

// Get available filters.
if ($list_source && $available_filters = $list_source->getAvailableFilters()) {
$exposed_filters = $this->getExposedFilters($list_source);
Expand Down Expand Up @@ -332,6 +360,29 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
$exposed_filters = [];
}

$sort = $form_state->getValue(['wrapper', 'sort']);
$default_bundle_sort = $form_state->get('default_bundle_sort');
if ($default_bundle_sort) {
// In case a bundle is not properly configured with a default sort, we
// need to check if the value is there before we generate a machine name.
$default_bundle_sort = static::generateSortMachineName($default_bundle_sort);
}

if ($sort) {
list($name, $direction) = explode('__', $sort);
$this->configuration->setSort([
'name' => $name,
'direction' => strtoupper($direction),
]);
}

if ($sort === $default_bundle_sort || $sort == "") {
// If the user configured the default bundle sort, we remove the value
// from the configuration in case later we change the defaults, we want
// the defaults to still take effect.
$this->configuration->setSort([]);
}

$this->configuration->setExposedFilters($exposed_filters);
}

Expand Down Expand Up @@ -515,4 +566,63 @@ protected function getBundleDefaultExposedFilters(ListSourceInterface $list_sour
return $bundle->getThirdPartySetting('oe_list_pages', 'default_exposed_filters', []);
}

/**
* Get the default sort configuration from the bundle.
*
* @param \Drupal\oe_list_pages\ListSourceInterface $list_source
* The selected list source.
*
* @return array
* The sort information.
*/
protected function getBundleDefaultSort(ListSourceInterface $list_source): array {
$default_sort = &drupal_static(__FUNCTION__ . $list_source->getEntityType() . $list_source->getBundle());
if ($default_sort) {
return $default_sort;
}
$bundle_entity_type = $this->entityTypeManager->getDefinition($list_source->getEntityType())->getBundleEntityType();
$storage = $this->entityTypeManager->getStorage($bundle_entity_type);
$bundle = $storage->load($list_source->getBundle());
$default_sort = $bundle->getThirdPartySetting('oe_list_pages', 'default_sort', []);
return $default_sort;
}

/**
* Gets the available sort options.
*
* @param \Drupal\oe_list_pages\ListSourceInterface $list_source
* The selected list source.
*
* @return array
* The sort options.
*/
protected function getSortOptions(ListSourceInterface $list_source): array {
$options = [];

// Get the default sort option.
$sort = $this->getBundleDefaultSort($list_source);
if ($sort) {
$options[static::generateSortMachineName($sort)] = $this->t('Default');
}

$event = new ListPageSortAlterEvent($list_source->getEntityType(), $list_source->getBundle());
$event->setOptions($options);
$this->eventDispatcher->dispatch(ListPageEvents::ALTER_SORT_OPTIONS, $event);

return $event->getOptions();
}

/**
* Given a sort array with "name" and "direction", generate a machine name.
*
* @param array $sort
* The sort information.
*
* @return string
* The machine name.
*/
public static function generateSortMachineName(array $sort): string {
return $sort['name'] . '__' . strtoupper($sort['direction']);
}

}
28 changes: 22 additions & 6 deletions src/ListExecutionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public function executeList(ListPageConfiguration $configuration): ?ListExecutio
// If there is a sort configured use it,
// otherwise use the bundle's default sort if set.
$sort = $configuration->getSort();
if (!$sort) {
$bundle_entity_type = $this->entityTypeManager->getDefinition($configuration->getEntityType())->getBundleEntityType();
$storage = $this->entityTypeManager->getStorage($bundle_entity_type);
$bundle = $storage->load($configuration->getBundle());
$sort = $bundle->getThirdPartySetting('oe_list_pages', 'default_sort', []);
}
$bundle_sort = $this->getBundleDefaultSort($list_source);
// If we have a specific sort, we use that first, followed by the default
// bundle sort. Otherwise, just the bundle sort.
$sort = $sort ? [$sort['name'] => $sort['direction']] : [];
if ($bundle_sort) {
$sort[$bundle_sort['name']] = $bundle_sort['direction'];
}

$options = [
'limit' => $limit,
Expand All @@ -118,4 +118,20 @@ public function executeList(ListPageConfiguration $configuration): ?ListExecutio
return $list_execution;
}

/**
* Get the default sort configuration from the bundle.
*
* @param \Drupal\oe_list_pages\ListSourceInterface $list_source
* The selected list source.
*
* @return array
* The sort information.
*/
protected function getBundleDefaultSort(ListSourceInterface $list_source): array {
$bundle_entity_type = $this->entityTypeManager->getDefinition($list_source->getEntityType())->getBundleEntityType();
$storage = $this->entityTypeManager->getStorage($bundle_entity_type);
$bundle = $storage->load($list_source->getBundle());
return $bundle->getThirdPartySetting('oe_list_pages', 'default_sort', []);
}

}
2 changes: 1 addition & 1 deletion src/ListPageConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function fromEntity(ContentEntityInterface $entity): ListPageConfi
'exposed_filters_overridden' => isset($wrapper_configuration['override_exposed_filters']) ? (bool) $wrapper_configuration['override_exposed_filters'] : FALSE,
'limit' => $wrapper_configuration['limit'] ?? NULL,
'page' => $wrapper_configuration['page'] ?? NULL,
'sort' => [],
'sort' => $wrapper_configuration['sort'] ?? [],
];

$exclude = [
Expand Down
8 changes: 8 additions & 0 deletions src/ListPageEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ final class ListPageEvents {
*/
const ALTER_RSS_ITEM_BUILD = "oe_list_pages.rss_build_item_alter";


/**
* Event fired to determine sort options.
*
* @var string
*/
const ALTER_SORT_OPTIONS = "oe_list_pages.alter_sort_options";

}
88 changes: 88 additions & 0 deletions src/ListPageSortAlterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_list_pages;

use Symfony\Component\EventDispatcher\Event;

/**
* Event thrown in order to determine sort options.
*/
class ListPageSortAlterEvent extends Event {

/**
* The entity type.
*
* @var string
*/
protected $entityType;

/**
* The bundle.
*
* @var string
*/
protected $bundle;

/**
* The sorting options.
*
* @var array
*/
protected $options = [];

/**
* Constructs a new ListPageSourceAlterEvent.
*
* @param string $entity_type
* The The entity type.
* @param string $bundle
* The bundle.
*/
public function __construct(string $entity_type, string $bundle) {
$this->entityType = $entity_type;
$this->bundle = $bundle;
}

/**
* Returns the entity type.
*
* @return string
* The entity type.
*/
public function getEntityType(): string {
return $this->entityType;
}

/**
* Returns the bundle.
*
* @return string
* The bundle.
*/
public function getBundle(): string {
return $this->bundle;
}

/**
* Returns the sort options.
*
* @return array
* The sort options.
*/
public function getOptions(): array {
return $this->options;
}

/**
* Sets the sort information.
*
* @param array $options
* The sort options.
*/
public function setOptions(array $options): void {
$this->options = $options;
}

}
1 change: 1 addition & 0 deletions src/Plugin/EntityMetaRelation/ListPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public function submit(array $form, FormStateInterface $form_state): void {
$this->getFormKey(),
'limit',
]);
$entity_meta_configuration['sort'] = $configuration->getSort();
$entity_meta_wrapper->setConfiguration($entity_meta_configuration);
$host_entity->get('emr_entity_metas')->attach($entity_meta);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\oe_list_pages\ListPageRssAlterEvent;
use Drupal\oe_list_pages\ListPageEvents;
use Drupal\oe_list_pages\ListPageRssItemAlterEvent;
use Drupal\oe_list_pages\ListPageSortAlterEvent;
use Drupal\oe_list_pages\ListPageSourceAlterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -42,6 +43,7 @@ public static function getSubscribedEvents() {
ListPageEvents::ALTER_BUNDLES => ['onBundlesAlter'],
ListPageEvents::ALTER_RSS_BUILD => ['onRssBuildAlter'],
ListPageEvents::ALTER_RSS_ITEM_BUILD => ['onRssItemBuildAlter'],
ListPageEvents::ALTER_SORT_OPTIONS => ['onSortOptionsAlter'],
];
}

Expand Down Expand Up @@ -117,4 +119,19 @@ public function onRssItemBuildAlter(ListPageRssItemAlterEvent $event): void {
$event->setBuild($build);
}

/**
* Event handler for altering the sort options.
*
* @param \Drupal\oe_list_pages\ListPageSortAlterEvent $event
* The event.
*/
public function onSortOptionsAlter(ListPageSortAlterEvent $event): void {
$alter = (bool) $this->state->get('oe_list_pages_test.alter_sort_options');
if ($alter) {
$options = $event->getOptions();
$options['field_test_boolean__DESC'] = 'Boolean';
$event->setOptions($options);
}
}

}
Loading

0 comments on commit ec16ece

Please sign in to comment.