I want to create a new content type, e.g. company. This company needs an additional field which is a multiple file upload field. When the full node of the content type is displayed, I want to run some code in a controller which reads the uploaded files, executes logic and sends information to the twig template. However I'm unsure how to proceed. As far as I can see there are three options (of which the first isn't really an option):

  1. I can create an entity type through the admin interface, and create a new node.html.twig template file, however this lacks the ability to execute custom PHP logic and send the information to twig, as i only have the twig template.

  2. I can create a content type in a module and define the fields through the admin. If i understand correctly, this can be done through drupal generate:entity:content with the Drupal Console.

  3. I can create a module in which I define the content type, and hard-code all the fields. This is less flexible but maybe a stronger option: if something happens with the database I don't have to recreate the structure.

Both option 2 and option 3 can be viable. I've used Symfony2 before and using that I would take option 3, however it seems this option is over-complex in D8. If I understand correct in D8 fields should be defined through the admin interface. And a custom template can be created in the module eventually.

How should i tackle this issue? Which option is best? What is the best way to create a controller, which is used in displaying an entity/content type which is created via the D8 admin interface?

closed as primarily opinion-based by Mołot, Pierre.Vriens, kiamlaluno Feb 15 '16 at 21:39

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.


Given that you want to change the formatting of a file field, then I would create a FieldFormatter plugin, which could probably extend FileFormatterBase (or one of its sub classes). This would allow the formatter to be a configurable option for file fields in the Manage Display interface (or as part of a content type configuration YAML that your module provides).

By implementing FormatterInterface::viewElements, you can add to the render array that is returned.

To summarize:

  1. Create a field formatter plugin for file fields in a custom module: drupal generate:plugin:fieldformatter --field-type=file
  2. Create a content type: drupal generate:entity:bundle --bundle-name=company --bundle-title=Company
  3. Add a multiple-value file field to the content type.
  4. Configure the file field to be displayed with your custom field formatter plugin (in Manage Display).

Edit: Added drupal console commands as far as I know them

  • Thank you for your suggestions. Isn't it more easy to create a CRUD like controller with an entity? And instead of defining a custom field formatter, just create a new field.html.twig for the specific field for display purposes? Your – kilar Feb 15 '16 at 20:50
  • I think it would be easier to write a FieldFormatter plugin that can attach itself to a bundle of a Content Type (Node Type or any entity really) because it is one class rather than implementing a custom ContentEntityType entity and all the controllers and schemas. The CRUD is already handled by the File module's field type and field widget plugins so you're just modifying the behavior of the output, right? – mradcliffe Feb 15 '16 at 20:52
  • Does that make sense? I would use the built-in node functionality rather than creating a custom entity for Company. I'm not as familiar with Drupal Console, but the console command you used creates a custom ContentEntity entity type, not a Content Type (from node module). I think that the latter is done with drupal generate:entity:bundle. – mradcliffe Feb 15 '16 at 20:56
  • I like what you are suggesting and it indeed seems like a much cleaner and more simplistic approach. Let me try it and I'll report back. – kilar Feb 15 '16 at 21:04
  • I've tried your solution and it works! So thank you for that. The issue i'm now having is that i uploaded multiple files and i want to process all these files in one function, and the fieldformatter processess each uploaded file as a seperate field. But i am going to have a look at function hook_preprocess_HOOK – kilar Feb 18 '16 at 11:46

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