2

I am trying to render custom view and put the result as content of the node.

Here is my code

function my_theme_preprocess_node(&$variables)
{
    if (isset($variables['node']) && $variables['node']->getType() == 'custom_type') {
        $reference_view = $variables['node']->field_reference_view->first();
        if (!empty($reference_view)) {
            $view_name = $reference_view->getValue()['value'];
            $view = \Drupal\views\Views::getView($view_name);
            $view->setArguments(array());
            $view->setDisplay('block');
            $view->preExecute();
            $view->execute();
            $variables['embedded_view'] = $view->buildRenderable();

        }
    }

}

But this code shows only debug message and when I try to get output in a twig template file it is empty

    {{ node.embedded_view }}

What is wrong with my code. How can I render a view and set it as a content of node or just node field in order to print it in a twig template file.

3

Because your variable reference is incorrect. It should be:

{{ embedded_view }}

Since you added it to the $variables array as $variables['embedded_view'].

As an alternative you can get the Twig Tweak module. Example on the docs page:

{# You can pass arguments to the view if needed. #}
  <dt>View:</dt>
  <dd>{{ drupal_view('view_name', 'page_1') }}</dd>
2

I think that the better way is create a pseudo-field, is easy and very simple to arrange the field in the views modes.

Why you will create 2 templates when you can use only the node template?

Template files should only be considered when:

Adding & editing regions. Making global layout changes. Customizing your layout structure.

Why pseudo-field are useful?

Pseudo-fields are great if you have some content or data that you need to render together with that of a particular entity. And since you don't want to be hacky and hardcode all of this inside a template or preprocessor, you can use pseudo-fields to expose some control over this to the UI. This means that you can use the core drag-and-drop functionality to control their visibility and position relative to regular entity fields.

Example:

/**
 * Implements hook_entity_extra_field_info().
 */
function MY_MODULE_entity_extra_field_info() {
  $extra = [];

  $extra['node']['custom_type']['display']['pseudo_field_name'] = array(
    'label' => t('My own pseudo-field'),
    'description' => t('My pseudo-field'),
    'weight' => 100,
    'visible' => FALSE,
  );

  return $extra;
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function MY_MODULE_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  // Creating the pseudo-field     
  $reference_view = $entity->field_reference_view->first();
  $view_name = $reference_view->getValue()['value'];
  $view = \Drupal\views\Views::getView($view_name);

  if (is_object($view)) {
    $view->setArguments([]);
    $view->setDisplay('block');
    $view->preExecute();
    $view->execute();
    $content = $view->buildRenderable('block', $args);
  }

  $build['pseudo_field_name'] = $content;
}

Clean the cache and enjoy ;-)

enter image description here

Reference:

  • Thanks for the answer, you've already answered my question and suggested to use pseudo-fields, but unfortunately I didn't understand than. So basically the benefit is that you control the output of the field not inside a theme hook, but instead in a separate module ? – HelloMufecayo Mar 8 '17 at 18:02
  • @HelloMufecayo You need to create a custom module and put inside the code. With this you will have an aditional field in your Content Type Manage Display tab, so you can use it as a normal field in your Views Modes. In your Manage Display tab the Link field (in each Content Type) is a pseudo-field. – Adrian Cid Almaguer Mar 8 '17 at 18:23
  • thank you so much for the reply, but I am asking about benefits comparing to my approach, could you please add more info about that ? – HelloMufecayo Mar 8 '17 at 18:25
  • 1
    @HelloMufecayo, read the references pages – Adrian Cid Almaguer Mar 8 '17 at 18:26
  • 2
    @HelloMufecayo this is a benefit: You use only the node template, you don't need to create another template, and you can use the field in the Manage Display, this is very easy to use. – Adrian Cid Almaguer Mar 8 '17 at 18:34

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.