You are here

function content_lock_form_alter in Content locking (anti-concurrent editing) 7

Same name and namespace in other branches
  1. 8.2 content_lock.module \content_lock_form_alter()
  2. 8 content_lock.module \content_lock_form_alter()
  3. 6.2 content_lock.module \content_lock_form_alter()
  4. 6 content_lock.module \content_lock_form_alter()
  5. 7.3 content_lock.module \content_lock_form_alter()
  6. 7.2 content_lock.module \content_lock_form_alter()

Implementation of hook_form_alter().

File

./content_lock.module, line 190
Allows users to lock documents for modification.

Code

function content_lock_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  $node = empty($form['#node']) ? NULL : $form['#node'];
  $nid = empty($form['nid']['#value']) ? NULL : $form['nid']['#value'];
  $destination = 'node/' . $nid;

  /* Ensure that users acquire a lock when reverting a node to an older revision. */
  if (!empty($form['#node_revision'])) {
    $node = $form['#node_revision'];
    $nid = $node->nid;
    $destination = 'node/' . $nid . '/revisions';
  }

  /* **************** Restore the node format ****************************** */

  // _content_lock_is_lockable_node() needs to know the original
  // node format. We either dig up a stashed content_lock_old_format or
  // initialize it here.
  // Only touch node edit forms and then only if the form is `normal' and has a body with a format (#1183678):
  if (is_object($node) && is_numeric($nid) && ($form_id == $node->type . '_node_form' || $form_id == 'node_revision_revert_confirm') && !empty($node->body[$node->language][0]['format'])) {
    $old_format = $node->body[$node->language][0]['format'];
    if (!empty($node->content_lock_old_format)) {
      $old_format = $node->content_lock_old_format;
    }
    if (!empty($form_state['values']['content_lock_old_format'])) {
      $old_format = $form_state['values']['content_lock_old_format'];
    }

    // Needs to be manually set before first form submission.
    // We set this in the $node-> namespace because content_lock_nodeapi()
    // doesn't see $form_state['values'].
    $node->content_lock_old_format = $old_format;
    $form['content_lock_old_format'] = array(
      '#type' => 'hidden',
      '#value' => $node->content_lock_old_format,
    );
  }

  /** ******************* General preconditions for locking ***************** */

  // Veto-API. Let other modules veto the locking - so force skipping out of any conditions they want.
  // We will use || logic, so if any module denies locking then we deny locking.
  // Be sure to notice that content_lock also implements this api for his own vetos!
  $skip_lock = FALSE;

  // no veto yet
  $result = module_invoke_all('content_lock_skip_locking', $node, $form_id, $form, $form_state);
  foreach ($result as $bool) {
    if (is_bool($bool)) {
      $skip_lock = $skip_lock || $bool;
    }
  }
  if ($skip_lock == FALSE) {

    // if we should lock or already have been locked, load the unload js. Dont use
    // form alter but rather after build, so it works even for previews
    if (variable_get('content_lock_unload_js', true)) {
      $form['#after_build'][] = '_content_lock_add_unload_js';
    }

    // Adding cancel button, if configured
    if (variable_get('content_lock_admin_cancelbutton', true)) {
      _content_lock_add_cancelbutton($form, $form_state, $form_id);
    }

    // If we are handling a preview, skip locking
    if (!empty($form_state['rebuild']) && $form_state['rebuild'] == TRUE) {

      // We dont need anything here right now
    }
    else {
      if ($form_state['submitted'] === FALSE) {

        // Finally set the lock if everthing passed.
        if (content_lock_node($nid, $user->uid) == false) {

          // could not lock node, it's locked by someone else
          drupal_goto($destination);
        }
      }
    }

    // else if($form_state['submitted'] === TRUE)
    // if it is a submission, we would not need to lock once again, as we had before.
    // as nodeapi insert/update are not called on preview, the node should stay locked until saved or canceled.
  }
}