You are here

exclude_node_title.module in Exclude Node Title 7

Same filename and directory in other branches
  1. 8 exclude_node_title.module
  2. 6 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.
 */

/**
 * Implements hook_permission().
 */
function exclude_node_title_permission() {
  return array(
    'administer exclude node title' => array(
      'title' => t('Administer exclude node title'),
    ),
    'exclude any node title' => array(
      'title' => t('Exclude any node title'),
    ),
    'exclude own node title' => array(
      'title' => t('Exclude own node title'),
    ),
    'use exclude node title' => array(
      'title' => t('View nodes without titles per Exclude Node Title settings'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function exclude_node_title_menu() {
  $items = array();
  $items['admin/config/content/exclude_node_title'] = array(
    'title' => 'Exclude Node Title',
    'description' => 'Configure which nodes and view modes can exclude the display of 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;
}

/**
 * Determine whether a user has privilege and whether to exclude the node title.
 *
 * @param object $node
 *   The node object.
 * @param string $view_mode
 *   The view mode, e.g. 'full' or 'teaser'.
 *
 * @return bool
 *   Returns TRUE if title should be hidden, otherwise returns FALSE.
 */
function exclude_node_title($node, $view_mode) {
  return user_access('use exclude node title') && _exclude_node_title($node, $view_mode);
}

/**
 * Implements hook_preprocess_overlay().
 */
function exclude_node_title_preprocess_overlay(&$vars) {
  exclude_node_title_preprocess_page($vars);
}

/**
 * Implements hook_preprocess_page().
 */
function exclude_node_title_preprocess_page(&$vars) {
  if (!user_access('use exclude node title') || arg(0) == 'node' && arg(1) == 'add') {
    return;
  }
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    switch (arg(2)) {
      case 'edit':
        $view_mode = 'nodeform';
        break;
      case 'delete':
        return;
      default:
        $view_mode = 'full';
        break;
    }
    _exclude_node_title_preprocess($vars, arg(1), $view_mode);
  }
  elseif (isset($vars['page']['content']['system_main']['#node_edit_form']) && $vars['page']['content']['system_main']['#node_edit_form'] == TRUE) {
    _exclude_node_title_preprocess($vars, $vars['page']['content']['system_main']['#node'], 'nodeform');
  }
}

/**
 * Implements hook_preprocess_node().
 */
function exclude_node_title_preprocess_node(&$vars) {
  if (user_access('use exclude node title')) {
    _exclude_node_title_preprocess($vars, $vars['node'], $vars['view_mode']);
  }
}

/**
 * Remove the title from the variables array.
 */
function _exclude_node_title_preprocess(&$vars, $node, $view_mode) {
  if (_exclude_node_title($node, $view_mode)) {
    $hide_using_css = variable_get('exclude_node_title_hide_using_css', 0);
    if ($hide_using_css) {
      $vars['exclude_node_title_class'] = drupal_html_class('element-hidden');
    }
    else {
      $vars['title'] = '';
    }
    if ($view_mode == 'nodeform') {

      // Prevent access to the title field on nodeform view mode.
      $vars['page']['content']['system_main']['title']['#access'] = FALSE;
    }
  }
}

/**
 * Implements hook_preprocess_search_result().
 */
function exclude_node_title_preprocess_search_result(&$vars) {
  if (user_access('use exclude node title') && variable_get('exclude_node_title_search', 0)) {
    $hide_using_css = variable_get('exclude_node_title_hide_using_css', 0);
    if ($hide_using_css) {
      $vars['exclude_node_title_class'] = drupal_html_class('element-hidden');
    }
    else {
      $vars['title'] = '';
    }
  }
}

/**
 * Implements hook_node_update().
 */
function exclude_node_title_node_update($node) {
  if (isset($node->exclude_node_title) && exclude_node_title_check_perm($node)) {
    exclude_node_title_set_flag($node, $node->exclude_node_title);
  }
}

/**
 * Implements hook_node_insert().
 */
function exclude_node_title_node_insert($node) {
  if (isset($node->exclude_node_title) && exclude_node_title_check_perm($node)) {
    exclude_node_title_set_flag($node, $node->exclude_node_title);
  }
}

/**
 * Implements hook_node_delete().
 */
function exclude_node_title_node_delete($node) {
  if (isset($node->exclude_node_title) && $node->exclude_node_title == TRUE) {
    exclude_node_title_set_flag($node, 0);
  }
}

/**
 * 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);
}

/**
 * Implements 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) {

    // Exclude the node title for privileged users.
    if (user_access('use exclude node title')) {
      if (_exclude_node_title($form['#node'], 'nodeform')) {
        drupal_set_title('');
      }
    }

    // Prevent users without permission from accessing the node title settings.
    if (!exclude_node_title_check_perm($form['#node'])) {
      return FALSE;
    }

    // If the user chose to hide the title using CSS, add the form element only if the default theme supports the CSS method.
    if (variable_get('exclude_node_title_hide_using_css', 0)) {
      $theme_default = variable_get('theme_default', 'bartik');
      $supported_themes = variable_get('exclude_node_title_themes_supporting_css', array());
      if (!in_array($theme_default, $supported_themes)) {
        return FALSE;
      }
    }

    // Add form element only for enabled content types.
    if (variable_get('exclude_node_title_content_type_value_' . $form['#node']->type) == 'user') {
      $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,
      );
      if (module_exists('translation') && variable_get('exclude_node_title_translation_sync', TRUE) == TRUE && translation_supported_type($form['#node']->type) && !empty($form['nid']['#value'])) {

        // Get the translation source node ID.
        $tnid = db_select('node', 'n')
          ->fields('n', array(
          'tnid',
        ))
          ->condition('nid', $form['nid']['#value'])
          ->execute()
          ->fetchAssoc();
        if ($tnid['tnid'] != $form['nid']['#value']) {
          $form['exclude_node_title']['#description'] = t("Check !here if you don't have title disabled in the source language of this node.", array(
            '!here' => l(t('here'), 'node/' . $tnid['tnid'] . '/edit'),
          ));
        }
      }
    }
  }
  return FALSE;
}

/**
 * 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;
  }
}

/**
 * Implements hook_field_attach_delete_bundle().
 */
function exclude_node_title_field_attach_delete_bundle($entity_type, $bundle, $instances) {

  // Remove variables when a content type is deleted.
  if ($entity_type == 'node') {
    variable_del('exclude_node_title_content_type_value_' . $bundle);
    variable_del('exclude_node_title_content_type_modes_' . $bundle);
  }
}

/**
 * Determine whether the node title should be excluded.
 *
 * @param object $param
 *   Contains the node object.
 * @param string $view_mode
 *   The view mode, e.g. 'full' or 'teaser'.
 *
 * @return bool
 *   Returns TRUE if the title should be hidden, otherwise returns FALSE.
 */
function _exclude_node_title($param, $view_mode = 'full') {
  if (!($node_info = _exclude_node_title_get_node($param))) {
    return FALSE;
  }

  // Get exclude settings.
  static $exclude_settings;
  if (!isset($exclude_settings)) {
    foreach (_node_types_build()->names as $key => $val) {
      $exclude_settings[$key] = array(
        'type' => variable_get('exclude_node_title_content_type_value_' . $key, 'none'),
        'modes' => variable_get('exclude_node_title_content_type_modes_' . $key, array()),
      );
    }
  }
  if (!isset($exclude_settings[$node_info['node_type']]['type'])) {
    return FALSE;
  }
  switch ($exclude_settings[$node_info['node_type']]['type']) {
    case 'all':
      return !empty($exclude_settings[$node_info['node_type']]['modes'][$view_mode]);
    case 'user':
      if (!$node_info['nid']) {
        return FALSE;
      }

      // Prepare a list of the node IDs to be excluded.
      $nid_exclude_list = variable_get('exclude_node_title_nid_list', array());
      $nid_list = array(
        $node_info['nid'] => $node_info['nid'],
      );
      if (module_exists('translation') && variable_get('exclude_node_title_translation_sync', TRUE) == TRUE && translation_supported_type($node_info['node_type'])) {

        // Get the translation source node ID.
        $tnid = db_select('node', 'n')
          ->fields('n', array(
          'tnid',
        ))
          ->condition('nid', $node_info['nid'])
          ->execute()
          ->fetchAssoc();
        $tlist = translation_node_get_translations($tnid['tnid']);
        if (is_array($tlist)) {
          foreach ($tlist as $tlang => $tnode) {
            $nid_list[$tnode->nid] = $tnode->nid;
          }
        }
      }
      foreach ($nid_list as $item_nid) {
        if (in_array($item_nid, $nid_exclude_list)) {
          return !empty($exclude_settings[$node_info['node_type']]['modes'][$view_mode]);
        }
      }
      return FALSE;
    case 'none':
    default:
      return FALSE;
  }
}

/**
 * Helper function that extracts node information from $param.
 *
 * @param int|object $param
 *   Can be a node object or integer value (nid).
 *
 * @return mixed
 *   Returns an array with node id and node type, or FALSE if there were errors.
 */
function _exclude_node_title_get_node($param) {

  // Do not load nodes unless we have objects and integers.
  if (!is_object($param) && !is_numeric($param)) {
    return FALSE;
  }

  // Load the node by its ID if we have numeric value.
  if (is_numeric($param)) {
    $node = node_load(intval($param));
    if (!is_object($node)) {
      return FALSE;
    }
  }
  elseif (is_object($param)) {
    $node = $param;
    unset($param);
  }

  // Do not get node IDs for non-existing nodes.
  if (!isset($node) || !isset($node->type)) {
    return FALSE;
  }
  $node_type = $node->type;
  $nid = isset($node->nid) ? $node->nid : FALSE;
  unset($node);
  return array(
    'nid' => $nid,
    'node_type' => $node_type,
  );
}

/**
 * Implements hook_field_extra_fields().
 */
function exclude_node_title_field_extra_fields() {
  $extra = array();
  foreach (node_type_get_types() as $type) {
    $exclude_type = variable_get('exclude_node_title_content_type_value_' . $type->type, 'none');
    if ($exclude_type != 'user') {
      continue;
    }
    $extra['node'][$type->type]['form'] = array(
      'exclude_node_title' => array(
        'label' => t('Exclude Node Title'),
        'description' => t('Exclude node title from display.'),
        'weight' => 0,
        'visible' => TRUE,
      ),
    );
  }
  return $extra;
}

/**
 * Implements hook_ds_fields_info_alter().
 */
function exclude_node_title_ds_fields_info_alter(&$fields, $entity_type) {
  if ($entity_type == 'node') {
    $fields['title']['function'] = '_exclude_node_title_ds_render_field';
    $fields['title']['properties']['settings']['exclude node title settings'] = array(
      'type' => 'select',
      'options' => array(
        'No',
        'Yes',
      ),
      'description' => t('Use the settings for the Exclude Node Title module for the title. Set to "off" to always show title.'),
    );
    $fields['title']['properties']['default']['exclude node title settings'] = 1;
  }
}

/**
 * Render the field obeying exclude node title settings.
 */
function _exclude_node_title_ds_render_field($field) {
  $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : array();
  $settings += $field['properties']['default'];
  if ($settings['exclude node title settings'] && _exclude_node_title($field['entity']->nid, $field['view_mode'])) {
    $field['entity']->title = '';
  }
  return ds_render_field($field);
}

Functions

Namesort descending Description
exclude_node_title Determine whether a user has privilege and whether to exclude the node title.
exclude_node_title_check_perm Check permission to change node title exclusion.
exclude_node_title_ds_fields_info_alter Implements hook_ds_fields_info_alter().
exclude_node_title_field_attach_delete_bundle Implements hook_field_attach_delete_bundle().
exclude_node_title_field_extra_fields Implements hook_field_extra_fields().
exclude_node_title_form_alter Implements hook_form_alter().
exclude_node_title_menu Implements hook_menu().
exclude_node_title_node_delete Implements hook_node_delete().
exclude_node_title_node_insert Implements hook_node_insert().
exclude_node_title_node_update Implements hook_node_update().
exclude_node_title_permission Implements hook_permission().
exclude_node_title_preprocess_node Implements hook_preprocess_node().
exclude_node_title_preprocess_overlay Implements hook_preprocess_overlay().
exclude_node_title_preprocess_page Implements hook_preprocess_page().
exclude_node_title_preprocess_search_result Implements hook_preprocess_search_result().
exclude_node_title_set_flag Set exclude_node_title flag for the given node.
_exclude_node_title Determine whether the node title should be excluded.
_exclude_node_title_ds_render_field Render the field obeying exclude node title settings.
_exclude_node_title_get_node Helper function that extracts node information from $param.
_exclude_node_title_preprocess Remove the title from the variables array.