开发者

How to get the value of the form field in drupal 7?

开发者 https://www.devze.com 2023-04-02 11:20 出处:网络
I have added a field on the page_node_form with the following code. switch($form_id){ case \'page_node_form\':

I have added a field on the page_node_form with the following code.

switch($form_id){
case 'page_node_form':
$form['comment_popup'] = array(
'#type' => 'select',
'#title' => t('Comments popup'),
'#options' => array('On' => 'On', 'Off' => 'Off'),
);

}

As the comment form appears on the node so I want when the comment_popup field has the value Off then the subject field should not be displayed in the comment form and when the comment_popup field has the value 'On' then the开发者_开发知识库 subject field should be displayed.

I have tried the following code but it did not work.

case 'comment_node_page_form':
if($form_state['comment_popup']['#value'] == 'Off') {
$form['subject']['#access']=FALSE;
}

Any one who can help me?


What you're doing can't work I'm afraid, you're setting an element in one form and then trying to read it from another form which doesn't make sense.

What you need to do is add a submit handler to the page_node_form function which saves the comment_popup value for that node to a custom table. Then in the form alter for `comment_node_page_form' you need to read in the comment_popup data for that particular node, and make the access decisions to the form elements based on that.

Hope that helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号