You are here

exclude_node_title.module in Exclude Node Title 6

Same filename and directory in other branches
  1. 8 exclude_node_title.module
  2. 7 exclude_node_title.module

Primarily Drupal hooks and global API functions to exclude node titles.

This is the main module file for Exclude Node Title.

File

exclude_node_title.module
View source
<?php

/**
 * @file
 * Primarily Drupal hooks and global API functions to exclude node titles.
 *
 * This is the main module file for Exclude Node Title. 
 */

/**
 * Implementation of hook_perm().
 */
function exclude_node_title_perm() {
  return array(
    'administer exclude node title',
    'exclude any node title',
    'exclude own node title',
    'use exclude node title',
  );
}

/**
 * Implementation of hook_menu().
 */
function exclude_node_title_menu() {
  $items['admin/settings/exclude_node_title'] = array(
    'title' => t('Exclude Node Title from display'),
    'description' => t('Exclude Node Title from display'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'exclude_node_title_admin_settings',
    ),
    'access arguments' => array(
      'administer exclude node title',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'exclude_node_title.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_preprocess_page().
 */
function exclude_node_title_preprocess_page(&$vars, $hook) {
  if (!user_access('use exclude node title')) {

    // user doesn't have access to module
    return;
  }

  // exclude from path node/<nid>
  $nid = FALSE;
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
  }
  elseif (arg(0) == 'search' && variable_get('exclude_node_title_search', 0)) {
    $vars['title'] = '';
    return;
  }
  if ($nid) {
    if (in_array($nid, variable_get('exclude_node_title_nid_list', array()))) {

      // remove title on a per node id basis
      $vars['title'] = '';
    }
    else {

      // remove title on a per node type basis
      if (isset($vars['node']) && is_object($vars['node'])) {
        $node_type = $vars['node']->type;
      }
      elseif (is_numeric($nid)) {
        $node = node_load($nid);
        $node_type = $node->type;
        unset($node);

        // memory cleanup
      }
      $exclude_node_title_content_type = exclude_node_title_content_type();
      if (!empty($exclude_node_title_content_type) && !empty($exclude_node_title_content_type[$node_type])) {
        $vars['title'] = '';
      }
    }
  }
}

/**
 * Set exclude_node_title flag for the given node.
 */
function exclude_node_title_set_flag($node, $value = 1) {
  $exclude_list = variable_get('exclude_node_title_nid_list', array());
  $is_excluded = array_search($node->nid, $exclude_list);
  if ($value == 1 && $is_excluded === FALSE) {
    $exclude_list[] = $node->nid;
    variable_set('exclude_node_title_nid_list', $exclude_list);
    return;
  }
  if ($value == 0 && $is_excluded !== FALSE) {
    unset($exclude_list[$is_excluded]);
    variable_set('exclude_node_title_nid_list', $exclude_list);
    return;
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function exclude_node_title_nodeapi(&$node, $op) {
  switch ($op) {
    case 'view':
      if (variable_get('exclude_node_title_remove_title', 0) == 0 && !(arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) || !user_access('use exclude node title')) {
        return;
      }
      if (in_array($node->nid, variable_get('exclude_node_title_nid_list', array()))) {
        $node->title = '';
      }
      else {
        $exclude_node_title_content_type = exclude_node_title_content_type();
        if (!empty($exclude_node_title_content_type[$node->type])) {
          $node->title = '';
        }
      }
      break;
    case 'delete':
      exclude_node_title_set_flag($node, 0);
      break;
    case 'insert':
    case 'update':
      if (isset($node->exclude_node_title) && exclude_node_title_check_perm($node)) {
        exclude_node_title_set_flag($node, $node->exclude_node_title);
      }
      break;
  }
}

/**
 * Implementation of hook_content_extra_fields().
 */
function exclude_node_title_content_extra_fields($type_name) {

  // @todo Check based on $type_name if we should show the field on this Content-type
  $extras['exclude_node_title'] = array(
    'label' => t('Exclude title from display'),
    'description' => t('Exclude Node Title module form.'),
    'weight' => -4,
  );
  return $extras;
}

/**
 * Check permission to change node title exclusion.
 */
function exclude_node_title_check_perm($node) {
  global $user;
  if (user_access('exclude any node title')) {
    return TRUE;
  }
  if (!user_access('exclude own node title')) {
    return FALSE;
  }
  return !strcmp($node->name, $user->name);
}

/**
 * Implementation of hook_form_alter().
 */
function exclude_node_title_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    if (!exclude_node_title_check_perm($form['#node'])) {
      return FALSE;
    }
    $weight = $form['title']['#weight'] + 0.1;
    $form['exclude_node_title'] = array(
      '#type' => 'checkbox',
      '#title' => t('Exclude title from display'),
      '#required' => FALSE,
      '#default_value' => !empty($form['nid']['#value']) ? in_array($form['nid']['#value'], variable_get('exclude_node_title_nid_list', array())) : FALSE,
      '#weight' => $weight,
    );
    $exclude_node_title_content_type = exclude_node_title_content_type();
    if (!empty($exclude_node_title_content_type[$form['#node']->type])) {
      $form['exclude_node_title']['#disabled'] = TRUE;
      $form['exclude_node_title']['#description'] = t('Title already hidden by content-type in Exclude Node Title !url page.', array(
        '!url' => l(t('settings'), 'admin/settings/exclude_node_title'),
      ));
    }
  }
}

/*
 * Variable getter, using static caching
 */
function exclude_node_title_content_type($reset = FALSE) {
  static $exclude_node_title_content_type;

  // perform static caching of variable
  if (!is_array($exclude_node_title_content_type) || $reset) {
    $exclude_node_title_content_type = variable_get('exclude_node_title_content_type_values', array());
  }
  return $exclude_node_title_content_type;
}

Functions

Namesort descending Description
exclude_node_title_check_perm Check permission to change node title exclusion.
exclude_node_title_content_extra_fields Implementation of hook_content_extra_fields().
exclude_node_title_content_type
exclude_node_title_form_alter Implementation of hook_form_alter().
exclude_node_title_menu Implementation of hook_menu().
exclude_node_title_nodeapi Implementation of hook_nodeapi().
exclude_node_title_perm Implementation of hook_perm().
exclude_node_title_preprocess_page Implementation of hook_preprocess_page().
exclude_node_title_set_flag Set exclude_node_title flag for the given node.