Commit 0bacb207 authored by leonnk's avatar leonnk Committed by Spleshka

Issue #2201913 by joelpittet, leon.nk, mukhsim, dberror, Spleshka: Add support for terms expire

parent af2f57b2
......@@ -193,13 +193,124 @@ function expire_admin_settings_form() {
);
// TAXONOMY TERM SETTINGS.
if (module_exists('taxonomy')) {
$form['tabs']['taxonomy_term'] = array(
'#type' => 'fieldset',
'#title' => t('Taxonomy term expiration'),
'#group' => 'tabs',
'#weight' => 2,
);
$form['tabs']['taxonomy_term']['override'] = array(
'#type' => 'fieldset',
'#title' => t('Taxonomy term settings per vocabulary'),
);
$form['tabs']['taxonomy_term']['override']['info'] = array(
'#type' => 'item',
'#markup' => t('Please note that you may override this settings for each vocabulary on the vocabulary configuration page.'),
);
$form['tabs']['taxonomy_term']['actions'] = array(
'#type' => 'fieldset',
'#title' => t('Taxonomy term actions'),
);
$form['tabs']['taxonomy_term']['actions']['expire_taxonomy_term_actions'] = array(
'#type' => 'checkboxes',
'#description' => t('Page cache for taxonomy term will be flushed after selected actions.'),
'#options' => array(
EXPIRE_TAXONOMY_TERM_INSERT => t('Taxonomy term insert'),
EXPIRE_TAXONOMY_TERM_UPDATE => t('Taxonomy term update'),
EXPIRE_TAXONOMY_TERM_DELETE => t('Taxonomy term delete'),
),
'#default_value' => variable_get('expire_taxonomy_term_actions', array()),
);
$form['tabs']['taxonomy_term']['expire'] = array(
'#type' => 'fieldset',
'#title' => t('What URLs should be expired when taxonomy term action is triggered?'),
);
$form['tabs']['taxonomy_term']['expire']['expire_taxonomy_term_front_page'] = array(
'#type' => 'checkbox',
'#title' => t('Front page'),
'#description' => t('Expire url of the site front page'),
'#default_value' => variable_get('expire_taxonomy_term_front_page', EXPIRE_TAXONOMY_TERM_FRONT_PAGE),
);
$form['tabs']['taxonomy_term']['expire']['expire_taxonomy_term_taxonomy_term_page'] = array(
'#type' => 'checkbox',
'#title' => t('Taxonomy term page'),
'#description' => t('Expire url of the expiring taxonomy term.'),
'#default_value' => variable_get('expire_taxonomy_term_taxonomy_term_page', EXPIRE_TAXONOMY_TERM_TAXONOMY_TERM_PAGE),
);
if (module_exists('node_reference') || module_exists('user_reference') || module_exists('entityreference')) {
$form['tabs']['taxonomy_term']['expire']['expire_taxonomy_term_reference_pages'] = array(
'#type' => 'checkbox',
'#title' => t('Taxonomy term reference pages'),
'#description' => t('Expire urls of entities which are referenced from the expiring taxonomy term.'),
'#default_value' => variable_get('expire_taxonomy_term_reference_pages', EXPIRE_TAXONOMY_TERM_REFERENCE_PAGES),
);
if (module_exists('field_collection')) {
$form['tabs']['taxonomy_term']['expire']['expire_taxonomy_term_reference_field_collection_pages'] = array(
'#type' => 'checkbox',
'#title' => t('Traverse references attached to field collections'),
'#description' => t('Expire urls of entities which are referenced from field collections attached to the expiring taxonomy_term.'),
'#default_value' => variable_get('expire_taxonomy_term_reference_field_collection_pages', EXPIRE_TAXONOMY_TERM_REFERENCE_FC_PAGES),
'#states' => array(
'visible' => array(
':input[name="expire_taxonomy_term_reference_pages"]' => array('checked' => TRUE),
),
),
);
}
}
$form['tabs']['taxonomy_term']['expire']['expire_taxonomy_term_custom'] = array(
'#type' => 'checkbox',
'#title' => t('Custom pages'),
'#description' => t('Expire user-defined custom urls.'),
'#default_value' => variable_get('expire_taxonomy_term_custom', EXPIRE_TAXONOMY_TERM_CUSTOM),
);
$form['tabs']['taxonomy_term']['expire']['expire_taxonomy_term_custom_pages'] = array(
'#type' => 'textarea',
'#title' => t('Enter custom URLs'),
'#description' => t('Enter one relative URL per line. It can be the path of a taxonomy term (e.g. !example1) or of any alias (e.g. !example2). However, it has to be the final URL, not a redirect (use the !link1 and !link2 modules).', array(
'!example1' => '<strong>taxonomy/term/[taxonomy_term:tid]</strong>',
'!example2' => '<strong>my/path</strong>',
'!link1' => l('Global Redirect', 'https://drupal.org/project/globalredirect'),
'!link2' => l('Redirect', 'https://drupal.org/project/redirect')
)) . '<br/>'
. t('If you want to match a path with any ending, add "|wildcard" to the end of the line (see !link1 for details). Example: !example1 will match !example1a, but also !example1b, !example1c, etc.', array(
'!link1' => l('function cache_clear_all', 'https://api.drupal.org/api/drupal/includes%21cache.inc/function/cache_clear_all/7'),
'!example1' => '<strong>my/path|wildcard</strong>',
'!example1a' => '<strong>my/path</strong>',
'!example1b' => '<strong>my/path/one</strong>',
'!example1c' => '<strong>my/path/two</strong>'
)) . '<br/>'
. t('You may use tokens here.'),
'#states' => array(
'visible' => array(
':input[name="expire_taxonomy_term_custom"]' => array('checked' => TRUE),
),
),
'#default_value' => variable_get('expire_taxonomy_term_custom_pages'),
);
}
// COMMENT SETTINGS.
if (module_exists('comment')) {
$form['tabs']['comment'] = array(
'#type' => 'fieldset',
'#title' => t('Comment expiration'),
'#group' => 'tabs',
'#weight' => 2,
'#weight' => 3,
);
$form['tabs']['comment']['override'] = array(
......@@ -325,7 +436,7 @@ function expire_admin_settings_form() {
'#type' => 'fieldset',
'#title' => t('User expiration'),
'#group' => 'tabs',
'#weight' => 3,
'#weight' => 4,
);
$form['tabs']['user']['actions'] = array(
......@@ -424,7 +535,7 @@ function expire_admin_settings_form() {
'#type' => 'fieldset',
'#title' => t('File expiration'),
'#group' => 'tabs',
'#weight' => 4,
'#weight' => 5,
);
$form['tabs']['file']['actions'] = array(
......@@ -490,7 +601,7 @@ function expire_admin_settings_form() {
'#type' => 'fieldset',
'#title' => t('Votingapi expiration'),
'#group' => 'tabs',
'#weight' => 5,
'#weight' => 6,
);
$form['tabs']['votingapi']['actions'] = array(
......@@ -556,7 +667,7 @@ function expire_admin_settings_form() {
'#type' => 'fieldset',
'#title' => t('Menu links expiration'),
'#group' => 'tabs',
'#weight' => 6,
'#weight' => 7,
);
$form['tabs']['menu_links']['actions'] = array(
......@@ -764,3 +875,75 @@ function expire_node_settings_form(&$form) {
// Attach js.
$form['#attached']['js'][] = drupal_get_path('module', 'expire') . '/js/expire.admin.js';
}
/**
* Settings form for a taxonomy vocabulary.
*/
function expire_taxonomy_term_settings_form(&$form) {
if (empty($form['#vocabulary']->machine_name)) {
return;
}
$vocabulary = $form['#vocabulary']->machine_name;
$form['expire'] = array(
'#type' => 'fieldset',
'#title' => t('Cache Expiration'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
// TAXONOMY TERM SETTINGS.
$form['expire']['expire_taxonomy_term_override_defaults'] = array(
'#type' => 'checkbox',
'#title' => t('Override default settings for this vocabulary'),
'#default_value' => variable_get('expire_taxonomy_term_override_defaults_' . $vocabulary),
);
// Copy here settings from default settings form.
$settings_form = expire_admin_settings_form();
$form['expire']['taxonomy_term_actions'] = $settings_form['tabs']['taxonomy_term']['actions'];
$form['expire']['taxonomy_term_expires'] = $settings_form['tabs']['taxonomy_term']['expire'];
// Display fieldsets with settings only if they should be overriden.
$form['expire']['taxonomy_term_actions']['#states'] = array(
'visible' => array(':input[name="expire_taxonomy_term_override_defaults"]' => array('checked' => TRUE)),
);
$form['expire']['taxonomy_term_expires']['#states'] = array(
'visible' => array(':input[name="expire_taxonomy_term_override_defaults"]' => array('checked' => TRUE)),
);
// Change tab title to more suitable here.
$form['expire']['taxonomy_term_expires']['#title'] = t('What pages should be expired when taxonomy term action is triggered?');
// Set default settings for this vocabulary.
$form['expire']['taxonomy_term_actions']['expire_taxonomy_term_actions']['#default_value'] =
variable_get('expire_taxonomy_term_actions_' . $vocabulary, array());
$form['expire']['taxonomy_term_expires']['expire_taxonomy_term_front_page']['#default_value'] =
variable_get('expire_taxonomy_term_front_page_' . $vocabulary, EXPIRE_TAXONOMY_TERM_FRONT_PAGE);
$form['expire']['taxonomy_term_expires']['expire_taxonomy_term_taxonomy_term_page']['#default_value'] =
variable_get('expire_taxonomy_term_taxonomy_term_page_' . $vocabulary, EXPIRE_TAXONOMY_TERM_TAXONOMY_TERM_PAGE);
if (module_exists('node_reference') || module_exists('user_reference') || module_exists('entityreference')) {
$form['expire']['taxonomy_term_expires']['expire_taxonomy_term_reference_pages']['#default_value'] =
variable_get('expire_taxonomy_term_reference_pages_' . $vocabulary, EXPIRE_TAXONOMY_TERM_REFERENCE_PAGES);
if (module_exists('field_collection')) {
$form['expire']['taxonomy_term_expires']['expire_taxonomy_term_reference_field_collection_pages']['#default_value'] =
variable_get('expire_taxonomy_term_reference_field_collection_pages_' . $vocabulary, EXPIRE_TAXONOMY_TERM_REFERENCE_FC_PAGES);
}
}
$form['expire']['taxonomy_term_expires']['expire_taxonomy_term_custom']['#default_value'] =
variable_get('expire_taxonomy_term_custom_' . $vocabulary, EXPIRE_TAXONOMY_TERM_CUSTOM);
$form['expire']['taxonomy_term_expires']['expire_taxonomy_term_custom_pages']['#default_value'] =
variable_get('expire_taxonomy_term_custom_pages_' . $vocabulary);
// Attach js.
$form['#attached']['js'][] = drupal_get_path('module', 'expire') . '/js/expire.admin.js';
}
......@@ -56,6 +56,21 @@ function expire_drush_command() {
'callback' => 'drush_expire_node'
);
// Expire taxonomy term objects.
$items['expire-term'] = array(
'description' => 'Expire a taxonomy term by term ID.',
'arguments' => array(
'tids' => 'Numeric term ids to expire separated by spaces.',
),
'examples' => array(
'drush expire-term 2' => 'Expire single taxonomy term by term ID.',
'drush xp-term 2 24 612' => 'Expire multiple taxonomy terms by term IDs.',
),
'aliases' => array('xp-term'),
'drupal dependencies' => array('expire'),
'callback' => 'drush_expire_term',
);
// Expire user objects.
$items['expire-user'] = array(
'description' => 'Expire a user by user ID.',
......@@ -132,6 +147,13 @@ function drush_expire_node() {
_drush_expire_entity('node');
}
/**
* Callback for expire-term drush command.
*/
function drush_expire_term() {
_drush_expire_entity('taxonomy_term');
}
/**
* Callback for expire-user drush command.
*/
......
......@@ -11,6 +11,7 @@ files[] = includes/expire.domain.inc
files[] = includes/expire.interface.inc
files[] = includes/expire.menu_link.inc
files[] = includes/expire.node.inc
files[] = includes/expire.taxonomy_term.inc
files[] = includes/expire.user.inc
files[] = includes/expire.votingapi.inc
files[] = includes/expire.file.inc
......@@ -26,6 +26,11 @@ define('EXPIRE_NODE_INSERT', 1);
define('EXPIRE_NODE_UPDATE', 2);
define('EXPIRE_NODE_DELETE', 3);
// Taxonomy term actions.
define('EXPIRE_TAXONOMY_TERM_INSERT', 1);
define('EXPIRE_TAXONOMY_TERM_UPDATE', 2);
define('EXPIRE_TAXONOMY_TERM_DELETE', 3);
// Comment actions.
define('EXPIRE_COMMENT_INSERT' , 1);
define('EXPIRE_COMMENT_UPDATE' , 2);
......@@ -64,6 +69,12 @@ define('EXPIRE_NODE_TERM_PAGES' , FALSE);
define('EXPIRE_NODE_REFERENCE_PAGES' , FALSE);
define('EXPIRE_NODE_REFERENCE_FC_PAGES' , FALSE);
define('EXPIRE_NODE_CUSTOM' , FALSE);
define('EXPIRE_TAXONOMY_TERM_FRONT_PAGE' , FALSE);
define('EXPIRE_TAXONOMY_TERM_TAXONOMY_TERM_PAGE', TRUE);
define('EXPIRE_TAXONOMY_TERM_TERM_PAGES' , FALSE);
define('EXPIRE_TAXONOMY_TERM_REFERENCE_PAGES' , FALSE);
define('EXPIRE_TAXONOMY_TERM_REFERENCE_FC_PAGES', FALSE);
define('EXPIRE_TAXONOMY_TERM_CUSTOM' , FALSE);
define('EXPIRE_COMMENT_FRONT_PAGE' , FALSE);
define('EXPIRE_COMMENT_COMMENT_PAGE' , TRUE);
define('EXPIRE_COMMENT_REFERENCE_PAGES' , FALSE);
......@@ -132,6 +143,35 @@ function expire_node_delete($node) {
expire_execute_expiration('node', $node, EXPIRE_NODE_DELETE);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function expire_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
module_load_include('admin.inc', 'expire');
expire_taxonomy_term_settings_form($form, $form_state);
}
/**
* Implements hook_taxonomy_term_insert().
*/
function expire_taxonomy_term_insert($taxonomy_term) {
expire_execute_expiration('taxonomy_term', $taxonomy_term, EXPIRE_TAXONOMY_TERM_INSERT);
}
/**
* Implements hook_taxonomy_term_update().
*/
function expire_taxonomy_term_update($taxonomy_term) {
expire_execute_expiration('taxonomy_term', $taxonomy_term, EXPIRE_TAXONOMY_TERM_UPDATE);
}
/**
* Implements hook_taxonomy_term_delete().
*/
function expire_taxonomy_term_delete($taxonomy_term) {
expire_execute_expiration('taxonomy_term', $taxonomy_term, EXPIRE_TAXONOMY_TERM_DELETE);
}
/**
* Implements hook_comment_insert().
*/
......
<?php
/**
* @file
* Provides class that expires taxonomy terms.
*/
class ExpireTaxonomyTerm implements ExpireInterface {
/**
* Executes expiration actions for taxonomy term.
*
* @param $taxonomy_term
* Taxonomy term object.
*
* @param $action
* Action that has been executed.
*
* @param $skip_action_check
* Shows whether should we check executed action or just
* expire taxonomy term.
*/
function expire($taxonomy_term, $action, $skip_action_check = FALSE) {
if (empty($taxonomy_term->tid) || empty($taxonomy_term->vocabulary_machine_name)) {
return;
}
// See if cache settings was overridden for this
// taxonomy_term vocabulary_machine_name.
$settings_overridden = variable_get('expire_taxonomy_term_override_defaults_' . $taxonomy_term->vocabulary_machine_name);
$variable_suffix = '';
if (!empty($settings_overridden)) {
// If page cache settings was overridden for this taxonomy_term
// vocabulary_machine_name we should add
// "[taxonomy_term-VOCABULARY_MACHINE_NAME]" to every variable name
// we use here.
$variable_suffix = '_' . $taxonomy_term->vocabulary_machine_name;
}
$enabled_actions = variable_get('expire_taxonomy_term_actions' . $variable_suffix, array());
$enabled_actions = array_filter($enabled_actions);
// Stop further expiration if executed action is not selected by admin.
if (!in_array($action, $enabled_actions) && !$skip_action_check) {
return;
}
$expire_urls = array();
// Expire front page.
$expire_front_page = variable_get('expire_taxonomy_term_front_page' . $variable_suffix, EXPIRE_TAXONOMY_TERM_FRONT_PAGE);
if ($expire_front_page) {
$expire_urls = ExpireAPI::getFrontPageUrls();
}
// Expire taxonomy_term page.
$expire_taxonomy_term_page = variable_get('expire_taxonomy_term_taxonomy_term_page' . $variable_suffix, EXPIRE_TAXONOMY_TERM_TAXONOMY_TERM_PAGE);
if ($expire_taxonomy_term_page) {
$expire_urls['taxonomy_term-' . $taxonomy_term->tid] = 'taxonomy/term/' . $taxonomy_term->tid;
}
// Expire taxonomy_term reference's pages.
$expire_taxonomy_term_references = variable_get('expire_taxonomy_term_reference_pages' . $variable_suffix, EXPIRE_TAXONOMY_TERM_REFERENCE_PAGES);
if ($expire_taxonomy_term_references) {
$urls = ExpireAPI::expireTermPages($taxonomy_term, 'taxonomy_term');
$expire_urls = array_merge($expire_urls, $urls);
if (module_exists('node_reference') || module_exists('user_reference') || module_exists('entityreference')) {
$traverse_field_collection = module_exists('field_collection') && variable_get('expire_taxonomy_term_reference_field_collection_pages' . $variable_suffix, EXPIRE_TAXONOMY_TERM_REFERENCE_FC_PAGES);
$urls = ExpireAPI::expireReferences($taxonomy_term, 'taxonomy_term', $traverse_field_collection);
$expire_urls = array_merge($expire_urls, $urls);
}
}
// Expire custom pages.
$expire_custom = variable_get('expire_taxonomy_term_custom' . $variable_suffix, EXPIRE_TAXONOMY_TERM_CUSTOM);
if ($expire_custom) {
$pages = variable_get('expire_taxonomy_term_custom_pages' . $variable_suffix);
$urls = ExpireAPI::expireCustomPages($pages, array('taxonomy_term' => $taxonomy_term));
$expire_urls = array_merge($expire_urls, $urls);
}
// Flush page cache for expired urls.
ExpireAPI::executeExpiration($expire_urls, 'taxonomy_term', $taxonomy_term);
}
}
......@@ -6,11 +6,13 @@
$('fieldset#edit-expire', context).drupalSetSummary(function(context) {
var vals = [];
if ($('#edit-expire-node-override-defaults', context).is(':checked')) {
vals.push(Drupal.t('Node expiration: settings are overriden'));
}
else {
vals.push(Drupal.t('Node expiration: default settings'));
if ($('#edit-expire-node-override-defaults', context).length) {
if ($('#edit-expire-node-override-defaults', context).is(':checked')) {
vals.push(Drupal.t('Node expiration: settings are overriden'));
}
else {
vals.push(Drupal.t('Node expiration: default settings'));
}
}
if ($('#edit-expire-comment-override-defaults', context).length) {
......@@ -22,6 +24,15 @@
}
}
if ($('#edit-expire-taxonomy-term-override-defaults', context).length) {
if ($('#edit-expire-taxonomy-term-override-defaults', context).is(':checked')) {
vals.push(Drupal.t('Taxonomy term expiration: settings are overriden'));
}
else {
vals.push(Drupal.t('Taxonomy term expiration: default settings'));
}
}
return vals.join(', ');
});
......
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