0
votes

I'm using the elasticsearch connector module for Drupal 8 to manage indexes. https://www.drupal.org/project/elasticsearch_connector

One thing I need to happen on index creation time is to add some analyzers and filters to the elasticsearch index mapping.

I've tracked down the place that's happening here in the create() method for one of their classes:

modules\contrib\elasticsearch_connector\src\ElasticSearch\Parameters\Factory\IndexFactory.php

I have a custom module that I'm trying to extend that method and add to it but can't seem to get it to fire. I have the module enabled properly as it shows up on the extend page. So far my folder structure for my custom module looks like this:

modules\custom\elasticsearch_analyzer\src\elasticsearch_analyzer.php

    <?php

    namespace Drupal\elasticsearch_analyzer;

    use Drupal\search_api\IndexInterface;
    use Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory;

    class elasticsearch_analyzer extends IndexFactory {

      /**
       * {@inheritdoc}
       */
      public static function create(IndexInterface $index) {
        die('does this work?');
        return [
          'index' => IndexFactory::getIndexName($index),
          'body' => [
            'settings' => [
              'number_of_shards' => $index->getOption('number_of_shards', 5),
              'number_of_replicas' => $index->getOption('number_of_replicas', 1),
            ],
          ],
        ];
      }

    }

I've tried a bunch of different combinations surrounding the PSR-4 standard, but can't seem to get it to run. Is this how we'd normally go about extending things in Drupal 8? Should I just be putting this class in the .module file at the root of my module? Should I be registering this as a service?

Based on the tutorials I've read, I haven't been able to see any examples of extending contributed modules or core outside of creating block plugins or simple route pages. I thought now that we're out of the hook world I should be able to examine code within an existing class and override it using OOP inheritance. Or is that not the case?

1

1 Answers

0
votes

You cannot just extend a class. How is the module supposed to call this new class? How to you communicate its existence to other modules so they are able to call it? Your class in isolation does not do anything.

I was reading through the source code of this module. This IndexFactory class is used in the SearchApiElasticsearchBackend class which is registered as a backend to the Search API module (i.e. the search api knows that this backend exists and calls it).

Sadly this IndexManager class is just hardcoded into the backend and there is no way to inject your own. The class has static methods only so they are called directly. Even if you would create your own search backend and extend SearchApiElasticsearchBackend you would have to replace IndexManager with YourCustomIndexManager everywhere.

You will have to change the create() method directly in module to do what you want.