You are here

save_edit.module in Save & Edit 7

Same filename and directory in other branches
  1. 8 save_edit.module
  2. 6 save_edit.module

Save & Edit (http://drupal.org/project/save_edit) Provides a button that gives users the option to Save a form they are working on, AND return to the editing form in one step.

I find this quite usefull when entering a very long blog post, and my thumb hovering dangerously close to the mouse button that will take me "back" and lose all my entered changes/data.

No Kittens were harmed during the creation of this module.

File

save_edit.module
View source
<?php

// $Id: save_edit.module,v 1.1.2.5 2010/12/10 20:52:21 himerus Exp $

/**
 * @file
 * Save & Edit (http://drupal.org/project/save_edit)
 * Provides a button that gives users the option to 
 * Save a form they are working on, AND return to the
 * editing form in one step.
 * 
 * I find this quite usefull when entering a very long
 * blog post, and my thumb hovering dangerously close to
 * the mouse button that will take me "back" and lose all
 * my entered changes/data.
 * 
 * No Kittens were harmed during the creation of this module.
 */

/**
 * Provide online user help.
 *
 * @param $path
 * @param $arg
 * @return
 *   Help text
 */
function save_edit_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/save-edit':
      return '<p>' . t('Save & Edit adds a "Save and edit" button to the node add and node configure forms. The module also provides options to modify the way the "Publish" feature works when using Save & Edit. If the modifications are enabled to the Publish feature, when a node is unpublished, it will also create a "Publish" button that will obviously Save & Publish a node that was previously marked as Unpublished.') . '</p>';
    case 'admin/help#save_edit':
      return '<p>' . t('Save & Edit adds a "Save and edit" button to the node add and node configure forms. The module also provides options to modify the way the "Publish" feature works when using Save & Edit. If the modifications are enabled to the Publish feature, when a node is unpublished, it will also create a "Publish" button that will obviously Save & Publish a node that was previously marked as Unpublished.') . '</p>';
    default:
      return '';
  }
}

/**
 * Implements hook_permission().
 */
function save_edit_permission() {
  return array(
    'use save and edit' => array(
      'title' => t('Use Save and Edit Button'),
      'description' => t('Display the Save and Edit Button on the specified entity forms.'),
    ),
    'administer save and edit' => array(
      'title' => t('Administer Save and Edit'),
      'description' => t('Change the Save and Edit configurations.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function save_edit_menu() {
  $items = array();
  $items['admin/config/content/save-edit'] = array(
    'title' => 'Save & Edit Settings',
    'description' => 'Administer settings related to the Save & Edit module.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'save_edit_admin_settings',
    ),
    'access arguments' => array(
      'administer save and edit',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'save_edit.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_form_alter().
 */
function save_edit_form_alter(&$form, $form_state, $form_id) {
  $node_types = variable_get('save_edit_node_types', array());

  // if the type is not set, it's not a node and we are not interested
  if (!isset($form['type']['#value'])) {
    return;
  }
  $form_type = $form['type']['#value'];
  if (strstr($form_id, '_node_form') && !empty($node_types[$form_type]) && user_access('use save and edit')) {

    //add save and edit btn
    $form['actions']['save_edit'] = array(
      '#type' => 'submit',
      '#access' => user_access('use save and edit'),
      '#value' => t(variable_get('save_edit_button_value', 'Save and Edit')),
      '#weight' => variable_get('save_edit_button_weight', 4),
      '#submit' => array(
        'redirect_save_edit_submit',
      ),
    );

    // now if we have chosen to use the auto-unpublish feature, we should
    // create a Publish button to add a clear workflow
    if ((variable_get('save_edit_unpublish', 0) || variable_get('save_edit_unpublish_new_only', 0)) && !$form['#node']->status && !variable_get('save_edit_hide_publish', 0)) {
      $form['actions']['save_edit_publish'] = array(
        '#type' => 'submit',
        '#access' => user_access('use save and edit'),
        '#value' => t(variable_get('save_edit_publish_button_value', 'Save & Publish')),
        '#weight' => variable_get('save_edit_publish_button_weight', 7),
        '#submit' => array(
          'redirect_save_edit_submit',
        ),
      );
    }
    if (variable_get('save_edit_hide_default_save', 0)) {

      //unset($form['actions']['submit']);
      $form['actions']['submit']['#access'] = FALSE;
    }
    if (variable_get('save_edit_hide_default_preview', 0)) {

      //unset($form['actions']['preview']);
      $form['actions']['preview']['#access'] = FALSE;
    }
    if (variable_get('save_edit_hide_default_delete', 0)) {

      //unset($form['actions']['delete']);
      $form['actions']['delete']['#access'] = FALSE;
    }

    // this allows us to modify the default Save button to something we like more
    $form['actions']['submit']['#value'] = t(variable_get('save_edit_default_save_button_value', 'Save'));
    $form['actions']['submit']['#weight'] = variable_get('save_edit_default_save_button_weight', 5);
  }
}

/**
 * Custom Submit Handler for Redirecting Save & Edit's to the node form after saving
 *
 * @param $form
 * @param $form_state
 */
function redirect_save_edit_submit($form, &$form_state) {

  // we will first check to see if they want to auto-unpublish, and make modifications if so
  // before submitting the node
  // ONLY do something on new nodes
  if (variable_get('save_edit_unpublish_new_only', 0) && !$form_state['values']['nid']) {
    $form_state['values']['status'] = 0;
  }
  elseif (variable_get('save_edit_unpublish', 0) && !variable_get('save_edit_unpublish_new_only', 0)) {
    $form_state['values']['status'] = 0;
  }

  // WAIT... if someone clicked the Publish button, maybe we should retick that option now
  if ($form_state['clicked_button']['#id'] == 'edit-save-edit-publish') {
    $form_state['values']['status'] = 1;
  }

  // call default node save/submit function
  node_form_submit($form, $form_state);

  // only redirect if using the Save & Edit button
  if ($form_state['clicked_button']['#id'] == 'edit-save-edit' || $form_state['clicked_button']['#id'] == 'edit-actions-save-edit') {

    // change redirect location
    if (isset($_REQUEST['destination'])) {
      $form_state['redirect'] = url('node/' . $form_state['nid'] . '/edit', array(
        'query' => array(
          'destination' => $_REQUEST['destination'],
        ),
        'absolute' => TRUE,
      ));
      $_GET['destination'] = $form_state['redirect'];
      unset($_REQUEST['destination']);
    }
    else {

      // just go back to the form edit page, and dont worry about the redirect
      $form_state['redirect'] = 'node/' . $form_state['nid'] . '/edit';
    }
  }
}

Functions

Namesort descending Description
redirect_save_edit_submit Custom Submit Handler for Redirecting Save & Edit's to the node form after saving
save_edit_form_alter Implements hook_form_alter().
save_edit_help Provide online user help.
save_edit_menu Implements hook_menu().
save_edit_permission Implements hook_permission().