Skip to content

Commit

Permalink
OPENEUROPA-3272: Refactiring test and event subscriber.
Browse files Browse the repository at this point in the history
  • Loading branch information
upchuk committed Jun 12, 2020
1 parent 39f0fa0 commit eaf11d7
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 112 deletions.
8 changes: 4 additions & 4 deletions src/ListPageEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
final class ListPageEvents {

/**
* Name of the event fired on retrieving allowed entity types.
* Event fired when altering the entity types.
*
* @var string
*/
const ALTER_ALLOWED_ENTITY_TYPES = 'oe_list_pages.allowed_entity_types_alter';
const ALTER_ENTITY_TYPES = 'oe_list_pages.entity_types_alter';

/**
* Name of the event fired on retrieving allowed bundles.
* Event fired when altering the bundles.
*
* @var string
*/
const ALTER_ALLOWED_BUNDLES = 'oe_list_pages.allowed_bundles_alter';
const ALTER_BUNDLES = 'oe_list_pages.bundles_alter';

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@

namespace Drupal\oe_list_pages;

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Symfony\Component\EventDispatcher\Event;

/**
* Defines a ListPageSourceRetrieveEvent event.
* Event thrown in order to alter the list source.
*
* The entity types and bundles that can be selected for a list source can
* be altered by subscribing to this event.
*/
class ListPageSourceRetrieveEvent extends Event implements RefinableCacheableDependencyInterface {

use RefinableCacheableDependencyTrait;
class ListPageSourceAlterEvent extends Event {

/**
* The list of entity types.
*
* @var array
*/
protected $entityTypeIds;
protected $entityTypes;

/**
* The list of bundles.
Expand All @@ -30,15 +29,15 @@ class ListPageSourceRetrieveEvent extends Event implements RefinableCacheableDep
protected $bundles;

/**
* Constructs a new ListPageSourceRetrieveEvent.
* Constructs a new ListPageSourceAlterEvent.
*
* @param array $entity_type_ids
* @param array $entity_types
* The list of entity type ids.
* @param array $bundles
* The list of entity bundles.
*/
public function __construct(array $entity_type_ids, array $bundles = []) {
$this->entityTypeIds = $entity_type_ids;
public function __construct(array $entity_types, array $bundles = []) {
$this->entityTypes = $entity_types;
$this->bundles = $bundles;
}

Expand All @@ -49,17 +48,17 @@ public function __construct(array $entity_type_ids, array $bundles = []) {
* The list of entity types.
*/
public function getEntityTypes(): array {
return $this->entityTypeIds;
return $this->entityTypes;
}

/**
* Set the allowed entity types.
*
* @param array $entity_type_ids
* @param array $entity_types
* The list of entity types.
*/
public function setEntityTypes(array $entity_type_ids): void {
$this->entityTypeIds = $entity_type_ids;
public function setEntityTypes(array $entity_types): void {
$this->entityTypes = $entity_types;
}

/**
Expand All @@ -81,7 +80,7 @@ public function getBundles(): array {
* The bundles.
*/
public function setBundles(string $entity_type, array $bundles): void {
$this->entityTypeIds = [$entity_type => $entity_type];
$this->entityTypes = [$entity_type];
$this->bundles = $bundles;
}

Expand Down
21 changes: 11 additions & 10 deletions src/Plugin/EntityMetaRelation/ListPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Drupal\emr\Entity\EntityMetaInterface;
use Drupal\emr\Plugin\EntityMetaRelationContentFormPluginBase;
use Drupal\oe_list_pages\ListPageEvents;
use Drupal\oe_list_pages\ListPageSourceRetrieveEvent;
use Drupal\oe_list_pages\ListPageSourceAlterEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand Down Expand Up @@ -109,16 +109,16 @@ public function build(array $form, FormStateInterface $form_state, ContentEntity

$entity_type_options = [];
$entity_types = $this->entityTypeManager->getDefinitions();
foreach ($entity_types as $entity_key => $entity_type) {
foreach ($entity_types as $entity_type_key => $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface) {
continue;
}
$entity_type_options[$entity_key] = $entity_type->getLabel();
$entity_type_options[$entity_type_key] = $entity_type->getLabel();
}

$event = new ListPageSourceRetrieveEvent($entity_type_options);
$this->eventDispatcher->dispatch(ListPageEvents::ALTER_ALLOWED_ENTITY_TYPES, $event);
$entity_type_options = $event->getEntityTypes();
$event = new ListPageSourceAlterEvent(array_keys($entity_type_options));
$this->eventDispatcher->dispatch(ListPageEvents::ALTER_ENTITY_TYPES, $event);
$entity_type_options = array_intersect_key($entity_type_options, array_combine($event->getEntityTypes(), $event->getEntityTypes()));

$entity_type_id = $entity_meta_wrapper->getSourceEntityType();

Expand All @@ -127,8 +127,9 @@ public function build(array $form, FormStateInterface $form_state, ContentEntity
'#title' => $this->t('Source entity type'),
'#description' => $this->t('Select the entity type that will be used as the source for this list page.'),
'#options' => $entity_type_options,
// If there is no selection, the default entity type will be Node, due to
// self::fillDefaultEntityMetaValues().
'#default_value' => $form_state->getValue('entity_type') ?? $entity_type_id,
'#empty_option' => $this->t('- Select -'),
'#required' => TRUE,
'#ajax' => [
'callback' => [$this, 'updateEntityBundles'],
Expand All @@ -145,9 +146,9 @@ public function build(array $form, FormStateInterface $form_state, ContentEntity
$bundle_options[$bundle_key] = $bundle['label'];
}

$event->setBundles($selected_entity_type, $bundle_options);
$this->eventDispatcher->dispatch(ListPageEvents::ALTER_ALLOWED_BUNDLES, $event);
$bundle_options = $event->getBundles();
$event->setBundles($selected_entity_type, array_keys($bundle_options));
$this->eventDispatcher->dispatch(ListPageEvents::ALTER_BUNDLES, $event);
$bundle_options = array_intersect_key($bundle_options, array_combine($event->getBundles(), $event->getBundles()));
}

$entity_bundle_id = $entity_meta_wrapper->getSourceEntityBundle();
Expand Down
Loading

0 comments on commit eaf11d7

Please sign in to comment.