Ibexa DXP Discussions

Community discussion forum for developers working with Ibexa DXP

Retrieve Content Location field value

Hi community

How do I can retrieve a field value from eZ\Publish\Core\Repository\Values\Content\Location content object search result?.

I can use ez_content_name, but nothing else in twig.

If I use:

ez_field_value(item, 'url')

I get:

Type error: Argument 1 passed to eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\ContentExtension::getTranslatedFieldValue() 
must be an instance of 
eZ\Publish\API\Repository\Values\Content\Content, 
instance of eZ\Publish\Core\Repository\Values\Content\Location given

Any advice will be very appreciate!

Salut @hugospock,

eZ twig functions are essentially designed to handle Content type objects
https://doc.ezplatform.com/en/latest/guide/twig_functions_reference/

In most cases the content variable contains the Content.
In what condition do you find yourself with this item variable?
Maybe you can get some Content instead of Location?

Maybe twig functions work with contantInfo?

ez_field_value(item.contentInfo,'url')

To test.

Finaly, You can add a twig fonction.

service:
    smile.twig_extension:
        class: Smile\ToolsBundle\Twig\SmileTwigExtension
        arguments:
            - '@ezpublish.api.repository'
        tags:
            - { name: twig.extension }
class SmileTwigExtension extends \Twig_Extension
{
    public function __construct( Repository $repository ) {
        $this->repository = $repository;
    }
    public function getFunctions() {
        return array(
            new \Twig_SimpleFunction('smile_content_from_location', [$this, 'smile_content_from_location']),
        );
    }
    public function smile_content_from_location(Location $location)
    {
        return $this->repository->getContentService()->loadContentByContentInfo($location->contentInfo);
    }
}

Thank you very much @remy_php for all your suggestions.

All of them were very useful.

I found a temp solution modifying a query class found in ezplatform_demo code. I did modify LatestContentQueryType and MenuController changing template variable menuITems, now named contentItems. In this way I can use it from anyplace, like pagelayout and send parameters.

I don’t know if that is the most elegant solution or the better one, but it’s now working with ez_field_value().

MenuController:

    /**
     * Renders top menu with child items.
     *
     * @param string $template
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getChildNodesAction($template)
    {
        $locationSearchResults = $this->searchService->findLocations($this->menuQueryType->getQuery());

        $menuItems = [];
        foreach ($locationSearchResults->searchHits as $hit) {
            $menuItems[] = $hit->valueObject;
        }

        return $this->templating->renderResponse(
            $template, [
                'menuItems' => $menuItems,
            ], new Response()
        );
    }

Modified:

    /**
     * Renders top content with child items.
     *
     * @param string $template
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function getChildNodesAction($template)
    {
        $searchResults = $this->searchService->findContent($this->contentQueryType->getQuery());

        $contentItems = [];
        foreach ($searchResults->searchHits as $hit) {
            $contentItems[] = $hit->valueObject;
        }

        return $this->templating->renderResponse(
            $template, [
                'contentItems' => $contentItems,
            ], new Response()
        );
    }

As you can see,

$locationSearchResults = $this->searchService->findLocations($this->menuQueryType->getQuery());

Was change to:

$searchResults = $this->searchService->findContent($this->contentQueryType->getQuery());

The twig template:

{% for item in contentItems %}
   
{{ ez_field_value(item, "description") }}
{% endfor %}

In MenuQueryType.php insert this method:

    public function setTopLocationId(int $value)
    {
        $this->topLocationId = $value;
    }

Best regards.

Note:
In The twig template: section the <pre>

  tag didn’t work as expected.