This document discusses how to create a custom field in Drupal 7. It begins by explaining that fields were moved into core in Drupal 7. It then provides steps to create a custom "Collaborator" field with name, role, and link text components. These steps include creating module files, implementing hook functions to define the schema, widget, and formatter. Custom formatters and validation functions are also demonstrated. Documentation resources for fields are shared.
5. WHAT’S NEW IN DRUPAL 7
• InDrupal 6 we used the
CCK module to add fields
into nodes
6. WHAT’S NEW IN DRUPAL 7
• In
Drupal 7 a lot of CCK’s functionality has been moved into
core
• Not all CCK modules made it into core
7. WHAT’S NEW IN DRUPAL 7
• “Node reference” and “User reference” moved to http://
drupal.org/project/references
• “Fieldset” moved to http://drupal.org/project/field_group
• “Contentpermission” moved to http://drupal.org/project/
field_permissions
8. WHAT’S NEW IN DRUPAL 7
• Drupal 7 ships with text, options, number and list
• File and image field all in core
18. HOW TO CREATE A FIELD
• Createa custom
“Collaborators” field
• Module will be called
“collabfield”
• Unlimited value field with a
Name, Role and Link text
fields within
19. HOW TO CREATE A FIELD
• Createa module called
collabfield
• Createcollabfield.info,
collabfield.install and
collabfield.module files
20. HOW TO CREATE A FIELD
collabfield.info
; $Id$
name = Collaborator field
description = Custom collaborator field.
dependencies[] = field_ui
core = 7.x
21. HOW TO CREATE A FIELD
collabfield.install
<?php
// $Id$
/**
* Implements hook_install().
*/
function collabfield_install() {
}
/**
* Implements hook_uninstall().
*/
function collabfield_uninstall() {
}
23. HOW TO CREATE A FIELD
collabfield.module
<?php
// $Id$
/**
* Implementation of hook_field_info().
*/
function collabfield_field_info() {
return array(
'collabfield' => array(
'label' => t('Collaborator'),
'description' => t('Custom collaborators field.'),
'default_widget' => 'collabfield_collabfield_form',
'default_formatter' => 'collabfield_default',
),
);
}
24. HOW TO CREATE A FIELD
collabfield.module
/**
* Implementation of hook_field_is_empty().
*/
function collabfield_field_is_empty($item, $field) {
if ($field['type'] == 'collabfield') {
if (empty($item['name']) && empty($item['role']) && empty($item['link'])) {
return TRUE;
}
}
return FALSE;
}
25. HOW TO CREATE A FIELD
• Create a widget
• Use hook_field_widget_info
to setup field
• Use
hook_field_widget_form for
the actual form element
26. HOW TO CREATE A FIELD
/**
* Implements hook_field_widget_info().
*/
function collabfield_field_widget_info() {
return array(
'collabfield_collabfield_form' => array(
'label' => t('Collabfield form'),
'field types' => array('collabfield'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
//Use FIELD_BEHAVIOR_NONE for no default value.
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
28. HOW TO CREATE A FIELD
• To validate an element add #element_validate to the specific
element
$element['link'] = array(
'#type' => 'textfield',
'#title' => t('Link'),
'#default_value' => isset($items[$delta]['link']) ? $items[$delta]['link'] : NULL,
'#element_validate' => array('_collabfield_link_validate'),
);
/**
* Validation callback for a collabfield link element.
*/
function _collabfield_link_validate($element, &$form_state, $form) {
$value = $element['#value'];
if (!empty($value) && !valid_url($value, TRUE)) {
form_error($element, t('Invalid URL.'));
}
}
29. HOW TO CREATE A FIELD
• Drupalships with three form element validators in
field.module
• _element_validate_integer()
• _element_validate_integer_positive()
• _element_validate_number()
30. HOW TO CREATE A FIELD
• Create a formatter
• Use
hook_field_formatter_info
to setup a formatter
• Use
hook_field_formatter_view
for the actual formatter
31. HOW TO CREATE A FIELD
/**
* Implements hook_field_formatter_info().
*/
function collabfield_field_formatter_info() {
return array(
'collabfield_default' => array(
'label' => t('Default'),
'field types' => array('collabfield'),
),
);
}
32. HOW TO CREATE A FIELD
/**
* Implements hook_field_formatter_view().
*/
function collabfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'collabfield_default':
foreach ($items as $delta => $item) {
$element[$delta]['#markup'] = theme('collabfield_formatter_default', $item);
}
break;
}
return $element;
}
34. CUSTOM FORMATTERS
• Formatter to change default term reference link formatter
• Create as custom module
• Change URL taxonomy/term/[tid] to whatever/[tid]