drupaldrupal-viewsdrupal-9drupal-content-typesdrupal-entities

Drupal 9: Get rendered view within Content type


I have several views that I need rendered within pages. I have a field to pull in these views as blocks within the content editor, but I am running into an issue where the only display options for this field I get are "Label" or "Entity ID". How can I get Drupal to give me the view as a rendered entity instead? I'd prefer to not to have to use something like twig tweak's drupal_view() within templates, as the entity ID will change based on all the different pieces of content, and there are several pages that would need to use that function to render different views.

I have tried setting up the view field a handful of ways, but only am able to get "entity id" and "label" as options to display the view within the content. I'd like to be able to use "rendered entity" instead. I also tried to instead just use Views, but there is data needed from the associated content pages. Tried to use twig tweak, but as mentioned, there is a lot of variables, so I think receiving "rendered entity" for the views would be best, just not sure how to go about that.


Solution

  • In display

    You can just use views for this First create a view that displays the rendered entities you need. Create a "block display" for this view and add it via the structure > block layout under the "Main page content". Consider restricting this block to appear only for nodes of the "hosting" content type lets say basic page.

    The standard way to ascociate them with the view content with the hosting node page is to go to the views advanced settings and add an argument and maybe a relationship if needed. For the argument select the 'hosting' content id, select the provide default value option and then select to aquire the id from the url

    [![enter image description here][1]][1]

    this way you can relate the view content with the hosting page.

    You may also use twig tweaks module as you said or a module like panels another option to create entity reference fields in the hosting node and just select to render the referenced entity in the hosting content type display settings

    in node edit

    you may use this module https://www.drupal.org/project/inline_entity_form [1]: https://i.sstatic.net/YmFo1.png