0

This feels like it should be something people have tried, but I can't find anything after some googling. My use case is simple, I have a node type that only has 2 fields, body and media. The media field is, in fact, an entity reference field pointing to another node type (this is done because there are special cases where the media needs to be rendered HTML). All, I'm trying to do is render the referenced media field, by delta, in the main content area.

I can use the token [node:field-media:0] and print the title with no problem, I can print individual fields without an issue. Is there an existing method to print the rendered entity? I'm good with it using the default view mode.

I know I can code this up, but it seems like such a simple thing I figured there would already be a contrib module out there.

2

If I understand you correctly, you are trying to render a node inside another node.

In the past, I have used the Display Suite module to perform exactly this task.

With DS you can create custom view modes for content types. For example, for your media content type, you can create a new view mode called 'Embedded' and structure the display to include the fields you want.

Then, in your first content type, you can set the entity reference field to display a rendered entity and select the view mode you just created. The node page will display 'both' nodes as you've structured them.

Note: DS also exposes a Token view mode for you to be able to manage how the token version of a content type gets rendered. The token view mode is useful if you must use tokens (i.e. inject node into the body text), however rendering the entity just in the field formatting should cover a majority of the use cases.

DS is actually quiet deep in functionality - I use it for every build to structure content types, render html in the way I want, etc.

Hope that helps.

  • I have used DS before, it is a great module, but with this project we have not used it, and it is pretty heavyweight to add for something as simple as this. I went ahead and implemented a couple token hooks into a custom module just to get it working for now. – Trey Jun 1 '16 at 19:03
1

I would still like to find a module that supplies this functionality (with a little less bulk than DS), but in the interim, I just implemented this in a custom module:

function MYMODULE_token_info(){
    return array(
        'tokens' => array(
            'node' => array(
                'rendered' => array(
                    'name' => 'Rendered Node',
                    'description' => 'Render the Node using the provided view mode',
                    'dynamic' => true
                )
            )
        )
    );
}

function MYMODULE_tokens($type, $tokens, array $data = array(), array $options = array()){
    $return = array();
    if($type == 'node'){
        foreach($tokens as $key => $full){
            $parts = explode(':', $key);
            if($parts[0] == 'rendered'){
                $prep = node_view($data['node'], $parts[1]);
                $return[$full] = drupal_render($prep);
            }
        }
    }
    return $return;
}

Not terribly robust, and it will fail in the case of recursion (not something that can happen with my setup), but it works pretty simply.

This just adds a token [node:rendered:?] where ? is the view mode you wish to render the node in. So for my use case I can use:

[node:field-media:0:rendered:default]

And it prints the media node, at the 0 delta, attached through the entity reference field.

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.