Hi,
I'm trying to make dynamic tags work with Views. In the views-view.tpl.php I'm doing some operations that result with variables that should be used as meta description and title. E. g. :
$title = 'This is page title';
$description = 'This is meta description';
I was trying to add tags with drupal_add_html_head:
$meta_description = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'description',
'content' => 'some description here'
)
);
drupal_add_html_head( $meta_description, 'meta_description' );
It doesn't work. However, as soon as I change 'meta' to 'metas' and 'description' to 'descriptions' it works. Obviously it's not possible to define description tag if it's already outputted by Metatag module. Since I use Metatag in many other cases, I don't want to uninstall it.
I suppose that I have to use metatags_view_alter hook, but I can't make it work either. Can someone post a working example of it in this case? This is my latest try:
$meta_description = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'description',
'content' => 'some description here'
)
);
hook_metatag_metatags_view_alter( $meta_description, 'meta_description' );
Comments
Comment #2
deepakrmklm CreditAttribution: deepakrmklm at Zyxware Technologies commentedThis will work.
Comment #3
1mundus CreditAttribution: 1mundus commentedThank you for the answer, but I can't make it work. How should that code be called in views-view.tpl.php?
Comment #4
DamienMcKenna@1mundus: You won't be able to add code via views-view.tpl.php, it will need to go in the template.php file for your theme or a custom module.
Comment #5
1mundus CreditAttribution: 1mundus commented@DamienMcKenna
Sure, I understand that the function goes to template.php, but is there a way to pass data to that preprocess function via views-view.tpl.php?
Comment #6
deepakrmklm CreditAttribution: deepakrmklm at Zyxware Technologies commentedDear 1mundus,
The template file will execute at the end and its not possible to push meta data from tpl files. You can get the dynamic data from views, in $vars variable.
Thanks,
Deepak R
Comment #7
1mundus CreditAttribution: 1mundus commentedThank you deepak_zyxware. I'll have to find another way to achieve what I want, since none of the data I want to push to meta tags isn't available in default views variables.
Comment #8
DamienMcKenna@1mundus: The values that are output in the views-view.tpl.php file can be changed via hook_preprocess_views_view. Please give it a try.
Comment #9
deepakrmklm CreditAttribution: deepakrmklm at Zyxware Technologies commented@DamienMcKenna, The requirement was not to alter the views data, but to push the dynamic data as meta data. To update the values in view output, we can use hook_views_pre_render(&$view).
@1mundus, I believe, its not a good idea to push data from tpl files, what ever the requirement was. If you can explain what does the view outputs (contents in views) and what kind of dynamic data you need to get from views, will sort out a solution.
Comment #10
DamienMcKenna@deepak_zyxware: I'm not talking about modifying the Views data, I'm talking about the data that you're saying you want to use in the views-view.tpl.php template being available in the preprocess function, so that you can then use those values in the meta tags.
Comment #11
deepakrmklm CreditAttribution: deepakrmklm at Zyxware Technologies commentedYes @DamienMcKenna, all data available in tpl will be available in preprocess, and can push as meta data from there (and not from tpl). Based on the requirement the hook implementation may vary.
Comment #12
1mundus CreditAttribution: 1mundus commented@deepak_zyxware and @DamienMcKenna
It's a view that pulls data from multiple sources, not just Drupal, so it's by far the simplest solution for me to do everything in PHP instead of Drupal. The only thing left is to alter metadata. At the moment that view uses meta tags set in Views interface, which is not dynamic, since I can't use desired variables as tokens.
The view does various operations and outputs (via echo) a final title and a bulleted list. Example:
Of course, this is a simplified version. Now I need to use $result_title in metatags:
I hope I have described it better this time, please let me know if you need other information.
Comment #13
DamienMcKenna@1mundus: Are you saying that you have a whole bunch of extra logic in the views-view.tpl.php? If so, you might want to read some documentation about doing it in a better way, which would then also put you in the correct location to adjust the meta tags.
Comment #14
deepakrmklm CreditAttribution: deepakrmklm at Zyxware Technologies commented