You are here

save_edit.module in Save & Edit 6

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

File

save_edit.module
View source
<?php

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

/**
 * Implementation of hook_perm().
 */
function save_edit_perm() {
  return array(
    'use save and edit',
    'administer save and edit',
  );
}

/**
 * Implementation of hook_menu().
 */
function save_edit_menu() {
  $items = array();
  $items['admin/settings/save-edit'] = array(
    'title' => t('Save & Edit Settings'),
    'description' => t('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,
  );
  return $items;
}

/*
 * Admin Settings form for Save & Edit
 */
function save_edit_admin_settings() {
  $form['save_edit_this_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Save & Edit General Features'),
    '#description' => t('General settings that will change the usage and/or appearance of the <a href="http://drupal.org/project/save_edit">Save & Edit</a> module.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['save_edit_this_settings']['save_edit_button_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Text to use for Save & Edit button'),
    '#description' => t('This is the default text that will be used for the button at the bottom of the node form.<br />It would be best to use familiar terms like "<strong>Save & Edit</strong>" or "<strong>Apply</strong>" so that users can easily understand the feature/function related to this option.'),
    '#default_value' => variable_get('save_edit_button_value', 'Save & Edit'),
    '#required' => TRUE,
  );
  $form['save_edit_this_settings']['save_edit_button_weight'] = array(
    '#type' => 'weight',
    '#delta' => 10,
    '#description' => t('You may adjust the positioning left to right on the button sections using the weight fields for each button type.'),
    '#title' => t('Save & Edit Button Weight'),
    '#default_value' => variable_get('save_edit_button_weight', 4),
  );
  $form['save_edit_this_settings']['save_edit_default_save_button_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Text to use for default Save button'),
    '#description' => t('This will override the default "Save" button text to something more in line with adding the "Save & Edit" and "Save & Publish" options.'),
    '#default_value' => variable_get('save_edit_default_save_button_value', 'Save'),
    '#required' => TRUE,
  );
  $form['save_edit_this_settings']['save_edit_default_save_button_weight'] = array(
    '#type' => 'weight',
    '#delta' => 10,
    '#description' => t('You may adjust the positioning left to right on the button sections using the weight fields for each button type.'),
    '#title' => t('Default Save Button Weight'),
    '#default_value' => variable_get('save_edit_default_save_button_weight', 5),
  );
  $form['save_edit_this_settings']['save_edit_unpublish'] = array(
    '#type' => 'checkbox',
    '#title' => t('Auto Unpublish All Nodes'),
    '#default_value' => variable_get('save_edit_unpublish', 0),
    '#description' => t('This setting will automatically uncheck the "Published" status when using <strong>Save & Edit</strong> button to save nodes.'),
  );
  $form['save_edit_this_settings']['save_edit_unpublish_new_only'] = array(
    '#type' => 'checkbox',
    '#title' => t('Auto Unpublish on New Nodes Only'),
    '#default_value' => variable_get('save_edit_unpublish_new_only', 0),
    '#description' => t('This will only mark the node as unpublished upon creating a new node. Assuming this is used, on subsequent uses of <strong>Save & Edit</strong> the node will be unpublished already, and NOT affected. You will be required at some point to manually publish the node using the optional <strong>Publish</strong> button, or manually ticking the appropriate checkbox when hitting the default Save button.'),
  );
  $form['save_edit_this_settings']['save_edit_publish_button_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Text to use for Publish button'),
    '#description' => t('This is the default text that will be used for the Publish button. <em>Note: This button will ONLY appear if you have checked either of the above options that manipulate the default publishing actions provided by Drupal.</em>'),
    '#default_value' => variable_get('save_edit_publish_button_value', 'Publish'),
    '#required' => TRUE,
  );
  $form['save_edit_this_settings']['save_edit_publish_button_weight'] = array(
    '#type' => 'weight',
    '#delta' => 10,
    '#description' => t('You may adjust the positioning left to right on the button sections using the weight fields for each button type.'),
    '#title' => t('Publish Button Weight'),
    '#default_value' => variable_get('save_edit_publish_button_weight', 7),
  );
  $form['where_to_save_edit'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node Types'),
    '#description' => t('Set the node types you want to display links for.'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['where_to_save_edit']['save_edit_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Node types'),
    '#default_value' => variable_get('save_edit_node_types', array()),
    '#options' => node_get_types('names'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_form_alter().
 */
function save_edit_form_alter(&$form, $form_state, $form_id) {
  $node_types = variable_get('save_edit_node_types', array());
  $form_type = $form['type']['#value'];
  if ($form['#id'] == 'node-form' && $node_types[$form_type] && user_access('use save and edit')) {

    //add save and edit btn
    $form['buttons']['save_edit'] = array(
      '#type' => 'submit',
      '#access' => user_access('use save and edit'),
      '#value' => t(check_plain(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) {
      $form['buttons']['save_edit_publish'] = array(
        '#type' => 'submit',
        '#access' => user_access('use save and edit'),
        '#value' => t(check_plain(variable_get('save_edit_publish_button_value', 'Save and Publish'))),
        '#weight' => variable_get('save_edit_publish_button_weight', 7),
        '#submit' => array(
          'redirect_save_edit_submit',
        ),
      );
    }

    // this allows us to modify the default Save button to something we like more
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#access' => !variable_get('node_preview', 0) || !form_get_errors() && isset($form_state['node_preview']),
      '#value' => t(check_plain(variable_get('save_edit_default_save_button_value', 'Save'))),
      '#weight' => variable_get('save_edit_default_save_button_weight', 5),
      '#submit' => array(
        'node_form_submit',
      ),
    );

    // declare default handler & then the custom one
    // for some reason without the default drupal node handler, it is
    // completely ignoring the default/external submit handlers.
    // @todo read up on D6 submit handlers and how to simply append this
    // handler to any handler(s) being called to the normal submit function
    $form['buttons']['save_edit']['#submit'] = array(
      'redirect_save_edit_submit',
    );
  }
}

/**
 * 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') {

    // change redirect location
    if ($_REQUEST['destination']) {
      $form_state['redirect'] = url('node/' . $form_state['nid'] . '/edit', array(
        'query' => array(
          'destination' => $_REQUEST['destination'],
        ),
        'absolute' => TRUE,
      ));
      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_admin_settings
save_edit_form_alter Implementation of hook_form_alter().
save_edit_help Provide online user help.
save_edit_menu Implementation of hook_menu().
save_edit_perm Implementation of hook_perm().