Hi guys,
I've been working recently with the Meta tags module along with a setup on search_api, views, facetapi, search_api_db, etc... with a standard page setup and was wondering about the possibility of integrating the tokens provided by facetapi with the Meta tags views module (extending Meta tags).
The idea here is to be able to display values provided by the facetapi tokens in the meta tags and other head markup elements on search enabled pages.
After looking into Facetapi Bonus, which brought me to Page Title, I came up with a patch that seems to have got the maintainer's attention, requesting further documentation (See: #1830952: Introduce two new hooks to allow token types and patterns alteration).
In short, it's about introducing 2 new hooks in metatag module's api, to allow exposing tokens to metatag patterns and altering patterns before replacement.
So when I started pushing my investigations a bit further, I found out that the hook for pattern replacement, for facetapi, could potentially be avoided if proper replacement of tokens could take place in their own modules.
Which brought me to look further deeper into facetapi.tokens.inc to try to figure out why the following exposed tokens in metatag wouldn't get any value:
- facetapi_results
- facetapi_active
- facetapi_facet
It seems the reason why, for example [facetapi_active:active-value], or any other value is not replaced is because no value is passed to the $data
array parameter of the facetapi_tokens
function (See facetapi-7.x-1.x-dev facetapi.tokens.inc, lines 127, 171, 204), leaving the following choices:
- Working with replacements in metatag, implement
hook_metatag_pattern_alter
in facetapi_bonus for example, based on what already exists for page_title integration (very much a copy/paste with few adjustments).
That's what has been suggested previously and one of the reasons for this post: #1830952: Introduce two new hooks to allow token types and patterns alteration. - Another approach would be to try to solve the problem from the
facetapi_tokens
side. For example, by providing fallback replacements in case of absence of the right$data
parameters (see ifs (!empty($data['....'])), lines 127, 171, 204) and this is the object of my request here.
I was wondering if it would be possible to add some kind of fallback or attempt to initialize the correct variables, based on the searchers $searchers = facetapi_get_active_searchers
which would allow loading $adapter
which is required for entering in any of the replacements calculation code.
We could for example add a function that would initialize the $data
variable:
function facetapi_tokens_defaults($type, $data = array())
to attempt to initialize token data based on active searchers (like it's done in facetapi_bonus.page_title.inc).
facetapi_tokens_defaults
would return an initialized $data
array variable if any active searcher is found.
This function would be then called at the top of facetapi_tokens
which would provide a default argument if nothing is passed through $data
.
Currently, the function responsible for putting together token replacements based on patterns is: facetapi_tokens
.
If an empty $data
array is passed no value is evaluated so we could could perhaps add the following lines at the top of the function:
// Attempt to initialize tokens based on current active searchers.
if (empty($data[$type])) {
$data = facetapi_tokens_defaults($type, $data);
}
I would assume it would make sense for all the facetapi_tokens replacement values for the results returned for facetapi_results, facetapi_active, and since in a default case facetapi_facet could return information on the last selected facet (same facet as facetapi_active) with different info/properties.
If $data is provided then nothing would change from the current behavior and if the facetapi_tokens_defaults
returns FALSE
, then it means no searcher was found (which would be the correct result on a node list view page, for example, since it wouldn't be a search enabled page).
I have already taken a quick try and came up with the attached patch against the 1.x branch [facetapi-7.x-1.2+10-dev (2012-11-02)].
[File attached as: facetapi-initialize-default-tokens-active-searchers-0.patch]
I would greatly appreciate if you could take a bit of time to take a look at the patch and give me your feedback on this feature request.
Please let me know if you would have any questions on any points/code/aspects mentioned in this ticket or patch, I would surely be glad to provide more information.
Any testing, comments or feedback would be highly appreciated.
Thanks in advance.
Comment | File | Size | Author |
---|---|---|---|
#1 | facetapi-initialize-default-tokens-active-searchers-1856436-1.patch | 2.01 KB | DYdave |
facetapi-initialize-default-tokens-active-searchers-0.patch | 2 KB | DYdave | |
Comments
Comment #1
DYdave CreditAttribution: DYdave commentedQuick fix for spaces indentation and changing status for review.
Updated patch: facetapi-initialize-default-tokens-active-searchers-1856436-1.patch.
Thanks in advance for your feedback.
Comment #2
DYdave CreditAttribution: DYdave commentedChanging status for review.
Comment #3
Jarviss CreditAttribution: Jarviss commentedBug with current search block it doubles current active items label+value.
For axample for active content type and facet link it shows for both active facet label and value, but it should show active content type and facel label + link
I applied this patch http://drupal.org/files/facetapi-initialize-default-tokens-active-search...
Facet API (facetapi) 7.x-1.3
Facet API Bonus (facetapi_bonus) 7.x-1.1+7-dev
Current Search Links (current_search_links) 7.x-1.x-dev
Current Search Blocks 7.x-1.3
Page Title works it shows active facel label
Also what I see is that Page Title for current 4 active items it shows 1 label and value. It is not correct because we need all active facet labels, values
1) Is there a way to fix current search block active items?
2) Is there a way to loop Labels and Values in Page Title?
Comment #4
Jarviss CreditAttribution: Jarviss commentedOk I found in Facet Api Bonus How to loop active facets in Page Title we need to use: list<[facetapi_active:facet-label]: [facetapi_active:active-value]>
and it works so 2-d question is done.
Comment #5
philipz CreditAttribution: philipz commentedIt's been a while but the patch seems to be working fine. I applied it manually though so don't know if still applies.
Comment #6
Anonymous (not verified) CreditAttribution: Anonymous commentedWhen this patch is applied, all of the names for current search facets are replaced with the most recently chosen facet regardless of if the token is set.