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

1mundus created an issue. See original summary.

deepakrmklm’s picture

This will work.

/**
 * Implements hook_preprocess_page().
 */
function mymodule_preprocess_page(&$vars) {
  $path = request_path();
  // Set meta data for required pages.
  if ('x' == $path) {
    $description = 'Some text';
    $og_description = 'Some other text';
  }
  elseif ('y' == $path) {
    $description = 'Some text 2';
    $og_description = 'Some other text 2';
  }

  // Prepare meta data.
  if (isset($description) && isset($og_description)) {
    $html_head = array(
      'description' => array(
        '#tag' => 'meta',
        '#attributes' => array(
          'name' => 'description',
          'content' => $description,
          )
        ),
      'og_description' => array(
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'og:description',
        'content' => $og_description,
        )
      ),
    );
    // Push data to HTML head.
    foreach ($html_head as $key => $value) {
      drupal_add_html_head($value, $key);
    }
  }
}
1mundus’s picture

Thank you for the answer, but I can't make it work. How should that code be called in views-view.tpl.php?

DamienMcKenna’s picture

Version: 7.x-1.16 » 7.x-1.x-dev

@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.

1mundus’s picture

@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?

deepakrmklm’s picture

Dear 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

1mundus’s picture

Thank 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.

DamienMcKenna’s picture

@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.

deepakrmklm’s picture

@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.

DamienMcKenna’s picture

@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.

deepakrmklm’s picture

Yes @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.

1mundus’s picture

@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:

$result_title = 'Final Title is Text One';
echo '<h1>' . $result_title . '</h1>';

Of course, this is a simplified version. Now I need to use $result_title in metatags:

<title>Final Title is Text One</title>
<meta name="description" content="Hello, you're reading 'Final Title is Text One'" />

I hope I have described it better this time, please let me know if you need other information.

DamienMcKenna’s picture

@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.

deepakrmklm’s picture

Status: Active » Closed (works as designed)