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.
I create a custom module refered to "content_entity_example" module of to upload file.
is it possible to use BaseFieldDefinition here ?
This is my code
$validators = array(
'file_validate_extensions' => array('pdf doc docx xls xlsx ppt pptx png jpg jpeg'),
'file_validate_size' => array(file_upload_max_size()),
);
$fields['fid'] = BaseFieldDefinition::create('file')
->setLabel(t('File Name'))
->setDescription(t('The File of the associated event.'))
//->setSetting('target_type', 'file')
//->setSetting('handler', 'default')
->setSetting('upload_validators', $validators)
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'file',
'weight' => -3,
))
->setDisplayOptions('form', array(
'type' => 'file',
'description' => array( // doesn't work
'theme' => 'file_upload_help', // doesn't work
'description' => t('A Gettext Portable Object file.'), // doesn't work
), // doesn't work
'settings' => array(
'upload_validators' => $validators,
),
'weight' => -3,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
My problem is
1. using pdf doc docx xls xlsx ppt pptx png jpg jpeg file
- in description help informed Available extension is only txt
- i can upload file using extension above
- when submit button clicked i got error message that allowed extention is only txt.
2. using txt file
- i can not upload file and got an error that available extension are "pdf doc docx xls xlsx ppt pptx png jpg jpeg".
How to fix it ?
Thanks
Comments
Comment #2
darvanenYou need to add
->setSetting('file_extensions', 'png jpg jpeg svg ... etc ')
But I'm finding that the storage doesn't get automatically created for file fields, so that will be another problem.