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

dibyajyoti.mallick’s picture

Hi,

Please check the link for creating custom list : https://www.drupal.org/docs/8/creating-custom-modules/create-a-custom-field-type

Thanks

nick.ev’s picture

i have already have a look on that page, but the examples are only for text fields, not list

nick.ev’s picture

i solved editing the storage definition line from

$properties['settore']=DataDefinition::create('list')->setLabel(t('Settore'));

to

$properties['settore']=DataDefinition::create('integer')->setLabel(t('Settore'));