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 ?

  • 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'). – kiamlaluno Jul 4 '16 at 11:50
  • True i ll change that – Kevin Jul 4 '16 at 11:57
1

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

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, privacy policy and cookie policy

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