You are here

node_title_help_text.module in Node title help text 6

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

Allows user to add help text to node title per content type.

File

node_title_help_text.module
View source
<?php

/**
 * @file
 * Allows user to add help text to node title per content type.
 */

/**
 * Implements hook_form_alter().
 */
function node_title_help_text_form_alter(&$form, &$form_state, $form_id) {

  // Target node forms only.
  if (substr($form_id, -10) == '_node_form') {

    // Retrieve and add description to node title field on node add/edit page.
    $name = 'node_title_help_text_' . $form['#node']->type . '_title_help';
    $description = variable_get($name, NULL);
    if ($description != '') {
      $form['title']['#description'] = nl2br(filter_xss($description));
    }
  }
}

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

  // Set default value of the help text form element on node type form.
  $name = 'node_title_help_text_' . $form['#node_type']->type . '_title_help';
  $value = variable_get($name, NULL);
  $form['submission']['title_help'] = array(
    '#title' => t('Title field helptext'),
    '#type' => 'textarea',
    '#default_value' => $value,
    '#description' => t('This text will be shown below the title field of this content type.'),
  );
  $form['#submit'][] = 'node_title_help_text_node_type_form_submit';
}

/**
 * Submit handler for node type form. Stores help text in variable.
 */
function node_title_help_text_node_type_form_submit($form_id, &$form_state) {

  // In case of change of machine name, remove the old variable.
  $old_type = $form_state['values']['old_type'];
  $new_type = $form_state['values']['type'];
  if ($old_type != $new_type && $old_type != '') {
    $old_name = 'node_title_help_text_' . $form_state['values']['old_type'] . '_title_help';
    variable_del($old_name);
  }
  $name = 'node_title_help_text_' . $form_state['values']['type'] . '_title_help';
  $value = $form_state['values']['title_help'];
  variable_set($name, $value);
}

/**
 * Implements hook_node_type().
 */
function node_title_help_text_node_type($op, $info) {

  // Delete the variable along with content type.
  if ($op == 'delete') {
    $name = 'node_title_help_text_' . $info->type . '_title_help';
    variable_del($name);

    // Drupal core @link https://www.drupal.org/node/104572 bug. @endlink fix.
    $name = 'title_help_' . $info->type;
    variable_del($name);
  }
}

Functions