Skip to content

Commit

Permalink
EWPP-2570: Add EventSubscriber to alter created/updated facets.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergepavle committed Oct 14, 2022
1 parent 0a45edf commit 7e08d36
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
oe_list_pages_open_vocabularies.configurator:
class: \Drupal\oe_list_pages_open_vocabularies\SearchApiConfigurator
arguments: ['@entity_type.manager', '@entity_field.manager', '@oe_list_pages.list_source.factory', '@language_manager']
arguments: ['@entity_type.manager', '@entity_field.manager', '@oe_list_pages.list_source.factory', '@language_manager', '@event_dispatcher']
oe_list_pages_open_vocabularies.config_subscriber:
class: Drupal\oe_list_pages_open_vocabularies\EventSubscriber\AssociationTranslationSubscriber
arguments: ['@oe_list_pages_open_vocabularies.configurator', '@entity_type.manager']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_list_pages_open_vocabularies\Event;

/**
* Update Search API configuration.
*
* @internal
*/
final class SearchApiConfigurationEvents {

/**
* Event update search API facet configuration.
*/
const UPDATE_SEARCH_API_FACET = 'oe_list_pages_open_vocabularies.update_search_api_facet_config';

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_list_pages_open_vocabularies\Event;

use Drupal\facets\FacetInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* Event for updating search api facet configuration.
*/
class SearchApiFacetUpdateEvent extends Event {

/**
* The facet.
*
* @var \Drupal\facets\FacetInterface
*/
protected FacetInterface $facet;

/**
* SearchApiFacetUpdateEvent constructor.
*/
public function __construct(FacetInterface $facet) {
$this->facet = $facet;
}

/**
* Return the facet object.
*
* @return \Drupal\facets\FacetInterface
* The facet object instance.
*/
public function getFacet(): FacetInterface {
return $this->facet;
}

/**
* Set the updated facet object.
*
* @param \Drupal\facets\FacetInterface $facet
* The facet object instance.
*
* @return $this
*/
public function setFacet(FacetInterface $facet): SearchApiFacetUpdateEvent {
$this->facet = $facet;
return $this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use Drupal\facets\FacetInterface;
use Drupal\language\Config\LanguageConfigOverride;
use Drupal\oe_list_pages\ListSourceFactoryInterface;
use Drupal\oe_list_pages_open_vocabularies\Event\SearchApiConfigurationEvents;
use Drupal\oe_list_pages_open_vocabularies\Event\SearchApiFacetUpdateEvent;
use Drupal\open_vocabularies\OpenVocabularyAssociationInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Item\Field;
use Drupal\search_api\Item\FieldInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Configure search api when vocabulary associations are created.
Expand Down Expand Up @@ -53,14 +56,22 @@ class SearchApiConfigurator {
*/
private $languageManager;

/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;

/**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, ListSourceFactoryInterface $listSourceFactory, LanguageManagerInterface $languageManager) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, ListSourceFactoryInterface $listSourceFactory, LanguageManagerInterface $languageManager, EventDispatcherInterface $eventDispatcher) {
$this->entityTypeManager = $entityTypeManager;
$this->listSourceFactory = $listSourceFactory;
$this->entityFieldManager = $entityFieldManager;
$this->languageManager = $languageManager;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -133,7 +144,11 @@ public function updateConfig(OpenVocabularyAssociationInterface $association, st
$facet->setFacetSourceId($list_source->getSearchId());
$facet->setWidget('oe_list_pages_multiselect', []);
$facet->setFieldIdentifier($property_path);
$facet->save();

// Use event dispatching to allow alter facet config before saving.
$event = new SearchApiFacetUpdateEvent($facet);
$this->eventDispatcher->dispatch($event, SearchApiConfigurationEvents::UPDATE_SEARCH_API_FACET);
$event->getFacet()->save();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
oe_list_pages_open_vocabularies_test.config_subscriber:
class: Drupal\oe_list_pages_open_vocabularies_test\EventSubscriber\SearchApiFacetTestSubscriber
arguments: ['@oe_list_pages_open_vocabularies.configurator', '@entity_type.manager']
tags:
- { name: event_subscriber }
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_list_pages_open_vocabularies_test\EventSubscriber;

use Drupal\oe_list_pages_open_vocabularies\Event\SearchApiConfigurationEvents;
use Drupal\oe_list_pages_open_vocabularies\Event\SearchApiFacetUpdateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Responds to changes to facet configuration update/create.
*/
class SearchApiFacetTestSubscriber implements EventSubscriberInterface {

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[SearchApiConfigurationEvents::UPDATE_SEARCH_API_FACET] = 'onFacetUpdate';
return $events;
}

/**
* Updates the open vocabulary facets before saving.
*
* @param \Drupal\oe_list_pages_open_vocabularies\Event\SearchApiFacetUpdateEvent $event
* The Search API Facet update event.
*/
public function onFacetUpdate(SearchApiFacetUpdateEvent $event) {
$facet = $event->getFacet();
$settings = [
'behavior' => 'text',
'text' => 'No results found for this block!',
'text_format' => 'plain_text',
];
$facet->setEmptyBehavior($settings);
$event->setFacet($facet);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public function testVocabularyAssociationChanges(): void {
$this->assertEquals($values['label'], $facet_1->label());
$this->assertEquals($field_id, $facet_1->getFieldIdentifier());
$this->assertEquals($facet_1->getWeight(), SearchApiConfigurator::MAX_WEIGHT);
// Assert overriding of empty_behavior value by
// \Drupal\oe_list_pages_open_vocabularies_test\EventSubscriber\SearchApiFacetTestSubscriber.
$this->assertEquals([
'behavior' => 'text',
'text' => 'No results found for this block!',
'text_format' => 'plain_text',
], $facet_1->getEmptyBehavior());
$facet_2 = Facet::load($id_2);
$this->assertNull($facet_2);
// Check fields exists.
Expand Down Expand Up @@ -88,14 +95,27 @@ public function testVocabularyAssociationChanges(): void {
$this->assertEquals('New label', $facet_1->label());
$this->assertEquals($field_id, $facet_1->getFieldIdentifier());
$this->assertEquals($facet_1->getWeight(), SearchApiConfigurator::MAX_WEIGHT);
// Assert overriding of empty_behavior value by
// \Drupal\oe_list_pages_open_vocabularies_test\EventSubscriber\SearchApiFacetTestSubscriber.
$this->assertEquals([
'behavior' => 'text',
'text' => 'No results found for this block!',
'text_format' => 'plain_text',
], $facet_1->getEmptyBehavior());
$this->assertArrayHasKey('url_processor_handler', $facet_1->getProcessors());
$this->assertArrayHasKey('display_value_widget_order', $facet_1->getProcessors());
$this->assertArrayHasKey('translate_entity', $facet_1->getProcessors());
$facet_2 = Facet::load($id_2);
$this->assertEquals('New label', $facet_2->label());
$this->assertEquals($field_id, $facet_2->getFieldIdentifier());
$this->assertEquals($facet_2->getWeight(), SearchApiConfigurator::MAX_WEIGHT);

// Assert overriding of empty_behavior value by
// \Drupal\oe_list_pages_open_vocabularies_test\EventSubscriber\SearchApiFacetTestSubscriber.
$this->assertEquals([
'behavior' => 'text',
'text' => 'No results found for this block!',
'text_format' => 'plain_text',
], $facet_2->getEmptyBehavior());
// Check field exists.
$node_index = Index::load('node');
$field_1 = $node_index->getField($field_id);
Expand Down

0 comments on commit 7e08d36

Please sign in to comment.