phpdrupal-7drupal-modulesdrupal-search

How do I mark content entities as dirty in Drupal 7 Search API module


I am using Drupal 7 with the search api. I understand that one of the 'common pitfalls' of the search api is 'Changes in related entities don't lead to re-indexing'. I am bringing in a field called 'Collection Reference' in my search api index as 'type = content' . So when the title of a collection changes the search API doesn't realise it changes. I have tried to sort this using the rules module as discussed - https://www.drupal.org/docs/7/modules/search-api/getting-started/common-pitfalls#indirect-changes - but I have not been able to get it working. Has anybody had any luck with this technique?


Solution

  • I solved the issue.

    In my case I have photographs in an index and they have a gallery as an entity field and the galleries were not updating if they were modified in the photo index. So grabbed all of the photos relating to that gallery using SQL then put into a dirty ids array. So the code below:

    function hook_entity_presave($entity, $type) {
      if ($entity->type == 'MYCONTENTTYPE') {
        if ($entity->original->title !== $entity->title) {
          $dirty_ids = array();
          $nid = $entity->nid;
          $result = db_query('SELECT g.entity_id FROM {gallery} g WHERE c.id = :nid', array(':nid' => $nid));
    
          foreach($result as $record) {
            $dirty_ids[] = $record->entity_id;
          }
    
          if(!empty($dirty_ids)) {
            search_api_track_item_change('node', $dirty_ids);
          }
        }
      }
    }