eZ Platform Discussions

Render field in current langage

version22

#1

Hi! Hi!

I still have a strange problem.

In my pagelayout I have a twig helper that returns the content to me that contains manageable fields such as menu, footer, etc.

But when I do an ez_render_field it displays the eng-GB version while my site access is fre-FR.
(Content exists in both languages.)

pagelayout.html.twig

{% set site_settings = get_site_settings() %}
{{ ez_render_field(site_settings, 'header_contributable_bloc') }} {# Print eng-GB translation of the field although the siteaccess if FR  #} 
{{dump(app.request.locale)}} {# fr_FR #}

MyTwigExtension.php

    public function getSiteSettings()
    {
        $query = new Query();
        $query->filter = new Query\Criterion\LogicalAnd([
            new Query\Criterion\ContentTypeIdentifier('site_settings'),
            new Query\Criterion\Subtree(92),
        ]);
        $query->limit = 1;
        $searchResult = $this->repository->getSearchService()->findContent($query);
        foreach ($searchResult->searchHits as $hit) {
            /** @var Content $content */
            $content = $hit->valueObject;
            return $content;
        }
        return null;
    }

#2

If I load the right content directly from its ContentId I don’t have a pb.

    public function getSiteSettings($locale = null)
    {
        return $this->repository->getContentService()->loadContent(13700);
    }

The pb does not come from the render field but from my research.

In my system the site_settings is the first object of type site_settings under the root location.


#3

I just discovered the findSingle() function that corrects my pb.

    public function getSiteSettings()
    {
        return $this->repository->getSearchService()->findSingle(new Query\Criterion\LogicalAnd([
            new Query\Criterion\ContentTypeIdentifier('site_settings'),
            new Query\Criterion\Subtree(92),
        ]));
    }

But I don’t understand why the two versions of my function don’t return the same result…


#4

Hi @remy_php,
This is due to the implementaion of the loadContent() and findSingle() methods. If you take a look to the ContentService and SearchService Classes you will find both implementation there. Both methods call the internalLoadContent() where the mainLanguageCode from the ContentInfo will be loaded. I can assume that you have the alwaysAvailable checked in your ContentType. This is why you get the Content in the right language, because it is ‘fre-FR’.
If you follow the findContent() implementation you will see that there is no language set. I think the Content Handler is loading the content using the initial_language_id . You can find more information about the id in ezcontentobject.
Another way is that you force the language of the current siteaccess. See here:


#5

Hy @da_real,

Thank you for these explanations
Indeed, the alwaysAvailable is checked in my ContentType
And the initial_language_id is eng-GB

In my case, languages equal to :

array(1) { [0]=> string(6) "fre-FR" }

But

$query->filter = new Query\Criterion\LogicalAnd([new Query\Criterion\LanguageCode($languages)]);
foreach ($this->repository->getSearchService()->findContent($query)->searchHits as $hit) {
            return $hit->valueObject;
}

continues to return the English version.


#6

@remy_php Try transfering the $languages parameter as a second argument to findContent instead of using LanguageCode criterion: $this->repository->getSearchService()->findContent($query, $languages);