I've custom widget which consist multiple input elements in hook_field_widget_form()
as part of one field (like address field).
The problem is that Drupal complains about:
this field cannot hold more than @count values
which happens in field_default_validate()
, when I've multiple input filled, so it doesn't recognise that this is one single field.
I've tried to change my cardinality in every possible place as below, but without success:
/**
* Implements hook_field_info().
*/
function my_address_field_field_info() {
return [
'my_address_field' => [
'label' => t('My Address Field'),
'settings' => array('cardinality' => FIELD_CARDINALITY_UNLIMITED),
'instance_settings' => array('cardinality' => FIELD_CARDINALITY_UNLIMITED),
'default_widget' => 'my_address_field_widget',
'default_formatter' => 'my_address_field_default',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
],
];
}
/**
* Implements hook_field_widget_info().
*/
function my_address_field_field_widget_info() {
return [
'my_address_field_widget' => [
'label' => t('Default'),
'field types' => ['my_address_field'],
'settings' => array(
'size' => 60,
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_NONE,
'default value' => FIELD_BEHAVIOR_NONE,
),
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
],
];
}
/**
* Implements hook_field_widget_properties_alter().
*/
function my_address_field_field_widget_properties_alter(&$widget, $context) {
$widget['cardinality'] = FIELD_CARDINALITY_UNLIMITED;
}
/**
* Implements hook_field_formatter_info().
*/
function my_address_field_field_formatter_info() {
return [
'my_address_field_default' => [
'label' => t('Default'),
'field types' => ['my_address_field'],
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
],
];
}
/**
* Implements hook_field_widget_form().
*/
function my_address_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$widget = array();
$widget['label'] = array(
'#title' => $instance['label'],
'#theme' => 'form_element_label',
);
$widget['address_line_1'] = array(
'#type' => 'textfield',
'#title' => t('Address line 1'),
);
$widget['address_line_2'] = array(
'#type' => 'textfield',
'#title' => t('Address line 2'),
);
// more text fields here ...
return $widget;
}
Including defining hook_field_validate
which I thought it'll override default field validation handler, but this was not the case.
What's the right way of assigning unlimited cardinality for my widget?
hook_field_widget_form()
(you might need to go one level deeper in the array you return), could you add your code for that function?