You are here

public static function IndexListBuilder::checkDefaultsModuleCanBeInstalled in Search API 8

Determines whether the "Database Search Defaults" module can be installed.

Return value

\Drupal\Core\StringTranslation\TranslatableMarkup[] An array of error messages describing why the module cannot be installed, keyed by a short, machine name-like identifier for the kind of error. If the array is empty, the module can be installed.

2 calls to IndexListBuilder::checkDefaultsModuleCanBeInstalled()
IndexListBuilder::render in src/IndexListBuilder.php
Builds the entity listing as renderable array for table.html.twig.
search_api_db_defaults_requirements in modules/search_api_db/search_api_db_defaults/search_api_db_defaults.install
Implements hook_requirements().

File

src/IndexListBuilder.php, line 63

Class

IndexListBuilder
Builds a listing of search index entities.

Namespace

Drupal\search_api

Code

public static function checkDefaultsModuleCanBeInstalled() {
  $errors = [];

  // If the Node module is missing, no further checks are necessary/possible.
  if (!\Drupal::moduleHandler()
    ->moduleExists('node')) {
    $errors['node_module'] = t('The required Node module is not installed on your site. Database Search Defaults module could not be installed.');
    return $errors;
  }
  $node_types = NodeType::loadMultiple();
  $required_types = [
    'article' => [
      'body',
      'comment',
      'field_tags',
      'field_image',
    ],
    'page' => [
      'body',
    ],
  ];

  /** @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager */
  $entity_field_manager = \Drupal::service('entity_field.manager');
  foreach ($required_types as $required_type_id => $required_fields) {
    if (!isset($node_types[$required_type_id])) {
      $errors[$required_type_id] = t('Content type @content_type not found. Database Search Defaults module could not be installed.', [
        '@content_type' => $required_type_id,
      ]);
    }
    else {

      // Check if all the fields are here.
      $fields = $entity_field_manager
        ->getFieldDefinitions('node', $required_type_id);
      foreach ($required_fields as $required_field) {
        if (!isset($fields[$required_field])) {
          $errors[$required_type_id . ':' . $required_field] = t('Field @field in content type @node_type not found. Database Search Defaults module could not be installed', [
            '@node_type' => $required_type_id,
            '@field' => $required_field,
          ]);
        }
      }
    }
  }
  if (\Drupal::moduleHandler()
    ->moduleExists('search_api_db')) {
    $entities_to_check = [
      'search_api_index' => 'default_index',
      'search_api_server' => 'default_server',
      'view' => 'search_content',
    ];

    /** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */
    $entity_type_manager = \Drupal::service('entity_type.manager');
    foreach ($entities_to_check as $entity_type => $entity_id) {
      try {

        // Find out if the entity is already in place. If so, fail to install
        // the module.
        $entity_storage = $entity_type_manager
          ->getStorage($entity_type);
        $entity_storage
          ->resetCache();
        $entity = $entity_storage
          ->load($entity_id);
        if ($entity) {
          $errors['defaults_exist'] = t('It looks like the default setup provided by this module already exists on your site. Cannot re-install module.');
          break;
        }
      } catch (PluginException $e) {

        // This can only happen for the view, if the Views module isn't
        // installed. Ignore.
      }
    }
  }
  return $errors;
}