You are here

function custom_search_preprocess_search_result in Custom Search 8

Same name and namespace in other branches
  1. 6 theme/custom_search.pages.inc \custom_search_preprocess_search_result()
  2. 7 theme/custom_search.pages.inc \custom_search_preprocess_search_result()

Customisation of the results info.

File

./custom_search.pages.inc, line 114
User page callbacks for the custom_search module.

Code

function custom_search_preprocess_search_result(&$variables) {
  $language_interface = \Drupal::languageManager()
    ->getCurrentLanguage();
  $config = Drupal::config('custom_search.settings.results')
    ->getRawData();
  $page_config = FALSE;
  $current_path = \Drupal::request()
    ->getPathInfo();
  $path = substr($current_path, strpos($current_path, 'search/') + 7);

  // Search for the config of this page.
  foreach ($config as $c) {
    if (isset($c['path']) && $c['path'] == $path) {
      $page_config = $c;
      break;
    }
  }
  $result = $variables['result'];
  $variables['url'] = UrlHelper::stripDangerousProtocols($result['link']);
  $variables['title'] = $result['title'];
  if (isset($result['language']) && $result['language'] != $language_interface
    ->getId() && $result['language'] != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
    $variables['title_attributes']['lang'] = $result['language'];
    $variables['content_attributes']['lang'] = $result['language'];
  }
  $info = [];
  if (!empty($result['plugin_id'])) {
    $info['plugin_id'] = $result['plugin_id'];
  }
  if (!empty($result['user'])) {
    $info['user'] = $result['user'];
  }
  if (!empty($result['date'])) {
    $info['date'] = \Drupal::service('date.formatter')
      ->format($result['date'], 'short');
  }
  if (isset($result['extra']) && is_array($result['extra'])) {
    $info = array_merge($info, $result['extra']);
  }

  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  if ($page_config) {
    $info = [];
    if (!empty($result['plugin_id'])) {
      $info['plugin_id'] = Html::escape($result['plugin_id']);
    }
    if (!empty($result['type']) && in_array('type', $page_config['info'], TRUE)) {
      $info['type'] = $result['type'];
    }
    if (!empty($result['user']) && in_array('user', $page_config['info'], TRUE)) {
      $info['user'] = $result['user'];
    }
    if (!empty($result['date']) && in_array('date', $page_config['info'], TRUE)) {
      $info['date'] = \Drupal::service('date.formatter')
        ->format($result['date'], 'short');
    }
    if (isset($result['extra']) && is_array($result['extra'])) {
      $info = array_merge($info, $result['extra']);
    }
  }

  // Provide separated and grouped meta information..
  $variables['info_split'] = $info;
  $variables['info'] = [
    '#type' => 'inline_template',
    '#template' => '{{ info|safe_join(" - ") }}',
    '#context' => [
      'info' => $info,
    ],
  ];
}