Saving node-type settings in D7

Today I needed to port Node Limit to Drupal 7, which was an easy task. Node Limit for D6 is a well written module by Stefan Auditor, which makes the porting process easier.

While doing it I noticed that the form_alter he was making to the Node settings page/form included no submit handler, which wasn't a surprise, since most of the values end up then being a variable of the form "field-namenode-type", which in this case is "comment-limit_node-type". But some questions came up:

  • is everything then becoming a variable?
  • if I add my own _submit handler, how do I stop the node module from converting my form fields to variables?

And the answer was again simple, just unset the $form_state['values']['field-name'] value, but it needs some considerations, here is option 1:

  1. Have your own _submit handler (obvious, right? well, worth mentioning)
  2. Ensure your _submit runs before the default one.
    • Do whatever you need to do with your data.
  3. Unset the value here: unset($form_state['values']['field-name']);

And that might bring some funny things happening, like saving values when a node type is about to be deleted (which you don't want to do, because you don't need them anymore), since that happens in the default _submit handler. So here it is option 2:

  1. Have your own _validate AND _submit handlers
  2. In you _validate
    • move your data from $form_state['values']['field-name']to $form_state['WHATEVER']['field-name']
    • unset your data here: unset($form_state['values']['field-name']);
  3. In your submit, which runs in the normal order
    • Use $form_state[WHATEVER]['field-name'] instead of $form_state['values']['field-name']
  4. Nothing here!

I like the second option better, seems more elegant, but hey, there is the first one just in case.

Aug. 24, 2012

Comments