0

This is how i create my field in my Entity content :

  $fields['fid'] = BaseFieldDefinition::create('file')
      ->setLabel(t('Photo'))
      ->setRequired(false)
      ->setDescription(t('Product image'))
      ->setSettings(array(
          'upload_location' => 'public://produits/',
          'upload_validators' => array(
              'file_validate_extensions' => array('png jpg jpeg'),
          ),
      ))
      ->setDisplayOptions('view', array(
          'label' => 'above',
          'type' => 'file',
          'weight' => -3,
      ))
      ->setDisplayOptions('form', array(
          'type' => 'file',
          'settings' => array(
              'upload_location' => 'public://produits/',
              'upload_validators' => array(
                  'file_validate_extensions' => array('png jpg jpeg'),
              ),
          ),
          'weight' => -1,
      ))

      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

The field is create in my form but when i try to upload a file :

I got this error :

Only '.txt' extension is authorized

Edit 1 :

In fact my settings are never read, after upload a .txt for test, the directory is wrong.

Any idea about this ?

2
  • As side note, the string you pass to t() needs to be in English. You don't use a call like t('La photo de votre product').
    – avpaderno
    Commented Jul 4, 2016 at 11:50
  • True i ll change that
    – Kevin
    Commented Jul 4, 2016 at 11:57

2 Answers 2

4

Working example from something I'm working on:

'image' => BaseFieldDefinition::create('file')
      ->setLabel('Image')
      ->setSettings([
        'uri_scheme' => 'public',
        'file_directory' => 'credentialing_providers',
        'file_extensions' => 'png jpg jpeg',
      ])
      ->setDisplayOptions('view', array(
        'label' => 'above',
        'type' => 'file',
        'weight' => -3,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'file',
        'weight' => -1,
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE),
1
  • 1
    Nice work, wish I could give you more than 10 points for this!
    – Darvanen
    Commented Mar 7, 2022 at 4:37
1

The only way i have found is to move this param into the Entityform :

  $form['fid'] = array(
      '#type' => 'managed_file',
      '#upload_location' => 'public://produits/',
      '#default_value' => $entity->get('fid')->value,
      '#description' => t('png jpge jpg'),
      '#upload_validators' => array(
          'file_validate_extensions' => array('png jpg jpeg'),
      )
  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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