You are here

function apachesolr_attachments_field_attach_update in Apache Solr Attachments 7

Same name and namespace in other branches
  1. 6.3 apachesolr_attachments.module \apachesolr_attachments_field_attach_update()
1 call to apachesolr_attachments_field_attach_update()
apachesolr_attachments_field_attach_insert in ./apachesolr_attachments.module
Hook into the field operations

File

./apachesolr_attachments.module, line 599
Provides a file attachment search implementation for use with the Apache Solr module

Code

function apachesolr_attachments_field_attach_update($parent_entity_type, $parent_entity) {

  // Not all entities sent through this function have the same
  // syntax and/or content. We only check entities that have a type.
  // This excludes comments, so if there would be a file entity atttached to
  // a comment, this would not be picked up.
  // TODO: If you are a entity magician, please improve
  if (isset($parent_entity->type)) {

    // Check if the deleted entity had a file attached
    foreach (field_info_instances($parent_entity_type, $parent_entity->type) as $instance) {
      $field_info = field_info_field($instance['field_name']);
      if ($field_info['type'] == 'file') {

        // Include the file after the if, otherwise it'll get included everywhere
        module_load_include('inc', 'apachesolr_attachments', 'apachesolr_attachments.index');
        $items = field_get_items($parent_entity_type, $parent_entity, $field_info['field_name']);
        if ($items) {
          foreach ($items as $file_info) {
            $file = file_load($file_info['fid']);

            // Discard empty entities
            if (empty($file)) {
              continue;
            }

            // Retrieve parent entity id and add its file usage
            list($parent_entity_id) = entity_extract_ids($parent_entity_type, $parent_entity);
            apachesolr_attachments_add_file_usage($file, $parent_entity_type, $parent_entity_id);
          }
        }
      }
    }
  }
}