In the previous section we defined some default attributes of our IMEI component. Now we are going to define the actual component edit page using the Drupal Form API. This is the user interface that website administrators will use to change the defaults we specified previously.
Note that we are not defining the entire component edit page here. The data in this function will be utilized to supplement the default component edit form which already includes the Label and Field Key textfields. We are only specifying the additional fields required for the specific setup of our IMEI components.
Let us add the edit form functionality to our webform_imei.module
file:
/** * Implements _webform_edit_component(). */ function _webform_edit_imei($component) { $form = array(); $form['value'] = array( '#type' => 'textfield', '#title' => t('Default value'), '#default_value' => $component['value'], '#description' => t('The default value of the field.'), '#size' => 60, '#maxlength' => 1024, '#weight' => 0, ); $form['display']['size'] = array( '#type' => 'textfield', '#title' => t('Width'), '#default_value' => $component['extra']['size'], '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'), '#size' => 5, '#maxlength' => 10, '#weight' => 0, '#parents' => array('extra', 'size'), ); $form['validation']['unique'] = array( '#type' => 'checkbox', '#title' => t('Unique'), '#return_value' => 1, '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'), '#weight' => 1, '#default_value' => $component['extra']['unique'], '#parents' => array('extra', 'unique'), ); $form['validation']['maxlength'] = array( '#type' => 'textfield', '#title' => t('Maxlength'), '#default_value' => $component['extra']['maxlength'], '#description' => t('Maximum length of the textfield value.'), '#size' => 5, '#maxlength' => 10, '#weight' => 2, '#parents' => array('extra', 'maxlength'), ); return $form; }
When we add a new IMEI component to a Webform we are immediately redirected to the component edit page. The component attribute fields will be populated by the defaults we specified in the previous section. When we edit an existing IMEI component, the attribute fields will be populated with the previously saved values from our last edit session.
Later, after we have finished the coding and enabled our module, we will see these fields on our component edit form, along with some fields that are automatically created because of the entries in the first hook we implemented (hook_webform_component_info()
), for example Label display.
Drupal uses the $form
array that we have populated to construct the final form as rendered by our web browser. As such, we may manipulate this array to bring about changes in the final rendered form.