View source
<?php
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;
}
}
function node_noindex_views_api() {
return array(
'api' => '3.0',
);
}
function node_noindex_permission() {
return array(
'administer node_noindex' => array(
'title' => t('administer node_noindex'),
'description' => t('Administer node noindex'),
),
);
}
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;
}
}
function node_noindex_node_view($node, $view_mode = 'full') {
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');
}
}
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;
}
function node_noindex_html_head_alter(&$head_elements) {
$noindex = _node_noindex_preg_grep_keys('/node_noindex/', $head_elements);
if (empty($noindex)) {
return;
}
$keys = _node_noindex_preg_grep_keys('/drupal_add_html_head_link:canonical:/', $head_elements);
foreach ($keys as $key => $value) {
unset($head_elements[$key]);
}
}
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'];
$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,
);
}
}
}
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.'));
}
}
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']);
}
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?'),
);
}
function node_noindex_node_insert($node) {
_node_noindex_node_set_noindex($node);
}
function node_noindex_node_update($node) {
_node_noindex_node_set_noindex($node);
}
function node_noindex_node_delete($node) {
_node_noindex_node_delete_noindex($node);
}
function _node_noindex_node_set_noindex($node) {
if (isset($node->node_noindex['noindex'])) {
if ($node->node_noindex['noindex']) {
db_merge('node_noindex')
->key(array(
'nid' => $node->nid,
))
->fields(array(
'noindex' => 1,
))
->execute();
}
elseif (!$node->is_new) {
_node_noindex_node_delete_noindex($node);
}
}
}
function _node_noindex_node_delete_noindex($node) {
db_delete('node_noindex')
->condition('nid', $node->nid)
->execute();
}