hi,
I'm bulding a custom module with the purpose to alter the "edit form" of every content type (i.e. the edit form of the blog content type located at admin/content/node-type/blog).
Creating new form elements to extend the standard form with my custom form elements wasn't a problem at all. When I enable my module at admin/build/modules I can see my custom form elements and everything seems to work. Here's what my ".module" file looks like:
<?php
// $Id$
/**
* Implementation of hook_form_alter().
*/
function privacy_per_node_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'node_type_form') {
$form['privacy_per_node'] = array(
'#type' => 'fieldset',
'#title' => t('Privacy per Node'),
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
$form['privacy_per_node']['ppn_turn_on'] = array(
'#type' => 'checkbox',
'#title' => t('Activate Privacy Settings'),
'#description' => 'Set this checkbox to activate privacy settings for this content type.',
'#default_value' => variable_get('ppn_turn_on', 0)
);
$form['privacy_per_node']['ppn_settings'] = array(
'#type' => 'radios',
'#title' => t('Default Privacy Settings'),
'#description' => 'Select a visibility option.',
'#default_value' => variable_get('ppn_settings', 0),
'#options' => array(
t('Nobody <em>(No one except you will see this node)</em>.'),
t('Friends <em>(Only your friends are able to see this node)</em>.'),
t('All <em>(Everybody will see this node)</em>.')
)
);
}
//$output .= dsm($form);
return $form;
}
?>The big problem that I don't know how to solve is, that when I hit the "Save Button" the values of my custom form elements aren't saved! For example: by default the checkbox is not checked (which means that this particular content type doesn't need to have any privacy settings). If I want to enable the privacy settings, I check the checkbox and press save. Drupal then redirects me to the overview page of all content types (admin/content/types). From there I click on the content type I was editing. The checkbox for activating the privacy settings is set to default and it's not checked!
Same thing happens to the radio buttons of my custom form elements. By default, the first radio button is selected. When I select any other radio button (second or third), hit save and come back to the edit page the first radio button is selected again.
Seems like the new values of my custom form elements won't be saved to the database.
How can I solve this? Is my code correct or do I miss anything here?
Thanks in adavanced for any help! :-)
Comments
hook_nodeapi()
The form is only one part of what you need, but you haven't specified what should be done with the input. You need to implement hook_nodeapi() (http://api.drupal.org/api/function/hook_nodeapi/6) for the $op 'presave', 'insert' or 'update' and do something with that data: save it somewhere, display a message, etc.
Thanks for your answer :-) I
Thanks for your answer :-)
I don't understand how this hook will help me because I'm not trying to extend the edit form of a node. My module is designed to work only in the admin section. Could you give me an example on how to implement hook_nodeapi?
Thanks :-)
Content type suffix
Sorry, I read through the post way too quickly, this doesn't have anything to do with hook_nodeapi().
The node settings forms behaves a little differently from other settings forms in that it automatically appends the machine name of the content type to your variables. So in your case, you should use
variable_get('ppn_turn_on_' . $form['#node_type']->type, 0)instead ofvariable_get('ppn_turn_on', 0)I've tried around with
I've tried around with variable_get but couldn't figure it out!
Thank you so much! It works just perfect! :-)
Also look at this
hook_form_alter() and CCK fields
http://drupal.org/node/726282
use submit function
i always use
$form['#submit'][]='my_submit_function';and attach my function to submit function of form
resume: http://www.drupaldevelopers.net/profiles/esmailzadeh
drupal blog: http://local.bloghaa.com
personal blog: http://localinside.persianblog.ir
For Drupal 7 form_id returns
For Drupal 7 form_id returns an array...