You are here

termstatus.module in Taxonomy Term Status 7

Hook implementations and API for the taxonomy term status module.

File

termstatus.module
View source
<?php

/**
 * @file
 * Hook implementations and API for the taxonomy term status module.
 */

/**
 * Implements hook_help().
 */
function termstatus_help($path, $arg) {
  switch ($path) {

    // Main module help for the block module.
    case 'admin/help#termstatus':
      return '<p>' . t('Provides a checkbox on the term edit form in order to set the publishing state.') . '</p>';
  }
}

/**
 * Implements hook_menu().
 */
function termstatus_menu() {
  $items['admin/config/system/termstatus'] = array(
    'title' => 'Taxonomy Term Status',
    'description' => 'Control and rebuild taxonomy term status records',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'termstatus_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'termstatus.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function termstatus_permission() {
  $perms = array(
    'view unpublished taxonomy terms' => array(
      'title' => t('View unpublished taxonomy terms'),
    ),
  );
  return $perms;
}

/**
 * Implements hook_views_api().
 */
function termstatus_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Implements hook_action_info().
 */
function termstatus_action_info() {
  return array(
    'termstatus_publish_action' => array(
      'type' => 'taxonomy_term',
      'label' => t('Publish taxonomy term'),
      'configurable' => FALSE,
      'behavior' => array(
        'changes_property',
      ),
      'triggers' => array(
        'taxonomy_term_presave',
        'taxonomy_term_insert',
        'taxonomy_term_update',
        'taxonomy_term_delete',
      ),
    ),
    'termstatus_unpublish_action' => array(
      'type' => 'taxonomy_term',
      'label' => t('Unpublish taxonomy term'),
      'configurable' => FALSE,
      'behavior' => array(
        'changes_property',
      ),
      'triggers' => array(
        'taxonomy_term_presave',
        'taxonomy_term_insert',
        'taxonomy_term_update',
        'taxonomy_term_delete',
      ),
    ),
  );
}

/**
 * Implements hook_entity_property_info_alter().
 */
function termstatus_entity_property_info_alter(&$entity_info) {
  $entity_info['taxonomy_term']['properties']['status'] = array(
    'label' => t("Status"),
    'description' => t("Whether the taxonomy term is published or unpublished."),
    'setter callback' => 'entity_property_verbatim_set',
    'type' => 'integer',
    'options list' => 'entity_metadata_status_options_list',
    'access permission' => 'administer taxonomy',
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function termstatus_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {

  // Do not show termstatus settings on delete confirmation form.
  if (!empty($form_state['confirm_delete'])) {
    return;
  }
  $term = $form_state['term'];
  $form['termstatus'] = array(
    '#type' => 'fieldset',
    '#title' => t('Publishing options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => 95,
  );
  $form['termstatus']['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Published'),
    '#default_value' => termstatus_term_getstatus($term),
    '#description' => t('Only privileged users are allowed to view a taxonomy term if this option is disabled'),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function termstatus_form_taxonomy_form_vocabulary_alter(&$form, &$form_state, $form_id) {
  $vocabulary = $form_state['vocabulary'];
  $form['termstatus'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default publishing options'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => 95,
  );
  $form['termstatus']['termstatus_default_status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Published'),
    '#default_value' => variable_get('termstatus_default_' . $vocabulary->machine_name, TRUE),
    '#description' => t('Users with the <em>Administer vocabularies and terms</em> permission will be able to override this option for each term.'),
  );
  $form['#submit'][] = 'termstatus_form_taxonomy_form_vocabulary_submit';
}

/**
 * Form submit handler for taxonomy vocabulary form.
 */
function termstatus_form_taxonomy_form_vocabulary_submit($form, &$form_state) {
  $vocabulary = $form_state['vocabulary'];
  variable_set('termstatus_default_' . $vocabulary->machine_name, $form_state['values']['termstatus_default_status']);
}

/**
 * Implemens hook_preprocess_HOOK().
 */
function termstatus_preprocess_taxonomy_term(&$variables) {
  if (isset($variables['term']->status) && empty($variables['term']->status)) {
    $variables['classes_array'][] = 'term-unpublished';
  }
}

/**
 * Implements hook_taxonomy_term_load().
 */
function termstatus_taxonomy_term_load($terms) {
  foreach ($terms as $term) {
    $term->status = termstatus_term_getstatus($term);
  }
  $result = db_query('SELECT tid, status FROM {termstatus} WHERE tid IN (:tids)', array(
    ':tids' => array_keys($terms),
  ));
  foreach ($result as $record) {
    $terms[$record->tid]->status = $record->status;
  }
}

/**
 * Implements hook_taxonomy_term_insert().
 */
function termstatus_taxonomy_term_insert($term) {
  termstatus_term_save($term);
}

/**
 * Implements hook_taxonomy_term_update().
 */
function termstatus_taxonomy_term_update($term) {
  termstatus_term_save($term);
}

/**
 * Implements hook_taxonomy_term_delete().
 */
function termstatus_taxonomy_term_delete($term) {
  termstatus_term_delete($term->tid);
}

/**
 * Implements hook_query_TAG_alter().
 */
function termstatus_query_term_access_alter(QueryAlterableInterface $query) {
  $tables =& $query
    ->getTables();
  if (variable_get('termstatus_enable', FALSE) && !user_access('administer taxonomy') && !user_access('view unpublished taxonomy terms') && !$query
    ->hasTag('status_unpublished')) {

    // Extract taxonomy_term_data tables.
    foreach ($tables as $alias => $table) {
      if ($table['table'] == 'taxonomy_term_data') {

        // Add one inner join to termstatus for each taxonomy_term_data
        // instance.
        $query
          ->leftjoin('termstatus', 'termstatus_' . $alias, $alias . '.tid = termstatus_' . $alias . '.tid');
        $query
          ->condition('termstatus_' . $alias . '.status', 1, '=');
      }
    }
  }
}

/**
 * Return true if the given account has access to the specified term.
 */
function termstatus_term_access($term, $account = NULL) {
  if (variable_get('termstatus_enable', FALSE) && !user_access('administer taxonomy', $account) && !user_access('view unpublished taxonomy terms', $account)) {
    $result = db_query('SELECT status FROM {termstatus} where tid = :tid', array(
      ':tid' => $term->tid,
    ));
    return $result
      ->fetchField();
  }
  else {
    return user_access('access content', $account);
  }
}

/**
 * Implements hook_menu_alter().
 *
 * Inject a menu access callback for taxonomy term pages.
 */
function termstatus_menu_alter(&$items) {
  $orig_callback = isset($items['taxonomy/term/%taxonomy_term']['access callback']) ? $items['taxonomy/term/%taxonomy_term']['access callback'] : 'user_access';
  $orig_arguments = $items['taxonomy/term/%taxonomy_term']['access arguments'];
  $items['taxonomy/term/%taxonomy_term']['access callback'] = 'termstatus_menu_access';
  $items['taxonomy/term/%taxonomy_term']['access arguments'] = array(
    2,
    $orig_callback,
    $orig_arguments,
  );
}

/**
 * Menu access callback. Return true if access to term is granted.
 */
function termstatus_menu_access($term, $orig_callback, $orig_arguments) {
  if ($orig_callback && !call_user_func_array($orig_callback, $orig_arguments)) {
    return FALSE;
  }
  if (variable_get('termstatus_enable', FALSE) && !user_access('administer taxonomy') && !user_access('view unpublished taxonomy terms')) {
    return !empty($term->status);
  }
  return TRUE;
}

/**
 * Action callback: publish a term.
 */
function termstatus_publish_action($term, $context = array()) {
  $term->status = 1;
  watchdog('action', 'Set taxonomy term %title to published.', array(
    '%title' => $term->name,
  ));
}

/**
 * Action callback: unpublish a term.
 */
function termstatus_unpublish_action($term, $context = array()) {
  $term->status = 0;
  watchdog('action', 'Set taxonomy term %title to unpublished.', array(
    '%title' => $term->name,
  ));
}

/**
 * Save the status record of a term.
 */
function termstatus_term_save($term) {
  $status = termstatus_term_getstatus($term);
  db_merge('termstatus')
    ->key(array(
    'tid' => $term->tid,
  ))
    ->fields(array(
    'status' => $status,
  ))
    ->execute();
}

/**
 * Remove the status record of a term.
 */
function termstatus_term_delete($tid) {
  db_delete('termstatus')
    ->condition('tid', $tid)
    ->execute();
}

/**
 * Return the term status of a given term, defaulting to the vocabulary.
 */
function termstatus_term_getstatus($term) {
  return isset($term->status) ? $term->status : variable_get('termstatus_default_' . $term->vocabulary_machine_name, TRUE);
}

Functions

Namesort descending Description
termstatus_action_info Implements hook_action_info().
termstatus_entity_property_info_alter Implements hook_entity_property_info_alter().
termstatus_form_taxonomy_form_term_alter Implements hook_form_FORM_ID_alter().
termstatus_form_taxonomy_form_vocabulary_alter Implements hook_form_FORM_ID_alter().
termstatus_form_taxonomy_form_vocabulary_submit Form submit handler for taxonomy vocabulary form.
termstatus_help Implements hook_help().
termstatus_menu Implements hook_menu().
termstatus_menu_access Menu access callback. Return true if access to term is granted.
termstatus_menu_alter Implements hook_menu_alter().
termstatus_permission Implements hook_permission().
termstatus_preprocess_taxonomy_term Implemens hook_preprocess_HOOK().
termstatus_publish_action Action callback: publish a term.
termstatus_query_term_access_alter Implements hook_query_TAG_alter().
termstatus_taxonomy_term_delete Implements hook_taxonomy_term_delete().
termstatus_taxonomy_term_insert Implements hook_taxonomy_term_insert().
termstatus_taxonomy_term_load Implements hook_taxonomy_term_load().
termstatus_taxonomy_term_update Implements hook_taxonomy_term_update().
termstatus_term_access Return true if the given account has access to the specified term.
termstatus_term_delete Remove the status record of a term.
termstatus_term_getstatus Return the term status of a given term, defaulting to the vocabulary.
termstatus_term_save Save the status record of a term.
termstatus_unpublish_action Action callback: unpublish a term.
termstatus_views_api Implements hook_views_api().