You are here

node_noindex.module in Node Noindex 7

Same filename and directory in other branches
  1. 6 node_noindex.module

File

node_noindex.module
View source
<?php

/**
 * Implements hook_help().
 */
function node_noindex_help($path, $arg) {
  switch ($path) {
    case 'admin/help#node_noindex':
      $helptext = t('<p>The <strong>Node noindex</strong> module lets the administrator set the <code>robots</code> metatag in HTML head to <code>noindex</code>.</p>');
      if (function_exists('advanced_help_hint_docs')) {
        $helptext .= '<p>' . advanced_help_hint_docs('node_noindex', 'https://www.drupal.org/docs/7/modules/node-noindex', TRUE) . '</p>';
      }
      return $helptext;
      break;
  }
}

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

/**
 * Implements hook_permission().
 */
function node_noindex_permission() {
  return array(
    'administer node_noindex' => array(
      'title' => t('administer node_noindex'),
      'description' => t('Administer node noindex'),
    ),
  );
}

/**
 * Implements hook_node_load().
 */
function node_noindex_node_load($nodes, $types) {
  $result = db_query('SELECT nid, noindex FROM {node_noindex} WHERE nid IN(:nids)', array(
    ':nids' => array_keys($nodes),
  ));
  foreach ($result as $record) {
    $nodes[$record->nid]->noindex = $record->noindex;
  }
}

/**
 * Implements hook_node_view().
 */
function node_noindex_node_view($node, $view_mode = 'full') {

  // Make sure we are on the node page.
  if (isset($node->noindex) && $node->noindex && $view_mode == 'full' && ($node_mgo = menu_get_object()) && $node->nid == $node_mgo->nid) {
    $element = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'robots',
        'content' => 'noindex',
      ),
    );
    drupal_add_html_head($element, 'node_noindex');
  }
}

/**
 * Helper function: get keys from array matching pattern.
 */
function _node_noindex_preg_grep_keys($pattern, $array, $flags = 0) {
  $keys = array_intersect_key($array, array_flip(preg_grep($pattern, array_keys($array), $flags)));
  return $keys;
}

/**
 * Implements hook_html_head_alter().
 *
 * Remove rel canonical if noindex is set.
 */
function node_noindex_html_head_alter(&$head_elements) {
  $noindex = _node_noindex_preg_grep_keys('/node_noindex/', $head_elements);
  if (empty($noindex)) {

    // Noindex is not set.
    return;
  }
  $keys = _node_noindex_preg_grep_keys('/drupal_add_html_head_link:canonical:/', $head_elements);
  foreach ($keys as $key => $value) {

    // Remove rel canonical.
    unset($head_elements[$key]);
  }
}

/**
 * Implements hook_form_alter().
 *
 * Adds custom fieldset to the node form, and attach ajax behaviour for vertical
 * panels to update the settings description.
 *
 * @see node_noindex.js
 */
function node_noindex_form_alter(&$form, $form_state, $form_id) {
  if (!empty($form['#node_edit_form'])) {
    if (user_access('administer node_noindex') && variable_get('node_noindex_' . $form['type']['#value'], 0)) {
      $node = $form['#node'];

      // Create a fieldset named 'node_noindex' that will be included in the vertical tab.
      $form['node_noindex'] = array(
        '#type' => 'fieldset',
        '#title' => t('Search engine settings'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#tree' => TRUE,
        '#access' => user_access('administer node_noindex'),
        '#weight' => 40,
        '#group' => 'additional_settings',
        '#attached' => array(
          'js' => array(
            'vertical-tabs' => drupal_get_path('module', 'node_noindex') . '/node_noindex.js',
          ),
        ),
      );
      $form['node_noindex']['noindex'] = array(
        '#type' => 'checkbox',
        '#title' => t('Set <code>noindex</code> in HTML head'),
        '#description' => t('If enabled the “robots” meta tag will be set to “noindex” for this node.'),
        '#default_value' => isset($node->noindex) ? $node->noindex : variable_get('node_noindex_default_' . $form['type']['#value'], 0),
        '#weight' => 5,
      );
    }
  }
}

/**
 * Custom validation handler for the content type form.
 */
function node_noindex_ct_validate($form, &$form_state) {
  if (!$form_state['values']['node_noindex_ct']['node_noindex'] && $form_state['values']['node_noindex_ct']['node_noindex_default']) {
    form_set_error('node_noindex_ct][node_noindex_default', t('You cannot set <code>noindex</code> as the default without also enabling the <code>noindex</code> function.'));
  }
}

/**
 * Custom submit handler for the content type form.
 */
function node_noindex_ct_submit($form, &$form_state) {
  variable_set('node_noindex_' . $form_state['values']['type'], $form_state['values']['node_noindex_ct']['node_noindex']);
  variable_set('node_noindex_default_' . $form_state['values']['type'], $form_state['values']['node_noindex_ct']['node_noindex_default']);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alter the content type (ct) form.
 */
function node_noindex_form_node_type_form_alter(&$form, $form_state) {
  $form['#validate'][] = 'node_noindex_ct_validate';
  $form['#submit'][] = 'node_noindex_ct_submit';
  $form['node_noindex_ct'] = array(
    '#type' => 'fieldset',
    '#title' => t('Search engine settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        'vertical-tabs' => drupal_get_path('module', 'node_noindex') . '/node_noindex.js',
      ),
    ),
  );
  $form['node_noindex_ct']['node_noindex'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable noindex option.'),
    '#default_value' => variable_get('node_noindex_' . $form['#node_type']->type, 0),
    '#description' => t('Should this content type display the noindex setting in the node edit form?'),
  );
  $form['node_noindex_ct']['node_noindex_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Set noindex as default'),
    '#default_value' => variable_get('node_noindex_default_' . $form['#node_type']->type, 0),
    '#description' => t('Should this content type display have noindex as default?'),
  );
}

// Hooks for table {node_noindex}.

/**
 * Implements hook_node_insert().
 */
function node_noindex_node_insert($node) {
  _node_noindex_node_set_noindex($node);
}

/**
 * Implements hook_node_update().
 */
function node_noindex_node_update($node) {
  _node_noindex_node_set_noindex($node);
}

/**
 * Implements hook_node_delete().
 */
function node_noindex_node_delete($node) {
  _node_noindex_node_delete_noindex($node);
}

// Helper functions to insert and delete setting in {node_noindex}.

/**
 * Sets or deletes the noindex option on the node.
 *
 * @param
 *   object $node
 */
function _node_noindex_node_set_noindex($node) {

  // If the noindex checkbox is present...
  if (isset($node->node_noindex['noindex'])) {

    // If the noindex checkbox is checked...
    if ($node->node_noindex['noindex']) {

      // Insert/update the noindex value to 1.
      db_merge('node_noindex')
        ->key(array(
        'nid' => $node->nid,
      ))
        ->fields(array(
        'noindex' => 1,
      ))
        ->execute();
    }
    elseif (!$node->is_new) {

      // Delete the noindex value.
      _node_noindex_node_delete_noindex($node);
    }
  }
}

/**
 * Removes the noindex option from the node.
 *
 * @param
 *  object $node
 */
function _node_noindex_node_delete_noindex($node) {
  db_delete('node_noindex')
    ->condition('nid', $node->nid)
    ->execute();
}

Functions

Namesort descending Description
node_noindex_ct_submit Custom submit handler for the content type form.
node_noindex_ct_validate Custom validation handler for the content type form.
node_noindex_form_alter Implements hook_form_alter().
node_noindex_form_node_type_form_alter Implements hook_form_FORM_ID_alter().
node_noindex_help Implements hook_help().
node_noindex_html_head_alter Implements hook_html_head_alter().
node_noindex_node_delete Implements hook_node_delete().
node_noindex_node_insert Implements hook_node_insert().
node_noindex_node_load Implements hook_node_load().
node_noindex_node_update Implements hook_node_update().
node_noindex_node_view Implements hook_node_view().
node_noindex_permission Implements hook_permission().
node_noindex_views_api Implements hook_views_api().
_node_noindex_node_delete_noindex Removes the noindex option from the node.
_node_noindex_node_set_noindex Sets or deletes the noindex option on the node.
_node_noindex_preg_grep_keys Helper function: get keys from array matching pattern.