I'm trying to create a field that is a combination of two fields. The first field needs to a reference to another entity (just like in the entityreference module). The second field is just a text field (using the textarea widget).
Here's the code I have so far.
sample.install:
function sample_field_schema($field) {
$schema = array(
'columns' => array(
'place_id' => array(
'type' => 'int',
'unsigned' => true,
'not null' => true
),
'quote' => array(
'type' => 'text',
'size' => 'big',
'not null' => false
)
),
'indexes' => array(
'place_id' => array('place_id')
),
'foreign keys' => array()
);
$entity_type = 'place';
$bundles = field_info_bundles('node');
if (!empty($bundles['place'])) {
$schema['foreign keys']['place'] = array(
'table' => 'place',
'columns' => array('place_id' => 'nid')
);
}
return $schema;
}
sample.module:
function sample_field_info() {
return array(
'sample_field' => array(
'label' => t('Label'),
'description' => t('description.'),
'default_widget' => 'sample_field_widget',
'default_formatter' => 'sample_field_formatter',
'settings' => array(),
'instance_settings' => array(),
)
);
}
function sample_field_formatter_info() {
return array(
'sample_field_formatter' => array(
'label' => t('sample formatter'),
'field types' => array('sample_field')
)
);
}
function sample_field_formatter_view($entity_type, $entity, $field, $instance, $lang, $items, $display) {
$element = array();
switch ($display['type']) {
case 'sample_field_formatter':
// I'm not really familiar with these bits ... this is just to output *something*
$element[]['#markup'] = var_export($items, true);
break;
}
return $element;
}
function sample_field_widget_form(&$form, &$form_state, $field, $instance, $lang, $items, $delta, $element) {
switch ($instance['widget']['type']) {
case 'sample_field_widget':
$element['place_id'] = array(
'#type' => 'textfield',
'#maxlength' => 1024,
'#title' => t('Place'),
'#default_value' => isset($items[$delta]['place_id']) ? $items[$delta]['place_id'] : null,
'#required' => $element['#required'],
'#weight' => (int)$element['#weight'],
'#delta' => $delta,
'#size' => 'big',
'#autocomplete_path' => 'api/find/places'
);
$element['quote'] = array(
'#type' => 'textarea',
'#title' => t('Quote'),
'#default_value' => isset($items[$delta]['quote']) ? $items[$delta]['quote'] : null,
);
break;
}
return $element;
}
function sample_widget_error($element, $error, $form, &$form_state) {
switch ($error['error']) {
case 'sample_field_invalid':
form_error($element, $error['message']);
break;
}
}
function sample_field_is_empty($item, $field) {
if (empty($item['sample_field'])) {
return true;
}
}
function sample_field_widget_info() {
return array(
'sample_field_widget' => array(
'label' => t('Sample Field Widget'),
'field types' => array('sample_field'),
'settings' => array(),
'behaviors' => array(
'default value' => FIELD_BEHAVIOR_NONE
)
)
);
}
So this all gives me a new field type and a new widget for that field. When I edit/create new content that has this field type, the widget shows up properly with both fields but when I submit to insert/update the content, the fields are not saved. How can I get the form to save the field data for this custom field and how can I get that data to be loaded again when the content is edited?