As far as I can tell, there is no way to set the title of a facet.
In facets-item-list.html.twig:
- title: The title of the list.
{%- if title is not empty -%}
<h3>{{ title }}</h3>
{%- endif -%}
This title is always empty, and I can see no way to set it in the CMS.
Additionally, I can't preprocess it to set the title variable, because in template_preprocess_facets_item_list() there is no way to tell which facet I am looking at. So I can't fetch the administrative title, or give it a title according to facet ID.
Both theme hooks should pass along some kind of context so that the preprocess functions and templates are able to do different things to different facets.
Example:
function facets_theme($existing, $type, $theme, $path) {
return [
'facets_result_item' => [
'variables' => [
'facet_id' => '', // add this
'value' => '',
'show_count' => FALSE,
'count' => NULL,
'is_active' => FALSE,
],
],
'facets_item_list' => [
'variables' => [
'facet_id' => '', // add this
'items' => [],
'title' => '',
'list_type' => 'ul',
'wrapper_attributes' => [],
'attributes' => [],
'empty' => NULL,
'context' => [],
],
],
];
}
That way I can load the Facet entity in preprocess to inspect it, or I can have a switch() statement based on facet ID.
Comment | File | Size | Author |
---|---|---|---|
#14 | 2877634.patch | 4.24 KB | borisson_ |
Comments
Comment #2
Rob230 CreditAttribution: Rob230 commentedAh I see that the facet ID is in the attributes array, so that at least solves that part of my problem.
Comment #3
borisson_Related to #2855320: Provide facet id and raw value in Facets summary result items but not the same.
I hadn't looked at this, because I tend to use the block title instead for adding titles.
Comment #4
borisson_Actually fixed with #2855320: Provide facet id and raw value in Facets summary result items's latest patch, that passes the facet in, I think that means we can close this issue.
Comment #5
andreasderijckeI do not agree.
On multi language site, you can translate the facet name/title on the facet, which makes sense.
It doesn't make sense you have to translate the facet block to have the facet title translated on the page.
This should be a matter of setting the correct value for 'title', as it's all there in the twig.
Patch attached does the trick for me, without the need for additional preprocessing or twig overrides.
Comment #6
borisson_Forgot to apply the original patch, so no interdiff, but the patch is small enough.
Comment #7
borisson_I'm very anxious to commit this, as it might break exisiting layouts, but it is correct and the previous behavior wasn't. I have tweeted to gather feedback: https://twitter.com/Borisson/status/964790347363741696
Comment #8
remydenton CreditAttribution: remydenton at Pegasystems commentedThe way I see it, existing users will fall into one of these categories:
title
withfacet.label
, perhaps).title
in custom preprocessing without overriding facets-item-list.html.twig.I can't think of an elegant way to handle this that wouldn't potentially have unintended changes for at least one of the four groups above. With the current patch, the only side affect I see is that groups 1 and 3 may get a title that they didn't want (most of group 1 probably did want the titles and just didn't know it, while group 3 was doing something at least a little unorthodox).
It's not ideal, but also not the end of the world. If it were me, I would commit it, mention it in the release notes, and remind anyone who complains that the module is still in beta :)
Comment #9
remydenton CreditAttribution: remydenton at Pegasystems commentedThen again, in comment #3, borisson_ said:
Hardly "unorthodox" as I mentioned in my last comment, and probably something a lot of people might do, especially as site builders. In that case, my last comment might be too presumptuous as it would break things for all those folks...
Comment #10
borisson_Discussed this at the belgian user group sprint just now with @swentel, @andreasderijcke and @StryKaizer. We're going to make this configurable to make sure that not all sites break. Going to do this now.
Comment #11
borisson_As discussed.
Comment #12
borisson_Now with test-coverage.
Comment #13
borisson_NW based on the testfails.
Comment #14
borisson_This should fix the test.
Comment #16
Nick_vhComment #20
borisson_That should've been swentel. Fixing that.