Best way to define entity and additional custom logic (CRUD) in module 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):


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.
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.
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?
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:


Create a field formatter plugin for file fields in a custom module: drupal generate:plugin:fieldformatter --field-type=file
Create a content type: drupal generate:entity:bundle --bundle-name=company --bundle-title=Company
Add a multiple-value file field to the content type.
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