View source
<?php
define('NODE_EXPIRE_FORMAT', 'Y-m-d');
define('NODE_EXPIRE_FORMAT_JS', 'yy-mm-dd');
define('NODE_EXPIRE_NO_EXPIRE', 2145934800);
function node_expire_cron() {
$handle_content_expiry = \Drupal::config('node_expire.settings')
->get('node_expire_handle_content_expiry');
if ($handle_content_expiry != 2) {
$result = db_query('SELECT n.nid FROM {node} n
JOIN {node_expire} ne ON n.nid = ne.nid
WHERE ne.expire <= :ne_expire', array(
':ne_expire' => REQUEST_TIME,
));
}
else {
$result = db_query('SELECT n.nid FROM {node} n
JOIN {node_expire} ne ON n.nid = ne.nid
WHERE ne.expire <= :ne_expire AND ne.expired = 0', array(
':ne_expire' => REQUEST_TIME,
));
}
foreach ($result as $record) {
node_expire_set_expired($record->nid);
$node = \Drupal::entityManager()
->getStorage('node')
->load($record->nid);
rules_invoke_event('node_expired', $node);
}
}
function node_expire_menu() {
$items['admin/config/workflow/node_expire/settings'] = array(
'title' => 'Node Expire',
'description' => 'Configure node expire xettings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'node_expire_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'node_expire.admin.inc',
'weight' => 2,
);
return $items;
}
function node_expire_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
if (isset($form['type']) && isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id && ($ntypes = \Drupal::config('node_expire.settings')
->get('node_expire_ntypes')) && isset($ntypes[$form['type']['#value']]) && ($ntype = $ntypes[$form['type']['#value']])) {
module_load_include('nodeapi.inc', 'node_expire');
_node_expire_form_alter_nodeform($ntype, $form, $form_state, $form_id);
}
}
function node_expire_form_node_type_form_alter(&$form, &$form_state) {
if (\Drupal::currentUser()
->hasPermission('administer node expire')) {
$ntypes = \Drupal::config('node_expire.settings')
->get('node_expire_ntypes');
$node_type = $form['#node_type']->type;
$handle_content_expiry = \Drupal::config('node_expire.settings')
->get('node_expire_handle_content_expiry');
if ($handle_content_expiry != 0) {
$form['workflow']['node_expire_enabled'] = array(
'#title' => t('Enable Node Expiry'),
'#description' => t('Allow nodes to expire after a certain amount of time.'),
'#type' => 'checkbox',
'#default_value' => empty($ntypes[$node_type]['enabled']) ? '' : $ntypes[$node_type]['enabled'],
);
$states = array(
'visible' => array(
':input[name="node_expire_enabled"]' => array(
'checked' => TRUE,
),
),
);
$form['workflow']['node_expire_container'] = array(
'#type' => 'fieldset',
'#title' => t('Node Expire'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#states' => $states,
);
$form['workflow']['node_expire_container']['node_expire'] = array(
'#title' => t('Default expiration date'),
'#description' => t('Default date to consider the node expired.') . ' ' . t('Format: PHP <a href="http://www.php.net/strtotime" target="_blank">strtotime format</a>.'),
'#type' => 'textfield',
'#default_value' => empty($ntypes[$node_type]['default']) ? '' : $ntypes[$node_type]['default'],
'#states' => $states,
);
$form['workflow']['node_expire_container']['node_expire_max'] = array(
'#title' => t('Expiration date limit'),
'#description' => t('The max date to consider the node expired.') . ' ' . t('Format: PHP <a href="http://www.php.net/strtotime" target="_blank">strtotime format</a>.') . ' ' . t('Leave it blank if this there is no limit date.'),
'#type' => 'textfield',
'#default_value' => empty($ntypes[$node_type]['max']) ? '' : $ntypes[$node_type]['max'],
'#states' => $states,
);
$form['workflow']['node_expire_container']['node_expire_required'] = array(
'#title' => t('Expiration date required'),
'#type' => 'checkbox',
'#default_value' => !empty($ntypes[$node_type]['required']),
'#states' => $states,
);
}
else {
$form['workflow']['node_expire'] = array(
'#title' => t('Default expiration date'),
'#description' => t('Default date to consider the node expired.') . ' ' . t('Format: PHP <a href="http://www.php.net/strtotime" target="_blank">strtotime format</a>.') . ' ' . t('Leave it blank if this content type never expires.'),
'#type' => 'textfield',
'#default_value' => empty($ntypes[$node_type]['default']) ? '' : $ntypes[$node_type]['default'],
);
$form['workflow']['node_expire_max'] = array(
'#title' => t('Expiration date limit'),
'#description' => t('The max date to consider the node expired.') . ' ' . t('Format: PHP <a href="http://www.php.net/strtotime" target="_blank">strtotime format</a>.') . ' ' . t('Leave it blank if this there is no limit date.'),
'#type' => 'textfield',
'#default_value' => empty($ntypes[$node_type]['max']) ? '' : $ntypes[$node_type]['max'],
);
$form['workflow']['node_expire_required'] = array(
'#title' => t('Expiration date required'),
'#type' => 'checkbox',
'#default_value' => !empty($ntypes[$node_type]['required']),
);
}
module_load_include('ntype.inc', 'node_expire');
$form['#validate'][] = '_node_expire_form_node_type_form_alter_validate';
$form['#submit'][] = '_node_expire_form_node_type_form_alter_submit';
}
}
function node_expire_node_load($nodes, $types) {
module_load_include('nodeapi.inc', 'node_expire');
_node_expire_node_load($nodes, $types);
}
function node_expire_node_prepare_form($node) {
$ntypes = \Drupal::config('node_expire.settings')
->get('node_expire_ntypes');
if (!isset($ntypes[$node->type])) {
return;
}
if (!($ntype = $ntypes[$node->type])) {
return;
}
module_load_include('nodeapi.inc', 'node_expire');
_node_expire_node_prepare($ntype, $node);
}
function node_expire_node_validate(\Drupal\node\NodeInterface $node) {
$ntypes = \Drupal::config('node_expire.settings')
->get('node_expire_ntypes');
if (!isset($ntypes[$node->type])) {
return;
}
if (!($ntype = $ntypes[$node->type])) {
return;
}
module_load_include('nodeapi.inc', 'node_expire');
_node_expire_node_validate($ntype, $node);
}
function node_expire_node_update(\Drupal\node\NodeInterface $node) {
$ntypes = \Drupal::config('node_expire.settings')
->get('node_expire_ntypes');
if (!isset($ntypes[$node->type])) {
return;
}
if (!($ntype = $ntypes[$node->type])) {
return;
}
module_load_include('nodeapi.inc', 'node_expire');
_node_expire_node_update_insert($ntype, $node);
}
function node_expire_node_insert(\Drupal\node\NodeInterface $node) {
$ntypes = \Drupal::config('node_expire.settings')
->get('node_expire_ntypes');
if (!isset($ntypes[$node->type])) {
return;
}
if (!($ntype = $ntypes[$node->type])) {
return;
}
module_load_include('nodeapi.inc', 'node_expire');
_node_expire_node_update_insert($ntype, $node);
}
function node_expire_node_delete(\Drupal\node\NodeInterface $node) {
$ntypes = \Drupal::config('node_expire.settings')
->get('node_expire_ntypes');
if (!isset($ntypes[$node->type])) {
return;
}
if (!($ntype = $ntypes[$node->type])) {
return;
}
module_load_include('nodeapi.inc', 'node_expire');
_node_expire_node_delete($ntype, $node);
}
function node_expire_permission() {
return array(
'administer node expire' => array(
'title' => t('administer node expire'),
'description' => t('TODO Add a description for "administer node expire"'),
),
'edit node expire' => array(
'title' => t('edit node expire'),
'description' => t('TODO Add a description for "edit node expire"'),
),
);
}
function node_expire_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'node_expire'),
);
}
function node_expire_set_expired($nid) {
db_update('node_expire')
->fields(array(
'expired' => 1,
'lastnotify' => REQUEST_TIME,
))
->condition('nid', $nid)
->condition(db_or()
->condition('lastnotify', 0)
->condition('expired', 1, '!='))
->execute();
}
function _node_expire_get_date_entry_elements() {
if (!\Drupal::moduleHandler()
->moduleExists('date_popup')) {
return 0;
}
if (\Drupal::config('node_expire.settings')
->get('node_expire_handle_content_expiry') == 0) {
return 0;
}
return \Drupal::config('node_expire.settings')
->get('node_expire_date_entry_elements');
}
function node_expire_debug($data = array()) {
drupal_set_message(filter_xss_admin('<pre>' . print_r($data, TRUE) . '</pre>'));
\Drupal::logger('node_expire')
->notice('<pre>' . print_r($data, TRUE) . '</pre>', []);
}
module_load_include('ntype.inc', 'node_expire');