View source
<?php
define('SAVE_DRAFT_DISABLED', 0);
define('SAVE_DRAFT_ENABLED', 1);
function save_draft_permission() {
return array(
'save draft' => array(
'title' => t('Save content as draft'),
'description' => t('Allows people with permission to view and edit their own unpublished content to change the published state of their content when saving it.'),
),
);
}
function save_draft_form_node_form_alter(&$form, &$form_state) {
if (variable_get('save_draft_enabled_' . $form['#node']->type, SAVE_DRAFT_ENABLED) && save_draft_access($form, $form_state)) {
if (isset($form['options']['status'])) {
$form['options']['status']['#access'] = FALSE;
$form['options']['#attributes']['class'] = array(
'node-promotion-options',
);
$form['options']['#attached'] = array(
'js' => array(
'vertical-tabs' => drupal_get_path('module', 'save_draft') . '/save_draft.js',
),
);
}
if (empty($form['#validate'])) {
$form['#validate'] = array();
}
array_unshift($form['#validate'], 'save_draft_validate');
$form['actions']['draft'] = array(
'#type' => 'submit',
'#class' => 'form-submit',
'#submit' => array(
'save_draft_draft_button_submit',
),
'#weight' => 9,
);
if (!isset($form_state['node']->nid)) {
$form['actions']['submit']['#value'] = t('Publish');
$form['actions']['draft']['#value'] = t('Save as draft');
}
elseif (!$form_state['node']->status) {
$form['actions']['submit']['#value'] = t('Publish');
$form['actions']['draft']['#value'] = t('Save as draft');
$form['actions']['draft']['#weight'] = 0;
}
else {
$form['actions']['submit']['#value'] = t('Save');
$form['actions']['draft']['#value'] = t('Unpublish');
}
if (variable_get('save_draft_skip_required_' . $form['#node']->type, FALSE)) {
$form['actions']['draft']['#skip_required_validation'] = TRUE;
if (isset($form['actions']['preview'])) {
$form['actions']['preview']['#skip_required_validation'] = TRUE;
}
if (isset($form['actions']['delete'])) {
$form['actions']['delete']['#skip_required_validation'] = TRUE;
}
$form['#after_build'][] = 'save_draft_form_after_build';
}
}
}
function save_draft_draft_button_submit($form, &$form_state) {
if (variable_get('save_draft_show_message_' . $form['#node']->type, FALSE)) {
if ($form_state['triggering_element']['#value'] == $form['actions']['draft']['#value']) {
drupal_set_message(t('Saved as draft.'));
}
}
foreach ($form['actions']['submit']['#submit'] as $submit) {
$submit($form, $form_state);
}
}
function save_draft_validate($form, &$form_state) {
if ($form_state['triggering_element']['#value'] == $form['actions']['draft']['#value']) {
$form_state['values']['status'] = 0;
}
elseif ($form_state['triggering_element']['#value'] == $form['actions']['submit']['#value']) {
$form_state['values']['status'] = 1;
}
}
function save_draft_form_node_type_form_alter(&$form, &$form_state) {
$enabled = variable_get('save_draft_enabled_' . $form['#node_type']->type, SAVE_DRAFT_ENABLED);
$form['save_draft'] = array(
'#type' => 'fieldset',
'#title' => t('Enable "Save draft" button'),
'#weight' => 1,
'#collapsible' => TRUE,
'#collapsed' => !$enabled,
'#group' => 'additional_settings',
'#attached' => array(
'js' => array(
'save-draft' => drupal_get_path('module', 'save_draft') . '/save_draft.js',
),
),
);
$form['save_draft']['save_draft_enabled'] = array(
'#title' => t('Enabled'),
'#type' => 'checkbox',
'#description' => t('Enable save as draft functionality for this content type.'),
'#default_value' => $enabled,
);
$form['save_draft']['save_draft_skip_required'] = array(
'#title' => t('Skip required validation'),
'#type' => 'checkbox',
'#description' => t('Allow users to save a draft without having entered a value for required fields. If unchecked, all required fields must be filled before a draft can be saved.'),
'#default_value' => variable_get('save_draft_skip_required_' . $form['#node_type']->type, FALSE),
);
$form['save_draft']['save_draft_show_message'] = array(
'#title' => t('Show message'),
'#type' => 'checkbox',
'#description' => t('Display a "Saved as draft" message to the user when they use the save draft button.'),
'#default_value' => variable_get('save_draft_show_message_' . $form['#node_type']->type, FALSE),
);
}
function save_draft_access($form, &$form_state) {
$element = $form;
$access = TRUE;
foreach (array(
'options',
'status',
) as $key) {
if (!isset($element[$key])) {
$access = FALSE;
break;
}
$element = $element[$key];
if (isset($element['#access']) && !$element['#access'] || !empty($element['#disabled'])) {
$access = FALSE;
break;
}
}
if (!$access && user_access('save draft')) {
$node = clone $form_state['node'];
$node->status = FALSE;
if (!isset($node->nid)) {
$node->nid = NULL;
}
$access = node_access('update', $node) && node_access('view', $node);
}
return $access;
}
function save_draft_node_type_delete($info) {
variable_del('save_draft_enabled_' . $info->type);
}
function save_draft_form_after_build(array $element, array &$form_state) {
if ($form_state['process_input']) {
if (!empty($form_state['triggering_element']['#skip_required_validation'])) {
_save_draft_remove_required($element);
}
}
return $element;
}
function _save_draft_remove_required(array &$elements) {
foreach (element_children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
_save_draft_remove_required($elements[$key]);
}
}
if (!empty($elements['#required'])) {
$elements['#required'] = FALSE;
}
}