You are here

lightning_search.install in Lightning Core 8

Contains installation and update routines for Lightning Search.

File

modules/lightning_search/lightning_search.install
View source
<?php

/**
 * @file
 * Contains installation and update routines for Lightning Search.
 */
use Drupal\lightning_core\ConfigHelper as Config;
use Drupal\node\Entity\NodeType;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;

/**
 * Implements hook_install().
 */
function lightning_search_install() {

  // Search API DB is not a hard dependency, but install it if it's available so
  // that the search index we provide will "just work" out of the box.
  $module_data = system_rebuild_module_data();
  if (isset($module_data['search_api_db'])) {
    \Drupal::service('module_installer')
      ->install([
      'search_api_db',
    ]);
  }

  // Don't make any configuration changes during config sync.
  if (\Drupal::isConfigSyncing()) {
    return;
  }

  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_types = NodeType::loadMultiple();
  array_walk($node_types, 'lightning_search_node_type_insert');

  // The database server is optional configuration, to be installed only if
  // Search API DB is present. For some reason, it's not installed during a
  // normal site install, so create it now if it doesn't already exist.
  $server = Server::load('database');
  if (empty($server) && \Drupal::moduleHandler()
    ->moduleExists('search_api_db')) {
    Config::forModule('lightning_search')
      ->optional()
      ->getEntity('search_api_server', 'database')
      ->save();
    $server = Server::load('database');
  }
  if ($server) {
    Index::load('content')
      ->setServer($server)
      ->enable()
      ->save();
  }
}

/**
 * Implements hook_update_dependencies().
 */
function lightning_search_update_dependencies() {
  return [
    'block_content' => [
      8300 => [
        // block_content 8300 updates entity type definitions, which implicitly
        // touches a lot of Search API configuration because entity type
        // definition updates trigger Views cache rebuilds, which in turn
        // triggers a lot of work, especially plugin instantiation, in Search
        // API. So, if the configuration isn't fully up-to-date, things are
        // likely to go kaboom. This ensures that Search API configuration is
        // up-to-date before block_content 8300 updates entity type definitions.
        'search_api' => 8104,
      ],
    ],
  ];
}