1

I have a custom content entity. For this entity, I want add multiple uploaded files.

After I set this up, I looked at the database and saw:

file_managed => stores all files uploaded by a managed_file field

file_usage => links between your entity and the table file_managed

So for the managed_file field form, I don't need to create a field into my entity content (database), I just add a field into the EntityForm directly:

  $form['image'] = array(
      '#title'=>t('Image du produit'),
      '#type' => 'managed_file',
      '#upload_location' => 'public://produits/',
      //'#default_value' => array(),
      '#description' => t('png jpge jpg'),
      '#upload_validators' => array(
          'file_validate_extensions' => array('png jpg jpeg'),
      ),
      '#multiple'=>true,
  );

And then I can upload multiples files.

Then in my submit :

public function save(array $form, FormStateInterface $form_state) {
    $entity = $this->entity;


      foreach ($form_state->getValue('image') as $image) {
          $file = \Drupal\file\Entity\File::load($image);
          $file->setPermanent();
          $file->save();


      $file_usage = \Drupal::service('file.usage');
      $file_usage->add($file, 'boutique', 'produits_entity',$entity->id());


      }
}

I store image reference into the file_usage table and set it permanent. At this point all is good.

But I need to load all files when user want edit my entity, I can load this from the file_usage table and build my

'#default_value' => array($images)

I want to know if it's a good practice and if Drupal 8 work like that.

  • The ImageItem field type and ImageWidget field widget will handle storing in file_managed and file_usage. An image item field is an entity reference to a file entity. It is entirely possible and OK to manage files yourself as you have done in your question, but the custom code may not be necessary. – mradcliffe Jul 5 '16 at 13:00
  • the managed_file doesn't write into file_usage table. So i have no other solution to add custom code for add and remove files when i submit the entity. The only table where managed_file write is file_managed with ajax when i select a file. – Kevin Jul 6 '16 at 7:32
1

I don't need to create a field into my entity content (database), I just add a field into the EntityForm directly

...

I want to know if it's a good practice and if Drupal 8 work like that.

No, what you described is not good practice. For a Drupal entity to interact easily and correctly with a field, you should add the field to the entity, not add the widget to the form. It is probably possible to make it work but it will be harder to build and maintain.

  • But the link between my entity and my files are 1=>n, i cannot add a field into my entity... i need to add the id_entity reference into the file_managed and the file_usage.... – Kevin Jul 19 '16 at 15:49
  • Fields (including file fields) can be set to allow an unlimited number of values under field settings. – acrosman Jul 19 '16 at 17:19

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.