You are here

node_expire.module in Node expire 8

Set a timer into your content, allowing you to perform customized actions.

File

node_expire.module
View source
<?php

/**
 * @file
 * Set a timer into your content, allowing you to perform customized actions.
 */
define('NODE_EXPIRE_FORMAT', 'Y-m-d');
define('NODE_EXPIRE_FORMAT_JS', 'yy-mm-dd');

// define('NODE_EXPIRE_COMMON_DEFAULT_DATE', '2038-01-01');
// Will be placed to the "expire" field corresponding empty
// ("never expire") strings. 2145934800 is 2038-01-01.
define('NODE_EXPIRE_NO_EXPIRE', 2145934800);

/**
 * Implements hook_cron().
 */
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,
    ));
  }

  // $nids = array();
  foreach ($result as $record) {

    // $nids[] = $record->nid;
    node_expire_set_expired($record->nid);
    $node = \Drupal::entityManager()
      ->getStorage('node')
      ->load($record->nid);
    rules_invoke_event('node_expired', $node);
  }
}

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Implements hook_form_alter().
 *
 * Adds expiration options to the node entry forms
 */
function node_expire_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {

  // @FIXME
  // Could not extract the default value because it is either indeterminate, or
  // not scalar. You'll need to provide a default value in
  // config/install/node_expire.settings.yml and config/schema/node_expire.schema.yml.
  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);
  }
}

/**
 * Implements hook_form_alter().
 *
 * Enable/Disable expiration feature on node types
 */
function node_expire_form_node_type_form_alter(&$form, &$form_state) {
  if (\Drupal::currentUser()
    ->hasPermission('administer node expire')) {

    // @FIXME
    // Could not extract the default value because it is either indeterminate, or
    // not scalar. You'll need to provide a default value in
    // config/install/node_expire.settings.yml and config/schema/node_expire.schema.yml.
    $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'],
      );

      // Visibility.
      $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,
      );

      // Text fields.
      $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']),
      );
    }

    // Add special validate/submit functions.
    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';
  }
}

/**
 * Implements hook_node_load().
 */
function node_expire_node_load($nodes, $types) {
  module_load_include('nodeapi.inc', 'node_expire');

  // _node_expire_node_load($ntypes, $nodes, $types);
  _node_expire_node_load($nodes, $types);
}

/**
 * Implements hook_node_prepare().
 */
function node_expire_node_prepare_form($node) {

  // Only deal with node types that have the Node expire feature enabled.
  // @FIXME
  // Could not extract the default value because it is either indeterminate, or
  // not scalar. You'll need to provide a default value in
  // config/install/node_expire.settings.yml and config/schema/node_expire.schema.yml.
  $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);
}

/**
 * Implements hook_node_validate().
 */
function node_expire_node_validate(\Drupal\node\NodeInterface $node) {

  // Only deal with node types that have the Node expire feature enabled.
  // @FIXME
  // Could not extract the default value because it is either indeterminate, or
  // not scalar. You'll need to provide a default value in
  // config/install/node_expire.settings.yml and config/schema/node_expire.schema.yml.
  $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);
}

/**
 * Implements hook_nodeapi_update().
 */
function node_expire_node_update(\Drupal\node\NodeInterface $node) {

  // Only deal with node types that have the Node expire feature enabled.
  // @FIXME
  // Could not extract the default value because it is either indeterminate, or
  // not scalar. You'll need to provide a default value in
  // config/install/node_expire.settings.yml and config/schema/node_expire.schema.yml.
  $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);
}

/**
 * Implements hook_nodeapi_insert().
 */
function node_expire_node_insert(\Drupal\node\NodeInterface $node) {

  // Only deal with node types that have the Node expire feature enabled.
  // @FIXME
  // Could not extract the default value because it is either indeterminate, or
  // not scalar. You'll need to provide a default value in
  // config/install/node_expire.settings.yml and config/schema/node_expire.schema.yml.
  $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);
}

/**
 * Implements hook_nodeapi_delete().
 */
function node_expire_node_delete(\Drupal\node\NodeInterface $node) {

  // Only deal with node types that have the Node expire feature enabled.
  // @FIXME
  // Could not extract the default value because it is either indeterminate, or
  // not scalar. You'll need to provide a default value in
  // config/install/node_expire.settings.yml and config/schema/node_expire.schema.yml.
  $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);
}

/**
 * Implements hook_permission().
 */
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"'),
    ),
  );
}

/**
 * Implements hook_views_api().
 */
function node_expire_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'node_expire'),
  );
}

/**
 * Sets expired values.
 *
 * @param int $nid
 *   Node ID that should set the expired flag.
 */
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();
}

/**
 * Gets condition for date entry elements.
 *
 * This depends on value of variable node_expire_date_entry_elements
 * and also is date_popup module of date module installed or not.
 *
 * @return int
 *   0 - use text fields
 *   1 - use date popups
 */
function _node_expire_get_date_entry_elements() {

  // No date_popup module.
  if (!\Drupal::moduleHandler()
    ->moduleExists('date_popup')) {
    return 0;
  }

  // Legacy mode.
  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');
}

/**
 * Utility to display debug info.
 *
 * Display debug info and write it into log.
 *
 * @param object $data
 *   Data to display.
 */
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');

Functions

Namesort descending Description
node_expire_cron Implements hook_cron().
node_expire_debug Utility to display debug info.
node_expire_form_alter Implements hook_form_alter().
node_expire_form_node_type_form_alter Implements hook_form_alter().
node_expire_menu Implements hook_menu().
node_expire_node_delete Implements hook_nodeapi_delete().
node_expire_node_insert Implements hook_nodeapi_insert().
node_expire_node_load Implements hook_node_load().
node_expire_node_prepare_form Implements hook_node_prepare().
node_expire_node_update Implements hook_nodeapi_update().
node_expire_node_validate Implements hook_node_validate().
node_expire_permission Implements hook_permission().
node_expire_set_expired Sets expired values.
node_expire_views_api Implements hook_views_api().
_node_expire_get_date_entry_elements Gets condition for date entry elements.

Constants

Namesort descending Description
NODE_EXPIRE_FORMAT @file Set a timer into your content, allowing you to perform customized actions.
NODE_EXPIRE_FORMAT_JS
NODE_EXPIRE_NO_EXPIRE