Using the Matrix field type
So, I've used Matrix a lot outside of Low Variables, but I'm having a heck of a time getting it to work through LV. What I'm trying to do is create an very simple L10n implementation (most of which is being handled though the channel, but a couple bits of the page aren't handled that way, and it's out of scope for me to modify the channel entry template, so LV seems the next easiest approach....
Here's a screenshot of the LV setup:
http://screencast.com/t/SNmrCIdb6nz
Here's the code --
{exp:low_variables:pair var="lv_l10n_associations"}
{lv_l10n_associations search:l10n_regional_page="/{segment_1}/{segment_2}/{segment_3}" limit="1"}
{exp:low_variables:pair var="{l10n_set}"}
{exp:stash:set_value name="l10n_office_locations" value="{l10n_office_locations}"}
{exp:stash:set_value name="l10n_related_resources" value="{l10n_related_resources}"}
{exp:stash:set_value name="l10n_upcoming_events" value="{l10n_upcoming_events}"}
{/exp:low_variables:pair}
{/lv_l10n_associations}
{/exp:low_variables:pair}
I've tried various other itterations too. The best result I've managed was once I was able to get the correct set based on the URL, but it wouldn't get the correct data from that set's LV. That code looked something like this:
{exp:low_variables:pair var="lv_l10n_associations"}
{if {l10n_regional_page} == '{segment_1}/{segment_2}/{segment_3}'}
{exp:low_variables:pair var="{l10n_set}"}
{exp:stash:set_value name="l10n_office_locations" value="{l10n_office_locations}"}
{exp:stash:set_value name="l10n_related_resources" value="{l10n_related_resources}"}
{exp:stash:set_value name="l10n_upcoming_events" value="{l10n_upcoming_events}"}
{/exp:low_variables:pair}
{/if}
{/exp:low_variables:pair}
Anyway, I'm >< this close to building this out as a custom module, I just would prefer to use CP tools the site's editors are familiar with, especially since this would allow for a much more extensible approach... Any advice would be much appreciated!
Replies
kvetchus 30 Nov 2012 23:20
I updated it some more, and can give you a little better idea what's going on:
The code:
http://screencast.com/t/lUAT5IPI1
Here's a shot of the output in layout:
http://screencast.com/t/3arRxEOkFfqh
hopefully, that's a little more clear (didn't change the LV setup I posted already). I get that it seems to be returning the last row, probably because the search or if statement isn't working as expected, so it's just saving the last value. I don't get why the IF isn't working, and I also don't get why the l10n_set is getting the FIRST rather than LAST of those sets. If I manually change:
{exp:low_variables:pair var="{l10n_set}"}
to:
{exp:low_variables:pair var="lv_l10n_regions_en"}
or
{exp:low_variables:pair var="lv_l10n_regions_pt"}
or
{exp:low_variables:pair var="lv_l10n_regions_es"}
the correct values are output every time:
http://screencast.com/t/ZMC3nq74JfPP
That shows what happens when I manually hardcod "lv_l10n_regions_es" as the var.
Low 1 Dec 2012 09:51
A bit of a complex setup here, and I haven't used either Structure nor Stash, so I hope I can help.
First thing I noticed tho, is you're nesting the low_variables:pair tags. That might cause trouble, since the EE's template parser isn't smart enough to know which closing tag belongs to which opening tag. To work around this, try to use both the :pair and :parse tags; one for the inner and one for the outer tag. See if that helps.
kvetchus 3 Dec 2012 21:52
So -- I spend all day on this one and, around lunch I figured out the problem. It has to do with parse order. Near as I could figure out, the problem was related to how and when Stash lets you "set" and "get" values. The thing is, I could set the "associations" LV to parse early, but when I set the "sets" to parse early, the were not available in the dropdown in the "association" field (not sure what's up with that). So, as a result, the actual translation strings were not available to push into Stash until after Stash had captured data, resulting in unpredictable results.
So... I just banged out a custom method in PHP to do it. Basically, it ignores early/late parsing selections and does both the outer and inner loops shown in the code sort of at the same time and prints them out on Stash time (as it were). Still uses the matrix fields in the LV interface, which is rock solid, I just did the logic in PHP myself and used a custom tag array rather than the built-in tags.
Overall, it's a pretty handy implementation for a small-scale i10n project.