You are here

function search_api_requirements in Search API 8

Same name and namespace in other branches
  1. 7 search_api.install \search_api_requirements()

Implements hook_requirements().

File

./search_api.install, line 74
Install, update and uninstall functions for the Search API module.

Code

function search_api_requirements($phase) {
  if ($phase == 'runtime') {
    $requirements = [];
    $message = _search_api_search_module_warning();
    if ($message) {
      $requirements += [
        'search_api_core_search' => [
          'title' => t('Search API'),
          'value' => $message,
          'severity' => REQUIREMENT_WARNING,
        ],
      ];
    }

    /** @var \Drupal\search_api\ServerInterface[] $servers */
    $servers = Server::loadMultiple();
    $unavailable_servers = [];
    foreach ($servers as $server) {
      if ($server
        ->status() && !$server
        ->isAvailable()) {
        $unavailable_servers[] = $server
          ->label();
      }
    }
    if (!empty($unavailable_servers)) {
      $requirements += [
        'search_api_server_unavailable' => [
          'title' => t('Search API'),
          'value' => \Drupal::translation()
            ->formatPlural(count($unavailable_servers), 'The search server "@servers" is currently not available', 'The following search servers are not available: @servers', [
            '@servers' => implode(', ', $unavailable_servers),
          ]),
          'severity' => REQUIREMENT_ERROR,
        ],
      ];
    }
    $pending_tasks = \Drupal::getContainer()
      ->get('search_api.task_manager')
      ->getTasksCount();
    if ($pending_tasks) {
      $args['@link'] = '';
      $url = Url::fromRoute('search_api.execute_tasks');
      if ($url
        ->access()) {
        $link = new Link(t('Execute now'), $url);
        $link = $link
          ->toString();
        $args['@link'] = $link;
        $args['@link'] = new FormattableMarkup(' (@link)', $args);
      }
      $requirements['search_api_pending_tasks'] = [
        'title' => t('Search API'),
        'value' => \Drupal::translation()
          ->formatPlural($pending_tasks, 'There is @count pending Search API task. @link', 'There are @count pending Search API tasks. @link', $args),
        'severity' => REQUIREMENT_WARNING,
      ];
    }
    return $requirements;
  }
  return [];
}