Note: Requires content.module.');
}
}
/**
* Implementation of hook_field_info().
*/
function options_field_info() {
return array(
'options' => array('label' => 'Options'),
);
}
/**
* Implementation of hook_field_settings().
*/
function options_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array();
$form['options_list'] = array(
'#type' => 'textarea',
'#title' => t('Option Choices'),
'#default_value' => $field['options_list'] ? $field['options_list'] : '',
'#required' => 1,
'#description' => t("Enter a list of options to be offered for this field, separated by '|' (i.e. 'Red|Green|Blue'). If the field is required, a blank value will not be allowed. If the field is not required, a blank value will be automatically added as the first option in the list. "),
);
return $form;
case 'validate':
break;
case 'save':
return array('options_list');
}
}
/**
* Implementation of hook_field().
*/
function options_field($op, $node, &$field, $a2, $a3, $a4) {
//print_r($field);
switch ($op) {
case 'load':
$result = db_query("SELECT field_options FROM {node_field_options_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
if ($field['multiple']) {
$values = array();
while ($value = db_fetch_object($result)) {
$values[] = array('value' => $value->field_options);
}
$additions = array($field['field_name'] => $values);
}
else {
$value = db_fetch_object($result);
$additions = array($field['field_name'] => array('value' => $value->field_options));
}
return $additions;
case 'view':
$output = '';
$node_field = $node->$field['field_name'];
$node_field['view'] = options_field_view($node, $field);
$node->$field['field_name'] = $node_field;
$output = theme_form_element($field['widget']['label'], $node_field['view']);
return '
'.$output .'
';
case 'insert':
$node_field = $node->$field['field_name'];
if ($field['multiple']) {
foreach ($node_field as $delta => $item) {
db_query("INSERT INTO {node_field_options_data} (nid, vid, field_name, delta, field_options) VALUES (%d, %d, '%s', %d, '%s')", $node->nid, $node->vid, $field['field_name'], $delta, $item['value']);
}
}
else {
db_query("INSERT INTO {node_field_options_data} (nid, vid, field_name, field_options) VALUES (%d, %d, '%s', '%s')", $node->nid, $node->vid, $field['field_name'], $node_field['value']);
}
return;
case 'update':
$node_field = $node->$field['field_name'];
// Delete and insert, rather than upoptions, in case a field was added.
db_query("DELETE FROM {node_field_options_data} WHERE vid = %d AND field_name = '%s'", $node->vid, $field['field_name']);
if ($field['multiple']) {
foreach ($node_field['value'] as $delta => $item) {
db_query("INSERT INTO {node_field_options_data} (nid, vid, field_name, delta, field_options) VALUES (%d, %d, '%s', %d, '%s')", $node->nid, $node->vid, $field['field_name'], $delta, $item['value']);
}
}
else {
db_query("INSERT INTO {node_field_options_data} (nid, vid, field_name, field_options) VALUES (%d, %d, '%s', '%s')", $node->nid, $node->vid, $field['field_name'], $node_field['value']);
}
return;
case 'delete':
// Delete using nid rather than vid to purge all revisions.
db_query("DELETE FROM {node_field_options_data} WHERE nid = %d AND field_name = '%s'", $node->nid, $field['field_name']);
return;
}
}
/**
* Implementation of hook_widget_info().
*/
function options_widget_info() {
return array(
'options_select' => array(
'label' => 'Select List',
'field types' => array('options'),
),
'options_radio' => array(
'label' => 'Radio Boxes',
'field types' => array('options'),
),
'options_checkbox' => array(
'label' => 'Checkboxes',
'field types' => array('options'),
),
);
}
/**
* Implementation of hook_widget_settings().
*/
function options_widget_settings($op, $widget) {
switch ($op) {
case 'form':
$form = array();
$form['options_view_multiple'] = array(
'#type' => 'radios',
'#title' => t('View multiple values'),
'#default_value' => $field['options_view_multiple'] ? $field['options_view_multiple'] : 'comma',
'#description' => t("If multiple values are allowed, how should they be displayed in views?"),
'#options' => options_view_multiple($widget),
);
return $form;
case 'validate':
break;
case 'save':
return array('options_view_multiple');
}
}
/**
* Implementation of hook_widget().
*/
function options_widget($op, &$node, $field) {
switch ($op) {
case 'form':
$form = array();
$node_field = $node->$field['field_name'];
$type = str_replace('options_','',$field['widget']['type']);
$form[$field['field_name']] = array('#tree' => TRUE);
$form[$field['field_name']]['value'] = array(
'#type' => $type,
'#title' => t($field['widget']['label']),
'#default_value' => $node_field['value'],
'#required' => $field['required'],
'#multiple' => $field['multiple'],
'#options' => options_options($field),
);
return $form;
case 'validate':
return;
}
}
/**
* Implementation of hook_field_view() which performs any translation necessary.
*/
function options_field_view(&$node, $field) {
foreach ($node->$field['field_name'] as $delta => $item) {
$field['value_list'][] = $item['value'];
}
$options = options_options($field);
if ($field['multiple']) {
foreach($field['value_list'] as $item) {
$list[] = $options[$item];
}
$output = options_show_multiple($list, $field['widget']);
}
else {
$output = $options[$field['value_list'][0]];
}
return ($output);
}
/*
* function to create an array of possible formats for viewing multiple values
*/
function options_view_multiple($widget) {
return array(
'comma' => t('One line, separated by commas'),
'newline' => t('Each on new line'),
'list' => t('Bulleted list'),
);
}
function options_show_multiple($list, $widget) {
switch ($widget['options_view_multiple']) {
case ('newline'):
$output = implode('
', $list);
break;
case ('list'):
$output = theme('item_list', $list);
break;
default:
$output = implode(', ',$list);
}
return $output;
}
/*
* function to create an array of available options for this field
* create a 0 key for a blank field, start other values at 1
* remove the 0 option if the field is required
*/
function options_options($field) {
$vals = explode('|',check_plain($field['options_list']));
$options = array_merge(array(0 => ''), $vals);
if ($field['required']) array_shift($options);
return $options;
}
/**
* Implementation of hook_views_tables().
*/
function options_views_tables() {
$tables = array();
$fields = _content_fields();
foreach ($fields as $field) {
if ($field['type'] == 'options') {
$tables['node_field_options_data_'. $field['field_name']] = array(
'name' => 'node_field_options_data',
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'vid',
),
'right' => array(
'field' => 'vid',
),
'extra' => array('field_name' => $field['field_name']),
),
'fields' => array(
'field_options' => array(
'name' => 'Options: '. $field['field_name'],
'sortable' => TRUE,
'handler' => '_options_filter_options',
'options_field' => $field,
),
),
'sorts' => array(
'field_options' => array('name' => 'Options: '. $field['field_name']),
),
'filters' => array(
'field_options' => array(
'name' => 'Options: '. $field['field_name'],
'list' => options_options($field),
'operator' => array(
'=' => '=',
),
),
),
);
}
}
return $tables;
}
/*
* views handler to convert key of option field to its respective value
*/
function _options_filter_options($field, $operator, $value) {
$options = options_options($field['options_field']);
return $options[$value];
}