This tutorial is about Field API. Field API is a new API in Drupal. Field...
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 .= '<h3>' . t('About Math Work Module') . '</h3>';
$output .= '<p>' . t('On this page we will provide help about mathwork module.
See the <a href="@field-help">Field module help page</a> for more information
about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
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
This hook will create database table where values will be stored./**
* 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);
}![]()
Hook Field Validate
This hook will Validate field values and will through error if user will add wrong values/**
* 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.'),
);
}
}
}
}
}![]()
![]()
Hook Field Widget Settings Form
This hook will create textfield "size" in field settings page/**
* 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;
}![]()
Hook Field Formatter View
This hook will show square or factorial of a number on node view page/**
* 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] = '<p>' . $output . '</p>';
}
}
if($display['type']=='math_factformat') {
foreach ($items as $delta => $item) {
$output = factorial($item['math_fact']);
$element[$delta] = '<p>' . $output . '</p>';
}
}
return $element;
}
function factorial($input){
$output = 1;
for($x = $input; $x >= 1; $x--){
$output *= $x;
}
return($output);
}![]()
This tutorial is about Field API. Field API is a new API in Drupal. Field...