You are here

function search_api_view_insert in Search API 8

Implements hook_ENTITY_TYPE_insert() for type "view".

File

./search_api.module, line 421
Provides a rich framework for creating searches.

Code

function search_api_view_insert(ViewEntityInterface $view) {
  _search_api_view_crud_event($view);

  // Disable Views' default caching mechanisms on Search API views.
  $displays = $view
    ->get('display');
  if ($displays['default']['display_options']['query']['type'] === 'search_api_query') {
    $change = FALSE;
    foreach ($displays as $id => $display) {
      if (in_array($display['display_options']['cache']['type'] ?? '', [
        'tag',
        'time',
      ])) {
        $displays[$id]['display_options']['cache']['type'] = 'none';
        $change = TRUE;
      }
    }
    if ($change) {
      $warning = t('The selected caching mechanism does not work with views on Search API indexes. Please either use one of the Search API-specific caching options or "None". Caching was turned off for this view.');
      \Drupal::messenger()
        ->addWarning($warning);
      $view
        ->set('display', $displays);
      $view
        ->save();
    }
  }
}