Introduction:
This discussion references the upgrade process for cck_time module. One should look at the cck_time.module for Drupal 5 and Drupal 6 while reading the discussion of the functions and what they do.
Useful links:
Updating CCK Modules from 5.x to 6.x
CCK Handbook
Drupal 6 Form Api Reference
Widgets in Drupal 5 work on the following functions:
function cck_time_widget_info()
This function places your widget into a list when you are adding a new field. (content management → content types → edit content type → manage fields)
function cck_time_widget_settings($op, $widget)
This function has three main tasks which depend on the $op variable. When $op is 'form' the function returns the form inputs for when you are editing the field settings. When $op is 'validate' the function is responsible for validating the fields that are displayed when in the 'form' state. In this case the function calls form_set_error when an error is detected. Finally when $op is 'save' the function returns an array of fields to save to the form (they are accessed by form['widget']['varname'] in the cck_time_widget function).
function cck_time_widget($op, &$node, $field, &$node_field)
This function has four tasks depending on the $op variable.
The first task is to prepare form values (when $op is 'prepare form values'). In this case the form values are held in $node_field[0]['value'] and the function must take this value and split it up into values for each of the form inputs (hour, minute and meridiem). They are stored in $node_field ($node_field['default_hour']...)
The second task is to prepare form inputs when a content type is being added (when $op is 'form'). In this case the function returns an array of field inputs using drupals form api. One thing to note is that the default values should be loaded from the values stored in the node field by the 'prepare form values' case.
The third task is to validate the inputs that were prepared by the 'form' case (when $op is 'validate'). Once again this function takes the node_field variables that were prepared by the 'prepare form values' case and makes sure they are valid. This case calls the form_set_error function when it detects an invalid variable state.
The final task is to process the form values for output (when $op is 'process form values'). This case uses the node_field variables that have been prepared and validated and formats them into a state that can be inserted into the database. This is accomplished by setting $node_field[0]['value'].
Widgets in Drupal 6 work on the following functions:
function cck_time_widget_info()
Not much has changed since Drupal 5. With the exception of two more elements in the array (multiple values and callbacks).
function cck_time_widget_settings($op, $widget)
Not much has changed since Drupal 5. With the exception of one less case for $op. This case is 'callbacks' which has been moved to the cck_time_widget_info() function.
function cck_time_widget(&$form, &$form_state, $field, $items, $delta = 0)
This is where the bulk of the differences are between Drupal 5 and Drupal 6 widgets. Notice that there is no $op variable and no &$node_field variable. Most of the $op cases have been implemented using the form api instead. Now this function is mainly responsible for displaying the inputs for this widget (the 'form' case of Drupal 5). The function returns an array of inputs using the form api structure. The 'prepare form values' case is handled within this function. Usually the values come in via the $items except after a node is being edited, after being inserted, it comes in via $items['0']['value'] because it has already been processed. The validate case (from Drupal 5) is now handled by the form api. One must register the validation function: $element['#element_validate'][] = 'myvalidate_validate';. The 'process form values' case is handled by the form api as well. One must register the process function: $element['#process'][] = 'myprocess_process';.
**These final two function names depend on what one registered in the cck_time_widget function.
function myprocess_process($element, $edit, $form_state, $form)
This function is responsible for processing the input values for output. It takes in the values that were input from the cck_time_widget function and arranges them into a proper format (placed in $element['#value'][0])
function myvalidate_validate($element, &$form_values)
This function is responsible for the validation of your input values. It takes in the value via: $element['#value'][0][$colName]. It is responsible for calling the form_set_error function when an invalid input is detected.



Add new comment