Commit 224d64c6 authored by Spleshka's avatar Spleshka

Issue #1854156 by boyan.borisov, Spleshka: Add support for entity references.

parent 0aeeccd1
This diff is collapsed.
......@@ -58,12 +58,15 @@ define('EXPIRE_NODE_REFERENCE_PAGES' , FALSE);
define('EXPIRE_NODE_CUSTOM' , FALSE);
define('EXPIRE_COMMENT_FRONT_PAGE' , FALSE);
define('EXPIRE_COMMENT_COMMENT_PAGE' , TRUE);
define('EXPIRE_COMMENT_REFERENCE_PAGES' , FALSE);
define('EXPIRE_COMMENT_NODE_PAGE' , TRUE);
define('EXPIRE_COMMENT_NODE_TERM_PAGES' , FALSE);
define('EXPIRE_COMMENT_NODE_REFERENCE_PAGES', FALSE);
define('EXPIRE_COMMENT_CUSTOM' , FALSE);
define('EXPIRE_USER_FRONT_PAGE' , FALSE);
define('EXPIRE_USER_USER_PAGE' , TRUE);
define('EXPIRE_USER_TERM_PAGES' , FALSE);
define('EXPIRE_USER_REFERENCE_PAGES' , FALSE);
define('EXPIRE_USER_CUSTOM' , FALSE);
define('EXPIRE_VOTINGAPI_ENTITY' , FALSE);
define('EXPIRE_VOTINGAPI_ENTITY_PAGE' , FALSE);
......
......@@ -99,27 +99,34 @@ class ExpireAPI {
}
/**
* Find all taxonomy terms in node fields and build urls for them.
* Find all taxonomy terms in the entity fields and build urls for them.
*
* @param $node
* Node object.
* @param $entity
* Entity object.
*
* @param $entity_type
* Type of entity.
*
* @return array
* Term urls that should be flushed.
*/
public static function expireNodeTermPages($node) {
public static function expireTermPages($entity, $entity_type) {
$terms = array();
$field_info = field_info_fields();
$field_instances = field_info_instances('node', $node->type);
list($id, $vid, $bundle_name) = entity_extract_ids($entity_type, $entity);
$field_instances = field_info_instances($entity_type, $bundle_name);
foreach ($field_instances as $field_name => $field_instance) {
// Load information about field.
$field_info = field_info_field($field_name);
foreach ($field_instances as $name => $instance) {
if ($field_info[$name]['type'] == 'taxonomy_term_reference') {
$new_terms = field_get_items('node', $node, $name);
if ($field_info['type'] == 'taxonomy_term_reference') {
$new_terms = field_get_items($entity_type, $entity, $field_name);
if (is_array($new_terms) && !empty($new_terms)) {
$terms = array_merge($new_terms, $terms);
}
$old_terms = isset($node->original) && !empty($node->original) ? field_get_items('node', $node->original, $name) : array();
$old_terms = !empty($entity->original) ? field_get_items($entity_type, $entity->original, $field_name) : array();
if (is_array($old_terms) && !empty($old_terms)) {
$terms = array_merge($old_terms, $terms);
}
......@@ -135,36 +142,84 @@ class ExpireAPI {
}
/**
* Find all node references in node fields and build urls for them.
* Find all entity references in fields and build urls for them.
*
* @param $node
* Node object.
* @param $entity
* Entity object.
*
* @param $entity_type
* Type of entity.
*
* @return array
* Node urls that should be flushed.
* Entity references' urls that should be flushed.
*/
public static function expireNodeReferences($node) {
public static function expireReferences($entity, $entity_type) {
$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);
foreach ($field_instances as $field_name => $field_instance) {
// Load information about field.
$field_info = field_info_field($field_name);
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);
$references = array();
$field_info = field_info_fields();
$field_instances = field_info_instances('node', $node->type);
// Get old values from reference field.
$old_references = !empty($entity->original) ? field_get_items($entity_type, $entity->original, $field_name) : array();
foreach ($field_instances as $name => $instance) {
if ($field_info[$name]['type'] == 'node_reference') {
$new_references = field_get_items('node', $node, $name);
if (is_array($new_references) && !empty($new_references)) {
$references = array_merge($new_references, $references);
$references = array();
if (!empty($new_references) && is_array($new_references)) {
$references = array_merge($references, $new_references);
}
$old_references = isset($node->original) && !empty($node->original) ? field_get_items('node', $node->original, $name) : array();
if (is_array($old_references) && !empty($old_references)) {
$references = array_merge($old_references, $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 ($references as $reference) {
$urls['reference-' . $reference['nid']] = 'node/' . $reference['nid'];
foreach ($field_references as $field_reference) {
// Load entity.
$field_entity = entity_load_single($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'];
}
return $urls;
......
......@@ -83,10 +83,17 @@ class ExpireComment implements ExpireInterface {
$expire_urls = array_merge($expire_urls, $urls);
}
// Expire comment's references.
$expire_references = variable_get('expire_comment_reference_pages' . $variable_suffix, EXPIRE_COMMENT_REFERENCE_PAGES);
if ((module_exists('node_reference') || module_exists('user_reference') || module_exists('entityreference')) && $expire_references) {
$urls = ExpireAPI::expireReferences($comment, 'comment');
$expire_urls = array_merge($expire_urls, $urls);
}
// Expire comment's node references.
$expire_node_references = variable_get('expire_comment_node_reference_pages' . $variable_suffix, EXPIRE_COMMENT_NODE_REFERENCE_PAGES);
if (module_exists('node_reference') && $expire_node_references) {
$urls = ExpireAPI::expireNodeReferences($node);
if ((module_exists('node_reference') || module_exists('user_reference') || module_exists('entityreference')) && $expire_node_references) {
$urls = ExpireAPI::expireReferences($node, 'node');
$expire_urls = array_merge($expire_urls, $urls);
}
......@@ -101,6 +108,7 @@ class ExpireComment implements ExpireInterface {
// Flush page cache for expired urls.
ExpireAPI::executeExpiration($expire_urls, 'comment', $comment);
// Mark cache flushed as true, to avoid duplicate cache flushes in some cases.
$cache_flushed = TRUE;
}
......
......@@ -60,14 +60,14 @@ class ExpireNode implements ExpireInterface {
// Expire taxonomy term pages.
$expire_term_pages = variable_get('expire_node_term_pages' . $variable_suffix, EXPIRE_NODE_TERM_PAGES);
if (module_exists('taxonomy') && $expire_term_pages) {
$urls = ExpireAPI::expireNodeTermPages($node);
$urls = ExpireAPI::expireTermPages($node, 'node');
$expire_urls = array_merge($expire_urls, $urls);
}
// Expire node reference's pages.
$expire_node_references = variable_get('expire_node_reference_pages' . $variable_suffix, EXPIRE_NODE_REFERENCE_PAGES);
if (module_exists('node_reference') && $expire_node_references) {
$urls = ExpireAPI::expireNodeReferences($node);
if ((module_exists('node_reference') || module_exists('user_reference') || module_exists('entityreference')) && $expire_node_references) {
$urls = ExpireAPI::expireReferences($node, 'node');
$expire_urls = array_merge($expire_urls, $urls);
}
......
......@@ -10,7 +10,7 @@ class ExpireUser implements ExpireInterface {
/**
* Executes expiration actions for user.
*
* @param $user
* @param $account
* User object.
*
* @param $action
......@@ -19,16 +19,16 @@ class ExpireUser implements ExpireInterface {
* @param $skip_action_check
* Shows whether should we check executed action or just expire node.
*/
function expire($user, $action, $skip_action_check = FALSE) {
function expire($account, $action, $skip_action_check = FALSE) {
if (empty($user->uid)) {
if (empty($account->uid)) {
return;
}
$enabled_actions = variable_get('expire_user_actions', array());
$enabled_actions = array_filter($enabled_actions);
// Stop further expiration if executed action is not selected by admin.
// Stop further expiration if executed action is not selected by admin.
if (!in_array($action, $enabled_actions) && !$skip_action_check) {
return;
}
......@@ -44,19 +44,33 @@ class ExpireUser implements ExpireInterface {
// Expire user page.
$expire_user_page = variable_get('expire_user_user_page', EXPIRE_USER_USER_PAGE);
if ($expire_user_page) {
$expire_urls['user-' . $user->uid] = 'user/' . $user->uid;
$expire_urls['user-' . $account->uid] = 'user/' . $account->uid;
}
// Expire taxonomy term pages.
$expire_term_pages = variable_get('expire_user_term_pages', EXPIRE_USER_TERM_PAGES);
if (module_exists('taxonomy') && $expire_term_pages) {
$urls = ExpireAPI::expireTermPages($account, 'user');
$expire_urls = array_merge($expire_urls, $urls);
}
// Expire user reference's pages.
$expire_references = variable_get('expire_user_reference_pages', EXPIRE_USER_REFERENCE_PAGES);
if ((module_exists('node_reference') || module_exists('user_reference') || module_exists('entityreference')) && $expire_references) {
$urls = ExpireAPI::expireReferences($account, 'user');
$expire_urls = array_merge($expire_urls, $urls);
}
// Expire custom pages.
$expire_custom = variable_get('expire_user_custom', EXPIRE_USER_CUSTOM);
if ($expire_custom) {
$pages = variable_get('expire_user_custom_pages');
$urls = ExpireAPI::expireCustomPages($pages, array('user' => $user));
$urls = ExpireAPI::expireCustomPages($pages, array('user' => $account));
$expire_urls = array_merge($expire_urls, $urls);
}
// Flush page cache for expired urls.
ExpireAPI::executeExpiration($expire_urls, 'user', $user);
ExpireAPI::executeExpiration($expire_urls, 'user', $account);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment