You are here

auto_nodetitle.module in Automatic Nodetitles 7

Same filename and directory in other branches
  1. 8 auto_nodetitle.module
  2. 5 auto_nodetitle.module
  3. 6 auto_nodetitle.module

Allows hiding of the node title field and automatic title creation.

File

auto_nodetitle.module
View source
<?php

/**
 * @file
 * Allows hiding of the node title field and automatic title creation.
 */
define('AUTO_NODETITLE_DISABLED', 0);
define('AUTO_NODETITLE_ENABLED', 1);
define('AUTO_NODETITLE_OPTIONAL', 2);

/**
 * Implements hook_permission().
 */
function auto_nodetitle_permission() {
  return array(
    'use PHP for title patterns' => array(
      'title' => t('Use PHP for title patterns'),
      'description' => t('Use PHP for title patterns.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_form_alter() for the node form.
 */
function auto_nodetitle_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node_edit_form']) && $form['#node_edit_form'] === TRUE) {
    if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_ENABLED) {

      // We will autogenerate the title later, just hide the title field in the
      // meanwhile.
      $form['title']['#value'] = 'ant';
      $form['title']['#type'] = 'value';
      $form['title']['#required'] = FALSE;
    }
    elseif (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_OPTIONAL) {
      $form['title']['#required'] = FALSE;
    }
  }
}

/**
 * Implements hook_inline_entity_form_entity_form_alter().
 */
function auto_nodetitle_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  if ($entity_form['#entity_type'] == 'node') {
    $setting = auto_nodetitle_get_setting($entity_form['#entity']->type);
    if ($setting == AUTO_NODETITLE_ENABLED) {

      // We will autogenerate the title later, just hide the title field in the
      // meanwhile.
      $entity_form['title']['#value'] = 'ant';
      $entity_form['title']['#type'] = 'value';
      $entity_form['title']['#required'] = FALSE;

      // Fix for title replaced field if title module is active.
      if (module_exists('title') && isset($entity_form['title_field'])) {
        $entity_form['title_field'][$entity_form['title_field']['#language']][0]['value']['#type'] = 'value';
        $entity_form['title_field'][$entity_form['title_field']['#language']][0]['value']['#value'] = 'ant';
        $entity_form['title_field'][$entity_form['title_field']['#language']][0]['value']['#required'] = FALSE;
      }
    }
    elseif ($setting == AUTO_NODETITLE_OPTIONAL) {
      $entity_form['title']['#required'] = FALSE;
      if (module_exists('title') && isset($entity_form['title_field'])) {
        $entity_form['title_field'][$entity_form['title_field']['#language']][0]['value']['#required'] = FALSE;
      }
    }
  }
}

/**
 * Implements hook_node_submit().
 *
 * Generate the node title as soon as the form has been submitted. That way
 * the node preview is shown right too.
 */
function auto_nodetitle_node_submit($node, $form, &$form_state) {
  $setting = auto_nodetitle_get_setting($node->type);
  if ($setting == AUTO_NODETITLE_ENABLED || $setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title'])) {
    $node->auto_nodetitle_applied = TRUE;
    drupal_register_shutdown_function('_auto_nodetitle_save_title');
  }
}

/**
 * Implements hook_node_presave().
 */
function auto_nodetitle_node_presave($node) {

  // If not yet done, generate the title now.
  if (auto_nodetitle_is_needed($node)) {
    auto_nodetitle_set_title($node);
  }
}

/**
 * Helper function to save node title.
 */
function _auto_nodetitle_save_title() {
  $nodes = node_get_recent(1);
  foreach ($nodes as $node) {
    auto_nodetitle_set_title($node);
    db_update('node')
      ->fields(array(
      'title' => $node->title,
      'changed' => REQUEST_TIME,
    ))
      ->condition('nid', $node->nid)
      ->execute();
    db_update('node_revision')
      ->fields(array(
      'title' => $node->title,
      'timestamp' => REQUEST_TIME,
    ))
      ->condition('nid', $node->nid)
      ->execute();
  }
}

/**
 * Returns whether the auto nodetitle has to be set.
 */
function auto_nodetitle_is_needed($node) {
  return empty($node->auto_nodetitle_applied) && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title));
}

/**
 * Sets the automatically generated nodetitle for the node.
 */
function auto_nodetitle_set_title(&$node) {
  $types = node_type_get_types();
  $pattern = variable_get('ant_pattern_' . $node->type, '');
  if (trim($pattern)) {
    $node->changed = REQUEST_TIME;
    $node->title = _auto_nodetitle_patternprocessor($pattern, $node);
  }
  elseif (!empty($node->nid)) {
    $node->title = t('@type @node-id', array(
      '@type' => $types[$node->type]->name,
      '@node-id' => $node->nid,
    ));
  }
  else {
    $node->title = t('@type', array(
      '@type' => $types[$node->type]->name,
    ));
  }

  // Allow other modules to alter node title.
  drupal_alter('auto_nodetitle', $node);

  // Ensure the generated title isn't too long.
  $node->title = truncate_utf8($node->title, 70, TRUE, TRUE);

  // With that flag we ensure we don't apply the title two times to the same
  // node. See auto_nodetitle_is_needed().
  $node->auto_nodetitle_applied = TRUE;
}

/**
 * Implements hook_node_operations().
 */
function auto_nodetitle_node_operations() {
  $operations = array(
    'nodetitle_update' => array(
      'label' => t('Update automatic nodetitles'),
      'callback' => 'auto_nodetitle_operations_update',
    ),
  );
  return $operations;
}

/**
 * Callback function for updating node titles.
 */
function auto_nodetitle_operations_update($nodes) {
  foreach ($nodes as $nid) {
    $node = node_load($nid);
    if ($node && auto_nodetitle_is_needed($node)) {
      $previous_title = $node->title;
      auto_nodetitle_set_title($node);

      // Only save if the title has actually changed.
      if ($node->title != $previous_title) {
        node_save($node);
      }
    }
  }
}

/**
 * Helper function to generate the title according to the settings.
 *
 * @return a title string
 */
function _auto_nodetitle_patternprocessor($pattern, $node) {

  // // Replace tokens.
  // $output = token_replace($pattern, array('node' => $node), array('callback' => '_auto_nodetitle_nohtmlentities', 'sanitize' => FALSE, 'clear' => TRUE));
  // // Evaluate PHP.
  if (variable_get('ant_php_' . $node->type, 0)) {

    // Replace tokens.
    $output = token_replace($pattern, array(
      'node' => $node,
    ), array(
      'sanitize' => FALSE,
      'clear' => TRUE,
      'callback' => '_auto_nodetitle_token_replacement_escape',
    ));

    // Evalute PHP.
    $output = auto_nodetitle_eval($output, $node);
  }
  else {

    // Replace tokens.
    $output = token_replace($pattern, array(
      'node' => $node,
    ), array(
      'sanitize' => FALSE,
      'clear' => TRUE,
    ));
  }

  // Strip tags.
  $output = preg_replace('/[\\t\\n\\r\\0\\x0B]/', '', $output);
  return trim($output);
}

/**
 * Helper function to escape token replacements before evaluating PHP code.
 */
function _auto_nodetitle_token_replacement_escape(&$replacements, $data, $options) {
  foreach ($replacements as &$replacement) {
    $replacement = addslashes($replacement);
  }
}

/**
 * Callback function to remove entities.
 */
function _auto_nodetitle_nohtmlentities(&$replacements, $data, $options) {
  foreach ($replacements as $key => $value) {
    $replacements[$key] = decode_entities($value);
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for the node type form.
 */
function auto_nodetitle_form_node_type_form_alter(&$form, &$form_state) {
  $default_value = auto_nodetitle_get_setting($form['#node_type']->type);
  $form['auto_nodetitle'] = array(
    '#type' => 'fieldset',
    '#title' => t('Automatic title generation'),
    '#weight' => 0,
    '#collapsible' => TRUE,
    '#collapsed' => !$default_value,
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        'auto-nodetitle' => drupal_get_path('module', 'auto_nodetitle') . '/auto_nodetitle.js',
      ),
    ),
  );
  $form['auto_nodetitle']['ant'] = array(
    '#type' => 'radios',
    '#default_value' => $default_value,
    '#options' => array(
      t('Disabled'),
      t('Automatically generate the title and hide the title field'),
      t('Automatically generate the title if the title field is left empty'),
    ),
  );
  $form['auto_nodetitle']['ant_pattern'] = array(
    '#type' => 'textarea',
    '#title' => t('Pattern for the title'),
    '#description' => t('Leave blank for using the per default generated title. Otherwise this string will be used as title. Use the syntax [token] if you want to insert a replacement pattern.'),
    '#default_value' => variable_get('ant_pattern_' . $form['#node_type']->type, ''),
  );

  // Don't allow editing of the pattern if PHP is used, but the users lacks
  // permission for PHP.
  if (variable_get('ant_php_' . $form['#node_type']->type, '') && !user_access('use PHP for title patterns')) {
    $form['auto_nodetitle']['ant_pattern']['#disabled'] = TRUE;
    $form['auto_nodetitle']['ant_pattern']['#description'] = t('You are not allow the configure the pattern for the title, as you lack the %permission permission.', array(
      '%permission' => t('Use PHP for title patterns'),
    ));
  }

  // Display the list of available placeholders if token module is installed.
  if (module_exists('token')) {
    $form['auto_nodetitle']['token_help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'node',
      ),
      '#dialog' => TRUE,
    );
  }
  $form['auto_nodetitle']['ant_php'] = array(
    '#access' => user_access('use PHP for title patterns'),
    '#type' => 'checkbox',
    '#title' => t('Evaluate PHP in pattern.'),
    '#description' => t('Put PHP code above that returns your string, but make sure you surround code in &lt;?php and ?&gt;. Note that $node is available and can be used by your code.'),
    '#default_value' => variable_get('ant_php_' . $form['#node_type']->type, ''),
  );
}

/**
 * Gets the auto node title setting associated with the given content type.
 */
function auto_nodetitle_get_setting($type) {
  return variable_get('ant_' . $type, AUTO_NODETITLE_DISABLED);
}

/**
 * Evaluates php code and passes $node to it.
 */
function auto_nodetitle_eval($code, $node) {
  ob_start();
  print eval('?>' . $code);
  $output = ob_get_contents();
  ob_end_clean();
  return $output;
}

/**
 * Implements hook_node_type_delete().
 */
function auto_nodetitle_node_type_delete($info) {
  variable_del('ant_' . $info->type);
  variable_del('ant_pattern_' . $info->type);
  variable_del('ant_php_' . $info->type);
}

/**
 * Implements hook_node_type_update().
 */
function auto_nodetitle_node_type_update($info) {
  if (!empty($info->old_type) && $info->old_type != $info->type) {
    variable_set('ant_' . $info->type, auto_nodetitle_get_setting($info->old_type));
    variable_set('ant_pattern_' . $info->type, variable_get('ant_pattern_' . $info->old_type, ''));
    variable_set('ant_php_' . $info->type, variable_get('ant_php_' . $info->old_type, ''));
    variable_del('ant_' . $info->old_type);
    variable_del('ant_pattern_' . $info->old_type);
    variable_del('ant_php_' . $info->old_type);
  }
}

/**
 * Implements hook_features_pipe_COMPONENT_alter().
 */
function auto_nodetitle_features_pipe_node_alter(&$pipe, $data, $export) {
  foreach ($data as $type) {
    $pipe['variable'][] = 'ant_' . $type;
    $pipe['variable'][] = 'ant_pattern_' . $type;
    $pipe['variable'][] = 'ant_php_' . $type;
  }
}

Functions

Namesort descending Description
auto_nodetitle_eval Evaluates php code and passes $node to it.
auto_nodetitle_features_pipe_node_alter Implements hook_features_pipe_COMPONENT_alter().
auto_nodetitle_form_alter Implements hook_form_alter() for the node form.
auto_nodetitle_form_node_type_form_alter Implements hook_form_FORM_ID_alter() for the node type form.
auto_nodetitle_get_setting Gets the auto node title setting associated with the given content type.
auto_nodetitle_inline_entity_form_entity_form_alter Implements hook_inline_entity_form_entity_form_alter().
auto_nodetitle_is_needed Returns whether the auto nodetitle has to be set.
auto_nodetitle_node_operations Implements hook_node_operations().
auto_nodetitle_node_presave Implements hook_node_presave().
auto_nodetitle_node_submit Implements hook_node_submit().
auto_nodetitle_node_type_delete Implements hook_node_type_delete().
auto_nodetitle_node_type_update Implements hook_node_type_update().
auto_nodetitle_operations_update Callback function for updating node titles.
auto_nodetitle_permission Implements hook_permission().
auto_nodetitle_set_title Sets the automatically generated nodetitle for the node.
_auto_nodetitle_nohtmlentities Callback function to remove entities.
_auto_nodetitle_patternprocessor Helper function to generate the title according to the settings.
_auto_nodetitle_save_title Helper function to save node title.
_auto_nodetitle_token_replacement_escape Helper function to escape token replacements before evaluating PHP code.

Constants

Namesort descending Description
AUTO_NODETITLE_DISABLED @file Allows hiding of the node title field and automatic title creation.
AUTO_NODETITLE_ENABLED
AUTO_NODETITLE_OPTIONAL