Introduce events and deprecate hooks

Created on 13 March 2021, almost 4 years ago
Updated 6 August 2024, 4 months ago

After the commit of #3203214: Adopt Drupal >= 9.1 Event dispatching we can now introduce symfony 5 style events like solarium dispatches. The implementation will work for Drupal <= 9.0 and Drupal >= 9.1.

Feature request
Status

Fixed

Version

4.0

Component

Code

Created by

🇩🇪Germany mkalkbrenner 🇩🇪

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇱Netherlands Krilo_89

    @SomebodySysop, I'm reading this and seeing your example, I'm backing the idea to put in some examples on the module.

    The reason your code isn't working, is because:

    return [
       'SearchApiSolrEvents::POST_EXTRACT_RESULTS' => 'processResults'
    ];

    Must return an array like: Class => 'function'. You are returning a 'string' => 'string', which isn't working.

    I had to use an PostFieldMappingEvent and did it like so:

    services.yml file:

    services:
      custom_module.event_subscriber:
        class: Drupal\custom_module\EventSubscriber\PostFieldMappingEvent
        tags:
          - { name: event_subscriber }
    

    My PostFieldMappingEvent.php file:

    
    namespace Drupal\custom_module\EventSubscriber;
    
    use Drupal\search_api_solr\Event\SearchApiSolrEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    /**
     * Custom module event subscriber.
     */
    class PostFieldMappingEvent implements EventSubscriberInterface {
    
      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents(): array {
        return [
          SearchApiSolrEvents::POST_FIELD_MAPPING => 'alterSolrFieldNames',
        ];
      }
    
      /**
       * Change the Solr field names.
       *
       * @param \Drupal\search_api_solr\Event\PostFieldMappingEvent $event
       */
      public function alterSolrFieldNames(PostFieldMappingEvent $event): void {
        // Your code here
      }
    
    }
    

    I hope this makes it easier to understand, but maybe we still need to add some examples. Not everyone knows how to use Events..

  • 🇺🇸United States somebodysysop

    Thank you very much for the reply. I get the example you provided. The problem is, now knowing much about events, I followed the global examples I saw. Which is really why it is so useful to have search api solr examples available *somewhere*.

  • 🇬🇧United Kingdom nicrodgers Monmouthshire, UK

    I've added a guide page explaining how to replace the deprecated hook_search_api_solr_query_alter with PreQueryEvent. Hopefully it helps some people.
    https://www.drupal.org/docs/8/modules/search-api-solr/search-api-solr-ho...

  • 🇨🇦Canada mandclu

    @nicrodgers Thanks very much for the guide page, this has been very helpful. Since the changes here were released in search_api_solr 4.2.4, maybe the guide should mention updating your dependencies to require >= 4.2.4, or even ^4.3? I suppose this is more relevant to other contrib modules that have been using the hooks.

  • 🇮🇳India avinashm

    @nicrodgers Thank you so much for the documentation page. It really helps me to replace the deprecated hook_search_api_solr_documents_alter with PostCreateIndexDocumentsEvent.

  • 🇦🇺Australia taggartj

    Just came across this in my case I was trying to add an extra field as I used to do in hook_search_api_solr_documents_alter()

    See search_api_solr/src/Event/PostCreateIndexDocumentsEvent.php for doc
    but here is the quick version

      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents(): array {
        return [
          SearchApiSolrEvents::POST_CREATE_INDEX_DOCUMENTS => 'alterSolrFieldsAfterIndex',
        ];
      }
    
      /**
       * {@inheritdoc}
       */
      public function alterSolrFieldsAfterIndex(PostCreateIndexDocumentsEvent $event): void {
        $documents = $event->getSolariumDocuments();
        foreach ($documents as $document) {
          $documentFields = $document->getFields();
          .... 
          $document->setField('ss_test', 'hello');
        }
        $event->setSolariumDocuments($documents);
      }
    
  • 🇯🇵Japan liuyuanchao

    liuyuanchao changed the visibility of the branch 3203375-introduce-events-and to hidden.

  • 🇯🇵Japan liuyuanchao

    liuyuanchao changed the visibility of the branch 3203375-introduce-events-and to hidden.

  • 🇯🇵Japan liuyuanchao

    liuyuanchao changed the visibility of the branch 3203375-introduce-events-and to hidden.

Production build 0.71.5 2024