Twig, blocks, and localizedfields


#1

In my class, I have a block that contains a localizedfield. I can’t seem to figure out how to make twig reference the fields within the localizedfield as I iterate the block:
image


#2

I got further but is there not some kind of twig/pimcore idiom to by-pass traversing through the localizedfield as so:


#3

Never use the direct access to variables. From Pimcore v5.5.0 and higher the variables of the DataObjects are now protected so you can’t access them directly.
Always use the Getters like in your case part.getPartName


#4

@davidhoeck in twig that doesn’t matter, twig always checks for a getter first and then for a direct property.


#5

I know, but I had the same error also and my solution was to use directly the getter … maybe it solves his problem too


#6

The getter isn’t working, which is why I went elsewhere. How do you declare the language within twig?

dump($mlist[0]['localizedfields']->getData()->getItems());
shows:

but in twig:

{{ dump(step.localizedfields.getData.getItems) }}
it returns an assoc. array; however, when you attempt to access it via the key, it only sees “en_US” and not “zh”, which throws an key error.

I’m missing something…


#7

The twig dump:

{{ dump(step.localizedfields.getData.getItems.en_US) }}
doesn’t throw an error.
{{ dump(step.localizedfields.getData.getItems.zh) }}
does throw an error.


#8

The locale within Twig is defined via the global Symfony ‘locale’ parameter


#9

Makes sense for the default, but then how do you show multiple languages on a single page?


#10

{{ dump(step.localizedfields.getData.getItems|keys) }}
shows:
image
but I don’t seem able to access the values via the key “zh”
{{ dump(step.localizedfields.getData.getItems.zh) }}
throws a key error.


#11

This works in the console, however:
dump($mlist[0][‘localizedfields’]->getData()->getItems()[‘zh’]);


#12

I think I finally figured it out. One of the blocks didn’t have Chinese registered so it worked on all those with it and throws the error if not. Doh.