<?php

/**
 * @file
 * Install and update functions for the WxT Extend Search Database module.
 */

use Drupal\search_api\IndexListBuilder;

/**
 * Implements hook_requirements().
 */
function wxt_ext_search_db_requirements($phase) {
  $requirements = [];

  if ($phase == 'install') {
    // In case this is installed at the same time as the Search API module
    // itself, the class autoloader will not be able to find the
    // \Drupal\search_api\IndexListBuilder class on its own. Load it manually.
    if (!class_exists(IndexListBuilder::class)) {
      $search_api_path = \Drupal::service('extension.list.module')->getPath('search_api');
      require_once $search_api_path . '/src/IndexListBuilder.php';
    }

    $errors = IndexListBuilder::checkDefaultsModuleCanBeInstalled();
    foreach ($errors as $type => $error) {
      $requirements["search_api_db_defaults:$type"] = [
        'severity' => REQUIREMENT_ERROR,
        'description' => $error,
      ];
    }
  }

  return $requirements;
}

/**
 * Implements hook_install().
 */
function wxt_ext_search_db_install() {
  // Clear the display plugin cache after installation so the plugin for the new
  // view (display) gets found.
  \Drupal::service('plugin.manager.search_api.display')
    ->clearCachedDefinitions();
}
