You are here

function search_api_search_api_index_update in Search API 7

Same name and namespace in other branches
  1. 8 search_api.module \search_api_search_api_index_update()

Implements hook_search_api_index_update().

1 call to search_api_search_api_index_update()
search_api_search_api_index_insert in ./search_api.module
Implements hook_search_api_index_insert().

File

./search_api.module, line 668
Provides a flexible framework for implementing search services.

Code

function search_api_search_api_index_update(SearchApiIndex $index) {

  // Call the datasource update function with the tables this module provides.
  search_api_index_update_datasource($index, 'search_api_item');
  search_api_index_update_datasource($index, 'search_api_item_string_id');

  // If the server was changed, we have to call the appropriate service class
  // hook methods.
  if ($index->server != $index->original->server) {

    // Server changed - inform old and new ones.
    if ($index->original->server) {
      $old_server = search_api_server_load($index->original->server);

      // The server might have changed because the old one was deleted:
      if ($old_server) {
        $old_server
          ->removeIndex($index);
      }
    }
    if ($index->server) {
      try {
        $new_server = $index
          ->server(TRUE);

        // If the server is enabled, we call addIndex(); otherwise, we save the task.
        $new_server
          ->addIndex($index);
      } catch (SearchApiException $e) {
        watchdog_exception('search_api', $e);

        // If the new server doesn't exist, we remove the index from all
        // servers. Note that saving an entity in its own update hook is usually
        // a recipe for disaster, but since we are only doing this if a server
        // is set and remove the server here before saving, it should be safe
        // enough.
        $index->server = NULL;
        $index
          ->save();
      }
    }

    // We also have to re-index all content.
    _search_api_index_reindex($index);
  }

  // If the fields were changed, call the appropriate service class hook method
  // and re-index the content, if necessary.
  $old_fields = $index->original->options + array(
    'fields' => array(),
  );
  $old_fields = $old_fields['fields'];
  $new_fields = $index->options + array(
    'fields' => array(),
  );
  $new_fields = $new_fields['fields'];
  if ($old_fields != $new_fields) {
    if ($index->server) {
      $index
        ->server()
        ->fieldsUpdated($index);
    }
  }

  // If the index's enabled or read-only status is being changed, queue or
  // dequeue items for indexing.
  if (!$index->read_only && $index->enabled != $index->original->enabled) {
    if ($index->enabled) {
      $index
        ->queueItems();
    }
    else {
      $index
        ->dequeueItems();
    }
  }
  elseif ($index->read_only != $index->original->read_only) {
    if ($index->read_only) {
      $index
        ->dequeueItems();
    }
    else {
      $index
        ->queueItems();
    }
  }
}