John John - 26 days ago 10
PHP Question

ezplatform render links with url and object name from multi-relational content item in content type

does anyone know now to create a custom view type for ez platform? The default 3 have been exhausted and we need a new one for 'link'
Alternatively, does anyone know how to use the

render( controller(
with a custom template as this would also resolve out block right now.

Basically, we have a multi-relational field in a content object used and we need to print links to all the related contentIds, path works great but we cannot find a way to extract the name of the content object for the link without doing some fairly funky tpl logic of passing in params.

EG: As a hack for now we can pass in "embed_type" as a custom param with the
render(controller("ez_content:viewAction"
to pull in an alternate view for the content object for a specific content type and view type.

{% if embed_type is defined %}
{% include "embed/#{embed_type}.html.twig" %}
{% else %}
<h1>{{ ez_field_value( content, 'name') }}</h1>
{% endif %}


However, this is very ugly and all we really want to do is use 1 template for all content types, so all we need to do is loop through the relational field and print links (as the only thing available in the content field: "destination ids"). I am sure there used to be this option in the docs but i cannot find it anymore eg:

{% set links = ez_field_value( footer, "first_links_row" ).destinationContentIds%}
{% for id in links %}
{{ render(controller("ez_content:viewAction", {"contentId": id, "template": "link.html.twig"})) }}
{% endfor %}


Where the link.html.twig would simple print the link:

<a href="{{ path( "ez_urlalias", {"contentId": id} ) }}">
{{ ez_field_value( content, "name" ) }}
</a>


If using a custom tpl is not possible with the
render (controller (
helper then a new custom view type would also fix this issue, but i cannot find documentation for either.

Answer Source

You can create a twig function that would do that. We have something like this:

Definition:

new Twig_SimpleFunction(
   'content_type_name',
    array($this, 'getContentTypeName')
),

Implementation:

public function getContentName($content, $forcedLanguage = null)
{
    if (!$content instanceof Content && !$content instanceof ContentInfo) {
        $contentInfo = $this->repository->getContentService()->loadContentInfo($content);
    } elseif ($content instanceof Content) {
        $contentInfo = $content->contentInfo;
    } else {
        $contentInfo = $content;
    }

    return $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo, $forcedLanguage);
}

```

which enables you to provide either content id, content info or content itself, and it returns translated content name