Hi I was wondering if anyone could give me some advice on this. Basically what I have is a custom content type created by the content creator GUI, I am writing a custom module to alter the appearance and values of the content form.

like so:

function sswap_content_validation_form_alter(&$form, $form_state, $form_id) 
{ 
if (isset($form['type']) && isset($form['#node'])) 
{

if ($form_id == 'advertise_node_form') 
{

  // Unset the options we don't want user to see.
  unset($form['menu']);
  unset($form['comment_settings']);
  unset($form['revision_information']);
  unset($form['author']);
  unset($form['options']);
  unset($form['path']);

  $form['#after_build'][] = '_surplus_cck_form_alter';

  // we need to tell the form to follow a validation function
  $form['#validate'][] = '_surplus_cck_validation';
  $form['#submit'][] = '_surplus_cck_submit';
    }
  }
}

This all works fine and I am able to alter values in the form_alter section as expected, however once I get to the _surplus_cck_validation custom function I am unable to change values, basically what I am doing is checking if a value is X if not I'll replace it with something more suitable after some processing. At this point I can't even change X to a static string though.

To alter my form content I am using:

$form['field_adpostcode']['und'][0]['value']['#value'] = 'TESTING';
$form['field_adpostcode']['und'][0]['#value']['value'] = 'TESTING';

These lines do actually work in the form_alter part as mentioned. I should also note the function for surplus_cck_validation is being called I have experimented with a few echos and notices, and the $form and &$form_state are being passed to it also.

Any help on this would be greatly appreciated I've spent the last few hours trying different things and digging down into the form structure with dsm and debug, but can't seem to find what I need.

  • Hello, and welcome on Drupal Answers. You wrote that those lines work. What is your question, then? – kiamlaluno Oct 25 '12 at 16:31
  • Thanks for the welcome, the issue is that they work in form_alter but not in the custom validate function. I'm assuming something changes in the form structure when the validate is called, Sisko's answer helps in that it alters how the value is stored when it is viewed as a node, but then when you go to edit it the old values remain. – Daniel Waters Oct 25 '12 at 18:55
up vote 1 down vote accepted

Perhaps try $form_state.

$form_state['field_adpostcode']['und'][0]['value']['#value'] = 'TESTING';
$form_state['field_adpostcode']['und'][0]['#value']['value'] = 'TESTING';

I suspect the array structure is different but you get the idea.

  • I was lead to believe that form_state was simply for checking values and not altering them? This is just what I read from the Pro Drupal Development book I've been reading, I will give it a try though, thanks! – Daniel Waters Oct 25 '12 at 15:17
  • Well it worked to an extent, while viewing the node the value assigned via $form_state['values']['field_adpostcode']['und'][0]['value'] = 'TESTING'; is visible, however if you go to edit the form these values are what they originally where. – Daniel Waters Oct 25 '12 at 15:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.