You are here

function template_preprocess_search_api_server in Search API 8

Prepares variables for search_api_server form templates.

Default template: search-api-server.html.twig.

Parameters

array &$variables: An associative array containing:

  • server: The server that should be displayed.

File

./search_api.theme.inc, line 160
Defines theme functions for the Search API module.

Code

function template_preprocess_search_api_server(array &$variables) {

  // Get the search server.

  /** @var \Drupal\search_api\ServerInterface $server */
  $server = $variables['server'];
  if ($description = $server
    ->getDescription()) {

    // Sanitize the description and append to the output.
    $variables['description'] = $description;
  }

  // Initialize the $rows variable which will hold the different parts of server
  // information.
  $rows = [];

  // Create a row template with references so we don't have to deal with the
  // complicated structure for each individual row.
  $row = [
    'data' => [
      [
        'header' => TRUE,
      ],
      '',
    ],
    'class' => [
      '',
    ],
  ];

  // Get the individual parts of the row by reference.
  $label =& $row['data'][0]['data'];
  $info =& $row['data'][1];
  $classes =& $row['class'];

  // Check if the server is enabled.
  if ($server
    ->status()) {
    $classes[] = 'ok';
    $info = t('enabled (<a href=":url">disable</a>)', [
      ':url' => $server
        ->toUrl('disable')
        ->toString(),
    ]);
  }
  else {
    $classes[] = 'warning';
    $info = t('disabled (<a href=":url">enable</a>)', [
      ':url' => $server
        ->toUrl('enable')
        ->toString(),
    ]);
  }

  // Append the row and reset variables.
  $label = t('Status');
  $classes[] = 'search-api-server-summary--status';
  $rows[] = Utility::deepCopy($row);
  $classes = [];

  // Check if the backend used by the server is valid and get its label.
  if ($server
    ->hasValidBackend()) {
    $backend = $server
      ->getBackend();
    $info = Html::escape($backend
      ->label());
  }
  else {
    $classes[] = 'error';
    $info = t('Invalid or missing backend plugin: %backend_id', [
      '%backend_id' => $server
        ->getBackendId(),
    ]);
  }

  // Append the row and reset variables.
  $label = t('Backend class');
  $classes[] = 'search-api-server-summary--backend';
  $rows[] = Utility::deepCopy($row);
  $classes = [];

  // Build the indexes links container.
  $indexes = [
    '#theme' => 'links',
    '#attributes' => [
      'class' => [
        'inline',
      ],
    ],
    '#links' => [],
  ];

  // Add links for all indexes attached to this server.
  foreach ($server
    ->getIndexes() as $index) {
    $indexes['#links'][] = [
      'title' => $index
        ->label(),
      'url' => $index
        ->toUrl('canonical'),
    ];
  }

  // Check if the indexes variable contains links.
  if ($indexes['#links']) {
    $label = t('Search indexes');
    $info = render($indexes);
    $classes[] = 'search-api-server-summary--indexes';
    $rows[] = Utility::deepCopy($row);
    $classes = [];
  }

  // Add backend-specific additional information.
  foreach ($server
    ->viewSettings() as $information) {

    // Convert the extra information and append the information to the row.
    $label = $information['label'];
    $info = $information['info'];
    if (!empty($information['status'])) {
      $classes[] = $information['status'];
    }
    $rows[] = Utility::deepCopy($row);
    $classes = [];
  }

  // Append the server info table to the output.
  $variables['server_info_table'] = [
    '#theme' => 'table',
    '#rows' => $rows,
    '#attributes' => [
      'class' => [
        'search-api-server-summary',
      ],
    ],
  ];
}