You are here

addanother.module in Add Another 5

Presents users with an option to create another node of the same type after a node is added.

File

addanother.module
View source
<?php

/**
 * @file
 * Presents users with an option to create another node of the same type after a
 * node is added.
 */

/**
 * Implementation of hook_help().
 */
function addanother_help($section) {
  switch ($section) {
    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':
      $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">enable 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>';
  }
}

/**
 * Implementation of hook_perm().
 */
function addanother_perm() {
  return array(
    'enable add another',
  );
}

/**
 * Implementation of hook_menu().
 */
function addanother_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/addanother',
      'title' => 'Add Another',
      'description' => 'Modify which node types display the Add Another message.',
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'addanother_admin',
      ),
      'access' => user_access('access administration pages'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * 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);
}

/**
 * Implementation of hook_nodeapi().
 */
function addanother_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'insert':
      if ($node->op == t('Save and create another')) {

        // This prevents AddAnother's message from clashing with Submit Again.
        return;
      }
      $allowed_nodetypes = variable_get('addanother_nodetypes', array());
      if (user_access('enable add another') && isset($allowed_nodetypes[$node->type]) && $allowed_nodetypes[$node->type]) {
        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),
        ));
      }
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function addanother_form_alter($form_id, &$form) {
  if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) {
    $form['#submit']['_addanother_message'] = array(
      $form,
    );
  }
}

/**
 * Display the Add Another message if set by addanother_nodeapi().
 */
function _addanother_message($form) {
  global $_addanother_message;
  if (isset($_addanother_message)) {
    drupal_set_message($_addanother_message, 'status', FALSE);
  }
}

Functions

Namesort descending Description
addanother_admin This function sets up the admin/settings/addanother settings page.
addanother_form_alter Implementation of hook_form_alter().
addanother_help Implementation of hook_help().
addanother_menu Implementation of hook_menu().
addanother_nodeapi Implementation of hook_nodeapi().
addanother_perm Implementation of hook_perm().
_addanother_message Display the Add Another message if set by addanother_nodeapi().