Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By nick.ev on
Hi,
i need a custom field module with 3 fields: 2 are text and one is a select.
I have created the module, enable it and added to a content type without errors, but when i try to add a content with the field i have created i got the error
mod_fcgid: stderr: Uncaught PHP Exception InvalidArgumentException: "Cannot set a list with a non-array value." at /var/www/clients/client0/web7/web/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php line 59, referer: http://eagletest.evnetwork.lan/en/node/add/video_page
i think this error is due to some wrong code about the list field but i can't find any documentation on it can you help me?
Many thanks
here there are the portions of code where fields are defined and stored, settore is the list field.
FieldType definition
public static function schema(StorageDefinition $storage) {
$columns = [];
$columns['id_video'] = ['type' => 'char','length' => 255,];
$columns['titolo'] = ['type' => 'char','length' => 255,];
$columns['settore'] = ['type' => 'char','length' => 255,];
return [ 'columns' => $columns, 'indexes' => [], ];
}
public static function propertyDefinitions(StorageDefinition $storage) {
$properties = [];
$properties['id_video']=DataDefinition::create('string')->setLabel(t('ID Video'));
$properties['titolo']=DataDefinition::create('string')->setLabel(t('Titolo'));
$properties['settore']=DataDefinition::create('list')->setLabel(t('Settore'));
return $properties;
}
Widget definition
public function formElement(
FieldItemListInterface $items,
$delta,
Array $element,
Array &$form,
FormStateInterface $formState
) {
// id_video
$element['id_video'] = [
'#type' => 'textfield',
'#title' => t('ID id_video'),
// Set here the current value for this field, or a default value (or
// null) if there is no a value
'#default_value' => isset($items[$delta]->id_video) ?
$items[$delta]->id_video : null,
'#empty_value' => '',
'#placeholder' => t('ID video'),
];
// titolo
$element['titolo'] = [
'#type' => 'textfield',
'#title' => t('Titolo'),
'#default_value' => isset($items[$delta]->titolo) ?
$items[$delta]->titolo : null,
'#empty_value' => '',
'#placeholder' => t('Titolo'),
];
//settore
$element['settore'] = [
'#type' => 'select',
'#title' => t('Settore'),
'#options' => [
'1' => $this->t('UV'),
'2' => $this->t('TX'),
'3' => $this->t('HY'),
],
'#default_value' => isset($items[$delta]->settore) ?
$items[$delta]->settore : null,
'#empty_value' => '',
'#placeholder' => t('Settore'),
];
return $element;
}
Comments
Hi,
Hi,
Please check the link for creating custom list : https://www.drupal.org/docs/8/creating-custom-modules/create-a-custom-field-type
Thanks
i have already have a look on
i have already have a look on that page, but the examples are only for text fields, not list
i solved editing the storage
i solved editing the storage definition line from
to