Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Inserting attachments into a body with Drupal 8

Drupal 8 natively allows to insert images within a body field, of course if the format text used allows this functionality. But can we do the same and easily insert documents, attachments, within a body text ? We have many solutions with Drupal 8 to associate documents with content. From a dedicated field, allowing us to master its location on the rendered page, from paragraphs allowing the author to position his document wherever he likes, while maintaining mastery of the final rendering, or with the Media entity suite that also allows us to integrate media, including documents, into a body with the module entity embed. In short, there are many solutions.

We also have a very simple solution, configurable in one minute top chrono, to be able to upload a document into a body with the module D8 editor file upload. Let's quickly discover this module.

After installing the module, we will configure the text format, with CKEditor, we will use with the body field.

Configure the full HTML text format and click the corresponding button.

Les formats de texte de Drupal 8

You notice a new button (a paper clip) in the list of available buttons to activate the upload file widget. We drag and drop this button into the active toolbar.

La barre d'outils de CKEditor

We dropped this new button in the active toolbar (next to the Image button). This action displays the plugin settings that allow us to specify the file transfer directory, the allowed file extensions, and the maximum size of a file.

Activation du wigdet de chargement d'un fichier pour CkEditor

If you have enabled the Limit allowed HTML tags and correct faulty HTML in this text format, make sure that the tag has the possible attributes of data-entity-type and data-entity-uuid, as shown below.

les balises autorisées

And it's done. In less than a minute we have just configured the module and we can now upload as many documents as needed into our body text.

We can now upload a document into any body field that will use the full HTML format.

Widget de transfert de fichier

It is possible to rename the file to give it a more attractive name than the default file name.

Fichier transféré

And your content rendered with its attachment.

Rendu du contenu avec le fichier joint

The formatting capabilities of files associated with a content are limited. But it is not the vocation of this module that allows to simply upload a file, in the same way as the images, into a text long field type. For an equally simple rendering. For more advanced requirements for formatting the associated documents, or the needs related to control access to these documents, you will have to recourse to other solutions.

An effective, simple module that responds to an immediate and often vital need. Sometimes it is this kind of small details that can make a big difference in the choice in a tool, and meets the basic functions that any publisher expects to have out of the box. No ?

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web