hook_form_alter() node_type_form validation and persistence
Hi List! Maybe I just can't see the forest for the trees: In scheduler.module (http://drupal.org/project/scheduler) in scheduler_form_alter() the following code can be found (D6): if ('node_type_form' == $form_id) { $form['workflow']['scheduler'] = array( '#type' => 'checkbox', '#title' => t('Enable scheduled (un)publishing'), '#default_value' => variable_get('scheduler_'. $form['#node_type']->type, 0), '#description' => t('Check this box to enable scheduled (un)publishing for this node type.') ); $form['workflow']['scheduler_touch'] = array( '#type' => 'checkbox', '#title' => t('Alter published on time'), '#default_value' => variable_get('scheduler_touch_'. $form['#node_type']->type, 0), '#description' => t('Check this box to alter the published on time to match the scheduled time ("touch feature").') ); } I am trying to understand this. I can't figure out how the values of the two checkboxes get validated and persisted back to "scheduler_story" and "scheduler_touch_story". Is there some magic involved that guesses the variable names from $form['workflow']? How would I add my own fieldset? Thanks, Eric
Eric There is a bit of magic for that form, the values in the form are automatically stored as Drupal variables use the form elements name plus the node type So by using '#default_value' => variable_get('scheduler_'.$form['#node_type']->type, 0), the default value uses the previous stored value if available, otherwise 0. The 'scheduler' comes from the form element name ($form['workflow']['scheduler']) You can add new elements to existing field sets or add your own field set. Nevets On 10/6/2010 12:27 PM, Eric Schaefer wrote:
Hi List!
Maybe I just can't see the forest for the trees:
In scheduler.module (http://drupal.org/project/scheduler) in scheduler_form_alter() the following code can be found (D6):
if ('node_type_form' == $form_id) { $form['workflow']['scheduler'] = array( '#type' => 'checkbox', '#title' => t('Enable scheduled (un)publishing'), '#default_value' => variable_get('scheduler_'. $form['#node_type']->type, 0), '#description' => t('Check this box to enable scheduled (un)publishing for this node type.') ); $form['workflow']['scheduler_touch'] = array( '#type' => 'checkbox', '#title' => t('Alter published on time'), '#default_value' => variable_get('scheduler_touch_'. $form['#node_type']->type, 0), '#description' => t('Check this box to alter the published on time to match the scheduled time ("touch feature").') ); }
I am trying to understand this. I can't figure out how the values of the two checkboxes get validated and persisted back to "scheduler_story" and "scheduler_touch_story". Is there some magic involved that guesses the variable names from $form['workflow']? How would I add my own fieldset?
Thanks, Eric
2010/10/6 Steve Ringwood <nevets@tds.net>:
The 'scheduler' comes from the form element name ($form['workflow']['scheduler'])
You can add new elements to existing field sets or add your own field set.
Thats what I guessed. Thats why I tried this: $form['scheduler'] = array( '#type' => 'fieldset', '#title' => 'Scheduler settings', '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 35, '#group' => 'additional_settings', ); $form['scheduler']['publish'] = array( '#type' => 'fieldset', '#title' => 'Publishing settings', '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 1, '#group' => 'additional_settings', ); $form['scheduler']['publish']['enable'] = array( '#type' => 'checkbox', '#title' => t('Enable scheduled publishing'), '#default_value' => variable_get('scheduler_publish_enable_'. $form['#node_type']->type, 0), '#description' => t('Check this box to enable scheduled publishing for this node type.') ); $form['scheduler']['publish']['touch'] = array( '#type' => 'checkbox', '#title' => t('Alter published on time'), '#default_value' => variable_get('scheduler_publish_touch_'. $form['#node_type']->type, 0), '#description' => t('Check this box to alter the published on time to match the scheduled time ("touch feature").') ); There is supposed to be a "scheduler" fieldset and a "publish" fieldset inside it. Inside the latter are two checkboxes. The form gets created alright but the variables don't get set. What am I doing wrong? Thanks, Eric
Eric For the variable name you only want to use the element name, so for ['scheduler']['publish']['touch'] you would use 'touch', so the call to variable_get would be variable_get('touch_'.$form['#node_type']->type, 0) I tend to make my names more self contained so instead of 'touch' I would use something like "publish_touch" to the element and variable name. Nevets On 10/6/2010 2:02 PM, Eric Schaefer wrote:
2010/10/6 Steve Ringwood<nevets@tds.net>:
The 'scheduler' comes from the form element name ($form['workflow']['scheduler'])
You can add new elements to existing field sets or add your own field set.
Thats what I guessed. Thats why I tried this:
$form['scheduler'] = array( '#type' => 'fieldset', '#title' => 'Scheduler settings', '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 35, '#group' => 'additional_settings', );
$form['scheduler']['publish'] = array( '#type' => 'fieldset', '#title' => 'Publishing settings', '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 1, '#group' => 'additional_settings', ); $form['scheduler']['publish']['enable'] = array( '#type' => 'checkbox', '#title' => t('Enable scheduled publishing'), '#default_value' => variable_get('scheduler_publish_enable_'. $form['#node_type']->type, 0), '#description' => t('Check this box to enable scheduled publishing for this node type.') ); $form['scheduler']['publish']['touch'] = array( '#type' => 'checkbox', '#title' => t('Alter published on time'), '#default_value' => variable_get('scheduler_publish_touch_'. $form['#node_type']->type, 0), '#description' => t('Check this box to alter the published on time to match the scheduled time ("touch feature").') );
There is supposed to be a "scheduler" fieldset and a "publish" fieldset inside it. Inside the latter are two checkboxes. The form gets created alright but the variables don't get set. What am I doing wrong?
Thanks, Eric
Nevets That makes sense. There are actually variables named touch_story and enable_story. Great. I thought the variable name would be built from all the element names (like scheduler_publish_touch_story from ['scheduler']['publish']['touch']). Thanks a lot Eric
participants (2)
-
Eric Schaefer -
Steve Ringwood