I have many resources (mainly PDFs) that need to be imported to my site. Previously the files would manually be placed in sites/default/files/mydir and linked to the node like this:

<a href="/sites/default/files/mydir/mypdf.pdf">Read More</a>

This does work, but I am guessing there are probably strong advantages to actually using the content system - perhaps being able to better organize/track/update files.

How do I place files inside of nodes so the file still appears inline with the rest of the content, while actually being tracked by Drupal?

  • Are you looking for the best method to set this up, or a way to add all of your existing files to the managed files that Drupal knows about? – Trey May 25 '16 at 20:11
  • I'm looking for the preferred method to set this up. – Jon Carlyon May 25 '16 at 21:23
  • Still unsuccessfully looking for the preferred method to manage and link files inline. – Jon Carlyon Jun 2 '16 at 16:31
  • 1
    Sorry Jon, I've been busy and haven't had time to test on this. I do a lot of module development, but most of my production sites remain on Drupal 7 until some of the larger contrib modules are more fleshed out for D8. I have updated my answer with the only solution I could get working in my limited time – Trey Jun 2 '16 at 17:09

UPDATE: I missed the 8 tag

Drupal 8.x


So this got trickier. The only way I was able to accomplish it was using these steps:

  1. Add a field to the node, I called mine "Media"
  2. Instead of selecting "File" from the dropdown list, I selected "Other" under reference, and "File" on the next page, this creates an entity reference field rather than a file field.
  3. Add a file through file/add
  4. In your node, start typing the filename of the file you've added
  5. In the body field, enter this token: [node:field_media-formatted:0:entity_reference_entity_view:default]

The token inserts the rendered entity for the reference field into your content, you can edit how it looks (for images e.g.) through the display settings for file types (May require the File Entity module), so you can display a link rather than a rendered file.

Someone pointed out in a thread I saw a couple of months ago:

Drupal 8 is stable with exactly 0 contributed modules installed

That said, moving to Drupal 8 with requirements that fall outside of the scope of core may be premature for someone who isn't prepared to develop their own solutions yet, and this particular solution may change since parts of it rely on contributed modules.

Hopefully this will be an appropriate answer in the future

Very similar to how it would be handled in Drupal 7, but because of module availability there are some tweaks. The modules you would need are Token, Token Filter, and File Entity Browser.

The steps are the same as below essentially, but I think the token may use the underscore instead of a dash: [node:field_files:0:url].

Also, once the Token module is installed you can see a list of tokens at admin/help/token.

Drupal 7.x

There are several mechanisms available to achieve this. If you are using a wysiwyg editor (such as CKEditor), they sometimes have plugins available to allow you to add files directly into the content, but based on what you've stated you need, I think possibly the Media, Token, and Token Insert modules would be a good place to start.

The media module provides a file browser and upload mechanism that works with the core Drupal file system, making it possible to reuse files easily in different pieces of content.

The Token module extends Drupal's core token mechanism, and the Token Insert module allows you to use tokens in text fields.

The basic steps would go something like this:

  1. Install all 3 modules, make sure you have the core File module enabled.
  2. Go to the "Manage Fields" page for the content type you plan to use these files in and add a "files" field.
  3. Add the files that would be used in your article to the files field, either through uploading or using the file browser.
  4. Reference the files by index in a token in the text field, a list of available tokens are available here.

This is untested so may require some tweaking, but if the machine name for your file field is "field_files", the link in the text area would probably look something like this:

<a href="[node:field-files:0:file:url]">Read More</a>

That would print the link for the first file that is attached to the node.

There will, of course, be some configuration pages you will have to go through for the media module and field settings, but it's hard to give you any direction on those since they are all very much dependent on your particular installation.

|improve this answer|||||
  • Media and Token Insert have not yet been ported for 8. – Jon Carlyon May 26 '16 at 15:46
  • My mistake, I didn't see the 8 tag. If you only plan to use each file once, this can still be achieved with the Token module and a simple file field – Trey May 26 '16 at 16:02
  • Check out the Token Filter module – Trey May 26 '16 at 16:10
  • i've updated my answer – Trey May 26 '16 at 16:17
  • I installed the modules and the dependencies. I enabled "Replaces global and entity tokens with their values" in /admin/config/content/formats/. I can use Devel to see the available tokens and see that '[node:field_public_forms]' contains a list of files used on the page, but I'm unable to select a single value. Adding anything else, like '[node:field_public_forms:0:url]' is not translated. What am I doing wrong here? – Jon Carlyon May 27 '16 at 14:39

I accomplished this by using Entity embed in WYSIWYG.

1) "Add Embeded Button" in /admin/config/content/embed

2) Configure button to desired needs

3) Create/configure new desired display mode for "File" in /admin/structure/display-modes/view.

4) Edit node that files will be placed in. Click new embed button, reference desired files. Set display mode to the newly created display.

The part of the resulting HTML I care about (download link) is nested within 3 divs, so I used a little JS to style it. I'm leaving Trey's answer as accepted, because tokens would be the better way to do this, but using tokens I couldn't figure out how to select a single value out of a field containing multiple values.

|improve this answer|||||

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.