I have a custom auto-complete widget that I use to create a custom field that is used to create a custom content type in a custom module.
I have hook_field_widget_info
function MODULE_field_widget_info()
{
return array(
"MODULE_taxonomy_autocomplete" => array(
"label" => t("MODULE Autocomplete term widget (tagging)"),
"field types" => array("taxonomy_term_reference"),
"settings" => array(
"size" => 60,
"autocomplete_path" => "taxonomy/autocomplete",
),
"behaviors" => array(
"multiple values" => FIELD_CARDINALITY_UNLIMITED,
),
),
);
}
I have hook_field_widget_form
function MODULE_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element)
{
$tags = array();
foreach ($items as $item)
{
$tags[$item["tid"]] = isset($item["taxonomy_term"]) ? $item["taxonomy_term"] : taxonomy_term_load($item["tid"]);
}
$element += array(
"#type" => "textfield",
"#default_value" => taxonomy_implode_tags($tags),
"#autocomplete_path" => $instance["widget"]["settings"]["autocomplete_path"] . "/" . $field["field_name"],
"#size" => $instance["widget"]["settings"]["size"],
"#maxlength" => 1024,
);
return $element;
}
I have hook_field_widget_error
function MODULE_field_widget_error($element, $error, $form, &$form_state)
{
form_error($element, $error["message"]);
}
Here I am creating the field of the content type
field_create_field(
array(
"field_name" => "field_MODULE_tags",
"type" => "taxonomy_term_reference",
"bundles" => array(
"node" => array(
"CONTENT_TYPE",
),
),
"cardinality" => "-1",
"settings" => array(
"allowed_values" => array(
array(
"vocabulary" => "TAXONOMY_LIST",
"parent" => 0,
),
),
),
)
);
field_create_instance(
array(
"field_name" => "field_MODULE_tags",
"entity_type" => "node",
"label" => "Tags",
"description" => "",
"bundle" => "CONTENT_TYPE",
"widget" => array(
"weight" => 1,
"type" => "MODULE_taxonomy_autocomplete",
"settings" => array(
"size" => 60,
"autocomplete_path" => "taxonomy/autocomplete",
),
),
)
);
All of this appears to work fine, the content type shows the correct field, the auto complete works great, but when I try to save a new node of the custom type, I get the following error:
PHP Fatal error: Cannot unset string offsets in /modules/field/field.default.inc on line 41
If I look at field.default.inc file I see
function field_default_extract_form_values($entity_type, $entity, $field, $instance, $langcode, &$items, $form, &$form_state) {
$path = array_merge($form['#parents'], array($field['field_name'], $langcode));
$key_exists = NULL;
$values = drupal_array_get_nested_value($form_state['values'], $path, $key_exists);
if ($key_exists) {
// Remove the 'value' of the 'add more' button.
unset($values['add_more']);
$items = $values;
}
}
where line 41 is unset($values['add_more']);
If I output $values
, I get a string where unset is expecting an array.
How can I make my value an array?
I've tried using hook_form_alter to add a custom submit function so I can manipulate the value but the custom submit never gets fired (which is weird).