You are here

function apachesolr_attachments_add_file_usage in Apache Solr Attachments 7

Same name and namespace in other branches
  1. 6.3 apachesolr_attachments.index.inc \apachesolr_attachments_add_file_usage()

Records that a parent entity is using a file.

Parameters

$file: A file object.

$module: The name of the module using the file.

$type: The type of the object that contains the referenced file.

$id: The unique, numeric ID of the object containing the referenced file.

$count: (optional) The number of references to add to the object. Defaults to 1.

3 calls to apachesolr_attachments_add_file_usage()
apachesolr_attachments_field_attach_delete in ./apachesolr_attachments.module
apachesolr_attachments_field_attach_update in ./apachesolr_attachments.module
apachesolr_attachments_solr_reindex in ./apachesolr_attachments.module
Reindexing callback for ApacheSolr, for file entities.

File

./apachesolr_attachments.index.inc, line 198
Indexing-related functions.

Code

function apachesolr_attachments_add_file_usage(stdClass $stub_file, $parent_entity_type, $parent_entity_id) {

  // Only add this file type if the parent entity type can be indexed.
  // Example : node is mostly indexed but media module is not. So
  // exclude all media entities from being added
  $entity_info = entity_get_info($parent_entity_type);
  if (!empty($entity_info['apachesolr']['indexable'])) {

    // We do have to load the file, because there is no way to get the
    // bundle type, and media adds many bundles, so fixing this here
    $file = file_load($stub_file->fid);

    // Test if file is correctly loaded. Otherwise, return immediately.
    if (!$file) {
      return;
    }
    $indexer_table = apachesolr_get_indexer_table('file');

    // For non-media files there is no such thing as a defined type/bundle
    // Define it here, so we can have a seamless integration between media and
    // non-media
    if (empty($file->type)) {
      $file->type = 'file';
    }
    db_merge($indexer_table)
      ->key(array(
      'entity_type' => 'file',
      'entity_id' => $file->fid,
      'parent_entity_type' => $parent_entity_type,
      'parent_entity_id' => $parent_entity_id,
    ))
      ->fields(array(
      'bundle' => $file->type,
      'status' => $file->status,
      'changed' => REQUEST_TIME,
    ))
      ->execute();
  }
}