hook_field_widget_settings_form

7 field_ui.api.php hook_field_widget_settings_form($field, $instance)
8 field_ui.api.php hook_field_widget_settings_form($field, $instance)

Add settings to a widget settings form.

Invoked from field_ui_field_edit_form() to allow the module defining the widget to add settings for a widget instance.

Parameters

$field: The field structure being configured.

$instance: The instance structure being configured.

Return value

The form definition for the widget settings.

Related topics

5 functions implement hook_field_widget_settings_form()

1 invocation of hook_field_widget_settings_form()

File

modules/field_ui/field_ui.api.php, line 107
Hooks provided by the Field UI module.

Code

function hook_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];

  if ($widget['type'] == 'text_textfield') {
    $form['size'] = array(
      '#type' => 'textfield', 
      '#title' => t('Size of textfield'), 
      '#default_value' => $settings['size'], 
      '#element_validate' => array('element_validate_integer_positive'), 
      '#required' => TRUE,
    );
  }
  else {
    $form['rows'] = array(
      '#type' => 'textfield', 
      '#title' => t('Rows'), 
      '#default_value' => $settings['rows'], 
      '#element_validate' => array('element_validate_integer_positive'), 
      '#required' => TRUE,
    );
  }

  return $form;
}

Comments

we need form_submit and form_validate hooks as well ...

Since Drupal provides a hook to CREATE these settings form elements, then I should have a similar access to the SUBMIT and VALIDATE routines for the same form elements.

In short, there should exist :

hook_field_widget_settings_form_submit()
hook_field_widget_settings_form_validate()

How else can I tell Drupal (field_ui) how to process my customized (ie: not structured in a standard format) form structure?

M.M.

Use #element_validate and #element_submit

You have #element_validate and #element_submit array members exactly for that purpose

#element_submit does not exist

#element_submit does not exist, but I guess that #value_callback will cover your needs most of the times.

The form ID for the Field

The form ID for the Field Widget UI is field_ui_field_edit_form therefore a solution for this is to implement either hook_form_FORM_ID_alter() or hook_form_alter(), add a submit handler callback with $form['#submit'][] and you should be fine.

For example:

<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function MYMODULE_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
 
$form['#submit'][] = 'my_submit_handler';
}
?>

Use hook_field_validate() and hook_field_presave()

In Drupal 7 there are a number of built in validate, error, and save functions that handle fields that do not follow the traditional hook_form_submit() and hook_form_validate() naming that people may be used to in Drupal 6. They new ones are:

hook_field_widget_error()
hook_field_validate()
hook_field_presave()

You can also add a custom element level validation callback in hook_field_widget_form() using $element['#element_validate'][] = 'custom_field_widget_validate'; as stated earlier.

Login or register to post comments