⬆️ ⬇️

Drupal Forms API. Part 2 - for Drupal 7



The second part of the article contains the differences inherent in the Drupal Forms API for version 7. This part of the article is published as an open topic, visible not only to Drupal blog subscribers. But I will publish my further articles (on creating themes) as closed blog articles, so as not to disturb those who are not interested in Drupal.



In the last article , we disassembled the functionality of a simple module that returned the entered name to the form using AJAX (referred to in Drupal 6 as “AHAH”). Now the turn of version 7.





')

1. The most interesting changes in the API Drupal 7







2. Changes in the form code



Changes in the form code are minor. The principles of the Drupal Forms API remain the same.

/* FC Form */ //         (    , ,    ) function fc_form($form, &$form_state) { $form = array(); $form['name'] = array( '#type' => 'textfield', '#description' => 'Your name', '#size' => '15', ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Submit', //    ajax '#ajax' => array( //     callback- 'callback' => 'fastcontact_ajax', 'wrapper' => 'fc-form', ), ); return $form; } 




2. Changes in the request processing code



 //  callback-   .        function fastcontact_ajax($form, &$form_state) { //    name   values  form_state $name=$form_state['values']['name']; //       markup   . $form['result'] = array( '#markup' => '   <b>'.$name.'</b>', ); return $form; } 




The value of the $ form_state array is quite large. It stores all the data on the form and its condition . From there we take the value of our name field.



If in Drupal 6 we sent the text that was displayed in case of success, via drupal_json () , then in Drupal 7 we reassemble the form and add the markup element to it. Markup is used to output html markup to a form. This element existed in version 6 of Drupal - but in version 7 its syntax has changed .



Now we have to display the form. We can make it a standard code from the previous article.

 function render_fc_form() { $out = '<div id="fc-form-wrapper">'; $out .= drupal_render(drupal_get_form('fc_form')); $out .= '</div><!-- /.fc-form-->'; return $out; } 




Please note that there are many more API changes in Drupal 7. Here is a list of changes made to the Drupal API after version 6 concerning writing modules (for topics, a separate list).



Download the archive with the whole module

Source: https://habr.com/ru/post/111359/



All Articles