1

I use this field definition in the entity "message" class.

$fields['fichier'] = BaseFieldDefinition::create('file')
  ->setLabel(t('Fichier attaché'))
  ->setSetting('file_directory', 'messagerie/fichier') // dossier d'upload
  ->setSetting('max_filesize', '10MB') // taille max du fichier
  ->setSetting('file_extensions', 'pdf doc txt xls zip rar mp3 mp4 csv jpeg jpg ppt') // extensions autorisées, à séparer par un espace
  ->setSetting('description_field', FALSE) // si on veut activer un champ « description »
  ->setDisplayOptions('form', [
    'label' => 'hidden',
    'type' => 'file_generic',
    'weight' => 0,
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

In the associated form I need to set this field a multiple field (I can't find a way to set cardinality in the definition field). How can I do that ?

2

You can use BaseFieldDefinition::setCardinality() for that, e.g.:

$fields['fichier'] = BaseFieldDefinition::create('file')
  ->setLabel(t('Fichier attaché'))
  ->setSetting('file_directory', 'messagerie/fichier') // dossier d'upload
  ->setSetting('max_filesize', '10MB') // taille max du fichier
  ->setSetting('file_extensions', 'pdf doc txt xls zip rar mp3 mp4 csv jpeg jpg ppt') // extensions autorisées, à séparer par un espace
  ->setSetting('description_field', FALSE) // si on veut activer un champ « description »
  ->setDisplayOptions('form', [
    'label' => 'hidden',
    'type' => 'file_generic',
    'weight' => 0,
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE)
  ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
1

Set the cardinality to unlimited:

  ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.