Skip to content
addanother.module 3.3 KiB
Newer Older
<?php
// $Id$
Robin Monks's avatar
Robin Monks committed

 * @file
 * Presents users with an option to create another node of the same type after a
 * node is added.
 */
Robin Monks's avatar
Robin Monks committed

/**
 * Implementation of hook_help().
 */
function addanother_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#addanother":
      $output = '<p>'.  t("Presents users with an option to create another node of the same type after a node is added.") .'</p>';
      return $output;
    case 'admin/settings/addanother':
Robin Monks's avatar
Robin Monks committed
      $output = 'Here you can select the content types for which an <em>"Add another..."</em> message will be displayed. After creating a new content node, users with the <a href="@addanother_perm">use add another</a> permission will receive a message allowing them to add another content node of the same type.';
      return '<p>'. t($output, array(
        '@addanother_perm' => url('admin/user/permissions', array('fragment' => 'module-addanother')),
      )) .'</p>';
Robin Monks's avatar
Robin Monks committed

 * Implementation of hook_perm().
 */
function addanother_perm() {
Robin Monks's avatar
Robin Monks committed
  return array('administer add another', 'use add another');
Robin Monks's avatar
Robin Monks committed

 * Implementation of hook_menu().
 */
function addanother_menu() {
  $items = array();
  $items['admin/settings/addanother'] = array(
    'title' => 'Add Another',
    'description' => 'Modify which node types display the Add Another message.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('addanother_admin'),
Robin Monks's avatar
Robin Monks committed
    'access arguments' => array('administer add another'),
    'type' => MENU_NORMAL_ITEM,
  return $items;
}
Robin Monks's avatar
Robin Monks committed

 * This function sets up the admin/settings/addanother settings page.
 */
function addanother_admin() {
  $form['addanother_nodetypes'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enable Add Another for these content types'),
    '#options' => node_get_types('names'),
    '#default_value' => variable_get('addanother_nodetypes', array()),
    '#description' => 'An <em>Add Another</em> message will be shown after creating these content types',
  );
  return system_settings_form($form);
}
Robin Monks's avatar
Robin Monks committed

 * Implementation of hook_nodeapi().
 */
function addanother_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
Robin Monks's avatar
Robin Monks committed
      if ($node->op == t('Save and create another')) {
Robin Monks's avatar
Robin Monks committed
        // This prevents AddAnother's message from clashing with Submit Again.
        return;
      }
      $allowed_nodetypes = variable_get('addanother_nodetypes', array());
Robin Monks's avatar
Robin Monks committed
      if (user_access('use add another') && isset($allowed_nodetypes[$node->type]) && $allowed_nodetypes[$node->type]) {
Robin Monks's avatar
Robin Monks committed
        global $_addanother_message;
        $_addanother_message = t('Add another <a href="@typeurl">%type</a>.', array(
          '@typeurl' => url('node/add/'. str_replace('_', '-', $node->type)),
          '%type' => node_get_types('name', $node)
Robin Monks's avatar
Robin Monks committed
          ));
Robin Monks's avatar
Robin Monks committed

/**
 * Implementation of hook_form_alter().
Robin Monks's avatar
Robin Monks committed
 */
Robin Monks's avatar
Robin Monks committed
function addanother_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
    $form['buttons']['submit']['#submit'][] = '_addanother_message';
  }
}
/**
 * Display the Add Another message if set by addanother_nodeapi().
Robin Monks's avatar
Robin Monks committed
 */
Robin Monks's avatar
Robin Monks committed
function _addanother_message($form, &$form_state) {
  global $_addanother_message;
  if (isset($_addanother_message)) {
    drupal_set_message($_addanother_message, 'status', FALSE);
  }
}