You are here

function save_draft_access in Save Draft 7

Returns TRUE if the currently logged in user has permission to choose the

published state for the node being edited.

1 call to save_draft_access()
save_draft_form_node_form_alter in ./save_draft.module
Implements hook_form_FORM_ID_alter().

File

./save_draft.module, line 185
Main file for the Save Draft module, which adds a 'Save as draft' button to content types.

Code

function save_draft_access($form, &$form_state) {

  // Determine if the user has access to publish / unpublish this node via the
  // status checkbox.
  $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 not, but the user has the 'save draft' permission, determine if the user
  // has access to view and edit this node if it were unpublished. Don't allow a
  // user to save a draft that they won't be able to get back to.
  if (!$access && user_access('save draft')) {
    $node = clone $form_state['node'];
    $node->status = FALSE;

    // This might be a new node, and node_access() throws PHP warnings if nid
    // isn't defined.
    if (!isset($node->nid)) {
      $node->nid = NULL;
    }
    $access = node_access('update', $node) && node_access('view', $node);
  }
  return $access;
}