Field API Hooks Explaination

We will explain Field API Hooks with the help of a module. This Module is "Math Work". When this module will be installed, it will create two field type (1)Factorial (2)Square. User can create field of factorial type. User will write an integer in create content page and its factorial will be shown on node view Page. User can also create field of square type. User will write an integer in create content page and its square will be shown on node view Page



You can download mathwork module by clicking on this link Download

Hook Field Help

/** * Implements hook_help(). */ function mathwork_help($path, $arg) { switch ($path) { case 'admin/help#mathwork': $output = ''; $output .= '

' . t('About Math Work Module') . '

'; $output .= '

' . t('On this page we will provide help about mathwork module. See the Field module help page for more information about fields.', array('@field-help' => url('admin/help/field'))) . '

'; return $output; } }

This hook will create a link on /drupal/admin/help page

When you will click on this link "Math Work", the following page will open.

Hook Field Info

/** * Implements hook_field_info(). */ function mathwork_field_info() { return array( 'math_square' => array( 'label' => t('Square'), 'description' => t('Stores small text data.'), 'default_widget' => 'math_squarefield', 'default_formatter' => 'math_squareformat', ), 'math_fact' => array( 'label' => t('Factorial'), 'description' => t('Stores small text data.'), 'default_widget' => 'math_factfield', 'default_formatter' => 'math_factformat', ), ); }

This hook will create the following selections in manage fields page..

Hook Field Schema

/** * Implements hook_field_schema(). */ function mathwork_field_schema($field) { switch ($field['type']) { case 'math_square': $columns = array( 'math_square' => array( 'type' => 'varchar', 'length' => 10, 'not null' => FALSE, ), ); break; case 'math_fact': $columns = array( 'math_fact' => array( 'type' => 'varchar', 'length' => 10, 'not null' => FALSE, ), ); break; } return array('columns' => $columns); } This hook will create database table where values will be stored.

Hook Field Validate

/** * Implements hook_field_validate(). */ function mathwork_field_validate($obj_type, $object, $field, $instance, $langcode, &$items, &$errors) { foreach($items as $delta => $item) { if(isset($item['math_square'])) { if($item['math_square'] != ''){ if(! preg_match('@^[0-9]+$@', $item['math_square'])) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'mathwork_square_invalid', 'message' => t($instance['label'] . ' must be integer value.'), ); } else if($item['math_square'] > 999) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'mathwork_square_invalid', 'message' => t($instance['label'] . ' must be less than 1000.'), ); } } } else if(isset($item['math_fact'])) { if($item['math_fact'] != ''){ if(! preg_match('@^[0-9]+$@', $item['math_fact'])) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'mathwork_fact_invalid', 'message' => t($instance['label'] . ' must be integer value.'), ); } else if($item['math_fact'] > 99) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'mathwork_fact_invalid', 'message' => t($instance['label'] . ' must be less than 100.'), ); } } } } } This hook will Validate field values and will through error if user will add wrong values

Hook Field Widget Settings Form

/** * Implements hook_field_widget_settings_form(). */ function mathwork_field_widget_settings_form($field, $instance) { $widget = $instance['widget']; $settings = $widget['settings']; $form['size'] = array( '#type' => 'textfield', '#title' => t('Size of textfield'), '#default_value' => $settings['size'], '#required' => TRUE, '#element_validate' => array('_element_validate_integer_positive'), ); return $form; } This hook will create textfield "size" in field settings page

Hook Field Formatter View

/** * Implements hook_field_formatter_view(). */ function mathwork_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) { $element = array(); if($display['type']=='math_squareformat') { foreach ($items as $delta => $item) { $output = $item['math_square'] * $item['math_square']; $element[$delta] = '

' . $output . '

'; } } if($display['type']=='math_factformat') { foreach ($items as $delta => $item) { $output = factorial($item['math_fact']); $element[$delta] = '

' . $output . '

'; } } return $element; } function factorial($input){ $output = 1; for($x = $input; $x >= 1; $x--){ $output *= $x; } return($output); }
This hook will show square or factorial of a number on node view page