Currently mobwrite module by passes Drupal's validation to allow multiple users to save. This functionality is needed, but so is the rest of the validation.

// This "pre validation" function merely resents the timestamp of the node if its been saved by another user
159	// Referenced above in mobwrite_form_alter() via $form[]['#validate'][0] => mobwrite_pre_validation; and then mimics
160	// node_form_validate() at the end to put the validation back on track.
161	function mobwrite_pre_validation($node, $form) {
162	  if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
163	    $mobwrite_validate_timestamp = $node->changed;
164	    $mobwrite_validate_update = db_update('node')
165	    ->fields(array('changed' => $mobwrite_validate_timestamp))
166	    ->execute();
167	  }
168	  // The rest of mobwrite_pre_validation() is a copy of node_from_validate:7
169	  // $form_state['node'] contains the actual entity being edited, but we must
170	  // not update it with form values that have not yet been validated, so we
171	  // create a pseudo-entity to use during validation.
172	  //$node = (object) $form_state['values'];
173	  //node_validate($node, $form);
174	
175	  // Field validation. Requires access to $form_state, so this cannot be
176	  // done in node_validate() as it currently exists.
177	  // known bug: uncommenting breaks the page.
178	  //entity_form_field_validate('node', $form, $form_state)
179	} // function mobwrite_node_validate()

Comments

tomdavidson’s picture

Assigned: Unassigned » tomdavidson
tomdavidson’s picture

Title: "unskip" validation » "unskip" validation | How to test?
Status: Active » Needs work

Implementing hook_validate with the same guts as my own function but with the expectation that the rest of Drupal's validation would be kept has no effect what so ever:

function mobwrite_validate($node) {
 if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
    $mobwrite_validate_timestamp = $node->changed;
    $mobwrite_validate_update = db_update('node')
    ->fields(array('changed' => $mobwrite_validate_timestamp))
    ->execute();
  }
} // mobwrite_validate()

The above code (issue description the above code in this comment) functions, but it skips out on the rest of the validation process:

mobwrite_form_alter() {
...
// Slip in our own validation to allow users to save the node even if someone already has
  $form['#validate'][0] = 'mobwrite_pre_validation';
...
} // mobwrite_validate()

// This "pre validation" function merely resents the timestamp of the node if its been saved by another user
// Referenced above in mobwrite_form_alter() via $form[]['#validate'][0] => mobwrite_pre_validation; and then mimics
// node_form_validate() at the end to put the validation back on track.
function mobwrite_pre_validation($node, $form, &$form_state) {
  if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
    $mobwrite_validate_timestamp = $node->changed;
    $mobwrite_validate_update = db_update('node')
    ->fields(array('changed' => $mobwrite_validate_timestamp))
    ->execute();
  }
  //node_form_validate($form, &$form_state);
} //function mobwrite_node_validate()

But reseting the callback seams to work:

function mobwrite_pre_validation($node, $form) {
  if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
    $mobwrite_validate_timestamp = $node->changed;
    $mobwrite_validate_update = db_update('node')
    ->fields(array('changed' => $mobwrite_validate_timestamp))
    ->execute();
  }
  //node_form_validate($form, &$form_state);
  $form['#validate'][0] = 'node_form_validate';
} //function mobwrite_node_validate()

How do I test and verify?

tomdavidson’s picture

Rather than $form['#validate'][0] = 'mobwrite_validation'; in hook_form_alter() and then again $form['#validate'][0] = 'node_validation'; in mobwrite_validation() - sounds like even this might have not mattered.

Code uses array_unshift($form['#validate'], 'mobwrite_validation'); in hook_form_alter().

This provides the needed functionaliy - its it best practice? mobwrite_validation() might be expanding/changing next week with the submitt and auto save type functionality.

tomdavidson’s picture

oops, array_unshift($form['#validate'][], 'mobwrite_validation'); generates warning and does not work, "Warning: array_unshift(): The first argument should be an array in mobwrite_form_alter() (line 106 of /home/clients/websites/w_3f5b7z/public_html/3f5b7z/mw/sites/all/modules/mobwrite/mobwrite.module)."

reverting back to $form['#validate'][0] = 'mobwrite_validation' in form_alter() and $form['#validate'][0] = 'node_validation' in mbowrite_validation().

dmitrig01’s picture

A better approach, which wouldn't require any messing around with #validate, would be setting $form['changed']['#value'] to 0 in your _form_alter. This works because $form['changed'] has '#type' => 'value', which means it's like a hidden input, except it hasn't been rendered to the HTML - its gets passed
straight through to the submit as $form_state['values']['changed']. This value isn't depended upon anywhere except the validate - the changed value gets updated in node_save

tomdavidson’s picture

$form['changed'] is also used for the timestamp in revisions. If we do some think like = 0, then it will mess up the revision right? And thinking about it, so does the existing mobwrite_validation():

<?php
// This validation function merely resets the timestamp of the node if its been saved by another user.
// Referenced above in mobwrite_form_alter() via array_unshift($form['#validate'], 'mobwrite_validation');
function mobwrite_validation($node, $form) {
  if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
    $mobwrite_validate_timestamp = $node->changed;
    $mobwrite_validate_update = db_update('node')
    ->fields(array('changed' => $mobwrite_validate_timestamp))
    ->execute();
  }
} //function mobwrite_node_validation()
?>

poking around more.

tomdavidson’s picture

Status: Needs work » Closed (fixed)

btw: $form['changed'] == 0; Does not allow the prior editor to save after a later editor already has.
revision_timstamp, changed and changed from the node table are all = and shouldn't bite "if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed))" of node validate, but it does.

The task is to "unskip" Drupal's validation. I am reverting to what does work, but am renaming it to mobwrite_pre_validation() to avoid confusion with an implementation of hook_validate().

I agree there is prob to be a better way. But this issue I think is fixed and the solution does not appear grotesque. A more elegant solution will be sought when the remaining tasks are completed. Of course if dmitrig01 or lyricnz says issue is not resolved, ill come back to it right away.