public static function ExpireAPI::expireReferences in Cache Expiration 7.2
Find all entity references in fields and build urls for them.
Parameters
object $entity: Entity object.
string $entity_type: Type of entity.
bool $traverse_field_collection: (optional) Whether or not to traverse references from field collections attached to this entity. Defaults to FALSE.
Return value
array Entity references' urls that should be flushed.
4 calls to ExpireAPI::expireReferences()
- ExpireComment::expire in includes/
expire.comment.inc - Executes expiration actions for comment.
- ExpireNode::expire in includes/
expire.node.inc - Executes expiration actions for node.
- ExpireTaxonomyTerm::expire in includes/
expire.taxonomy_term.inc - Executes expiration actions for taxonomy term.
- ExpireUser::expire in includes/
expire.user.inc - Executes expiration actions for user.
File
- includes/
expire.api.inc, line 230 - Provides internal API for page cache flushes.
Class
- ExpireAPI
- @file Provides internal API for page cache flushes.
Code
public static function expireReferences($entity, $entity_type, $traverse_field_collection = FALSE) {
$field_references = array();
list($id, $vid, $bundle_name) = entity_extract_ids($entity_type, $entity);
// Get all fields from current entity type.
$field_instances = field_info_instances($entity_type, $bundle_name);
// Gather references across field_collections.
$field_collection_items = array();
foreach ($field_instances as $field_name => $field_instance) {
// Load information about field.
$field_info = field_info_field($field_name);
// Collect field collection items.
if ($traverse_field_collection && $field_info['type'] == 'field_collection') {
$items = field_get_items($entity_type, $entity, $field_name);
if (!empty($items) && is_array($items)) {
foreach ($items as $item) {
$field_collection_items[] = $item['value'];
}
}
}
if (in_array($field_info['type'], array(
'node_reference',
'user_reference',
'entityreference',
))) {
if (in_array($field_info['type'], array(
'node_reference',
'user_reference',
))) {
$field_value_key = $field_info['type'] == 'node_reference' ? 'nid' : 'uid';
$field_entity_type = $field_info['type'] == 'node_reference' ? 'node' : 'user';
}
else {
$field_value_key = 'target_id';
$field_entity_type = $field_info['settings']['target_type'];
}
// Get new values from reference field.
$new_references = field_get_items($entity_type, $entity, $field_name);
// Get old values from reference field.
$old_references = !empty($entity->original) ? field_get_items($entity_type, $entity->original, $field_name) : array();
$references = array();
if (!empty($new_references) && is_array($new_references)) {
$references = array_merge($references, $new_references);
}
if (!empty($old_references) && is_array($old_references)) {
$references = array_merge($references, $old_references);
}
foreach ($references as $reference) {
$field_entity_id = isset($reference[$field_value_key]) ? $reference[$field_value_key] : FALSE;
$field_references[$field_entity_type . $field_entity_id] = array(
'entity_type' => $field_entity_type,
'entity_id' => $field_entity_id,
);
}
}
}
// Collect urls of referenced entities.
$urls = array();
foreach ($field_references as $field_reference) {
// Load entity.
$field_entity = _expire_load_single_entity($field_reference['entity_type'], $field_reference['entity_id']);
if (empty($field_entity)) {
continue;
}
// Get entity uri;
$field_entity_uri = entity_uri($field_reference['entity_type'], $field_entity);
if (empty($field_entity_uri['path'])) {
continue;
}
$urls['reference-' . $field_reference['entity_type'] . '-' . $field_reference['entity_id']] = $field_entity_uri['path'];
}
// Traverse gathered field collections.
if ($field_collection_items) {
foreach (field_collection_item_load_multiple($field_collection_items) as $field_collection_item) {
$field_collection_urls = ExpireAPI::expireReferences($field_collection_item, 'field_collection_item');
$urls = array_merge($urls, $field_collection_urls);
}
}
return $urls;
}