Commit 9036d9db authored by Robin De Herdt's avatar Robin De Herdt
Browse files

Issue #3318075: Default search_item display mode

parent 70eed3d8
Loading
Loading
Loading
Loading
+3 −0
Changes for README.md: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -39,6 +39,9 @@ Add both this module and the block ui library to your project's composer.json, w
```
And install it as usual: `composer require drupal/elasticsearch_search_api`

When enabling the module, the 'search_index' view mode will be added to all existing content types.
Remember to run config export afterwards, in order to persist these view mode settings.

### Example module elasticsearch_search_api_example

The base module has a submodule, elasticsearch_search_api_example. This submodule is intended as a demo
+22 −0
Changes for elasticsearch_search_api.install: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

use Drupal\elasticsearch_search_api\Utility\UtilityHelper;
use Drupal\node\Entity\NodeType;

/**
 * Enable the 'search_index' view mode for every content type.
 *
 * By default, created view_mode references will only display node title,
 * the rest of the fields will need to be configured manually.
 */
function elasticsearch_search_api_install() {
  if (!\Drupal::moduleHandler()->moduleExists('node')) {
    return;
  }

  $content_types = NodeType::loadMultiple();
  foreach ($content_types as $content_type) {
    UtilityHelper::configureSearchIndexViewMode($content_type);
  }
}
+9 −0
Changes for elasticsearch_search_api.module: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@
 */

use Drupal\Core\Cache\Cache;
use Drupal\node\NodeTypeInterface;
use Drupal\elasticsearch_search_api\Utility\UtilityHelper;

/**
 * Implements hook_help().
@@ -129,3 +131,10 @@ function elasticsearch_search_api_cron() {
function elasticsearch_search_api_api_items_indexed() {
  Cache::invalidateTags(['elasticsearch_search_api.search']);
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function elasticsearch_search_api_node_type_insert(NodeTypeInterface $node_type) {
  UtilityHelper::configureSearchIndexViewMode($node_type);
}
+37 −0
Changes for src/Utility/UtilityHelper.php: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ namespace Drupal\elasticsearch_search_api\Utility;

use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\node\NodeTypeInterface;

/**
 * Provides specific helper methods.
@@ -27,4 +29,39 @@ class UtilityHelper {
    return $matches[1];
  }

  /**
   * Helper function to add 'search_index' view mode to the given content type.
   *
   * @param \Drupal\node\NodeTypeInterface $content_type
   *   A content type machine name.
   */
  public static function configureSearchIndexViewMode(NodeTypeInterface $content_type) {
    $display = EntityViewDisplay::load("node.{$content_type->id()}.search_index");

    // If there is already a 'search_index' view mode
    // defined for this content type, don't alter it.
    if ($display instanceof EntityViewDisplay) {
      return;
    }

    // Create & enable the view display.
    $display = EntityViewDisplay::create([
      'targetEntityType' => 'node',
      'bundle' => $content_type->id(),
      'mode' => 'search_index',
    ]);

    // Remove all default components, except title.
    foreach (array_keys($display->getComponents()) as $name) {
      if ($name !== 'title') {
        $display->removeComponent($name);
      }
    }

    $display->enable();
    $display->save();

    \Drupal::logger('elasticsearch_search_api')->notice("Display {$display->id()} was added.");
  }

}