Ensure Metatag works OOTB using Views to display Drupal core's entities, i.e. taxonomy term pages, nodes and users.

Any further integration will be handled by other issues and sandboxes, e.g. Dave's sandbox to add Metatag fields to the Views UI: http://drupal.org/sandbox/davereid/1281614


Original issue description:

With #681468: Create pluggable display extenders it should be possible to integrate and extend meta tag support to Views.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bryancasler’s picture

subscribe

quantos’s picture

+1

leendertvb’s picture

subscribe

BenK’s picture

Subscribing

Ankabout’s picture

Subscribing.

Dave Reid’s picture

.

Dave Reid’s picture

Assigned: Unassigned » Dave Reid
dropbydrop’s picture

+1

Dave Reid’s picture

I started a sandbox module for this at http://drupal.org/sandbox/davereid/1281614. Use at own risk for now. Requires latest Views 7.x-3.x-dev.

Ankabout’s picture

I'll try it out and report back Dave.

Dave Reid’s picture

Removing blocker tag.

ceefour’s picture

+1 for this!

BTW, what is the workaround until this feature is ready .... ??

Coupon Code Swap’s picture

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

A question on Views integration. Will taxonomy meta tags be supported with taxonomy term views in the same way that they are for the regular Drupal taxonomy term pages? I have added meta tags for each term and would like to be able to utilize these for the title etc. in the overridden taxonomy term view.

lsolesen’s picture

I tried the sandbox project - however, as far as I can tell the metatags are not added to the html output. However, the ui seems to work smoothly.

maximpodorov’s picture

This code works for views serving taxonomy term pages:

function mymodule_page_alter(&$page) {
  $view = views_get_page_view();
  if (empty($view)) return;

  if ($view->get_path() !== 'taxonomy/term/%') return;
  if (!($view->display_handler->has_path())) return;
  if (!($view->display_handler->uses_breadcrumb())) return;

  $context = array();
  $context['entity_type'] = 'taxonomy_term';
  $context['entity'] = taxonomy_term_load($view->args[0]);
  $context['view_mode'] = 'full';
  metatag_field_attach_view_alter($page['content'], $context);
}
facine’s picture

For multiple taxonomy views:

function mymodule_page_alter(&$page) {
  $view = views_get_page_view();

  if (empty($view)) return;
  if (strpos($view->get_path(), 'taxonomy/term/') !== 0) return;
  if (!($view->display_handler->has_path())) return;
  if (!($view->display_handler->uses_breadcrumb())) return;

  $context = array();
  $context['entity_type'] = 'taxonomy_term';
  $context['entity'] = taxonomy_term_load(arg(2));
  $context['view_mode'] = 'full';
  metatag_field_attach_view_alter($page['content'], $context);
}
maximpodorov’s picture

Is it possible to include taxonomy term view support into official Meta tags project?

Jorge Navarro’s picture

+1

quasi’s picture

Second the taxonomy view support. Thank you!

Ankabout’s picture

Dave, I haven't tried this yet, since someone said it doesn't add the html anyways, but maybe you can answer.

Can I use replacement patterns from views in the meta tags? It would be good if you have some sort of contextual filter (based on a field) and if you're able to use the information from the view in the tags.

I'm not a coder, so I'm not sure how hard this would be, but I can't be the only one hoping for this functionality. The problem I see however is that views seems to always be passed after the head is parsed and printed. Or am I wrong in that?

jeff.maes’s picture

Status: Postponed » Needs review
FileSize
895 bytes

Nice one!
I've created a patch of #16 - All credits should go to maximpodorov and facine.

Jorge Navarro’s picture

the patch is fixing the problem only for taxonomy, for page views there is no change.

selinav’s picture

+1,
I've a view page of my products, how to assign meta ?

keesje’s picture

Dave Reid’s picture

I would really prefer that people continue to check out http://drupal.org/sandbox/davereid/1281614 and head in that direction rather than the patch in #21.

Ankabout’s picture

Can you elaborate why the sandbox solution is better or why you prefer it? A short explanation would be good for people finding this thread for the first time, or for those like me that just don't get it because of lack of caffeine...

artis’s picture

Dave, while you are elaborating on Ankabout's question could you also touch on the difference between your sandbox module and ribakker's sandbox module? It would be nice to get one of the two sandbox modules committed to the Metatag module, but with 3 different directions that doesn't seem likely with out some sort of comparison.

Thanks.

voodootea’s picture

http://drupal.org/node/1151936#comment-6037226

the sandbox project is not available, or is just a folder without the module code?

I've fetched it using

git clone http://git.drupal.org/sandbox/davereid/1281614.git views_metatags

using command line, however it only contains a bunch of empty folder,, the readm file just says 'Master branch is empty.'

Any ideas??

many thanks D

rooby’s picture

Try git clone --branch 7.x-1.x http://git.drupal.org/sandbox/davereid/1281614.git views_metatags

selinav’s picture

I try to test views_metatags of davereid and I've the error message in the metatags settings

The Description is using the following invalid tokens: [view:description].
The Titre is using the following invalid tokens: [view:title].
The URL canonique is using the following invalid tokens: [view:url].

Even if, I override token by text, meta are not displayed in the view page.

ordermind’s picture

I have just uploaded a sandbox module at http://drupal.org/sandbox/ordermind/1623122 which includes a version of views_metatags that is mostly working. It also provides some useful improvements on the original metatag module.

nocean’s picture

#15/16 worked great for me. @Dave, I tried to test out your sandbox project, but had trouble -- eventually running up against a 500 error on Drupalcode.org.

norio’s picture

I tested ribakker's sandbox and davereid's. Dave's uses invalid tokens, so doesn't work, and ribakker's LOOKS like it will work but no actual meta tags are added to the view.

If I had to choose, I'd definitely say ribakker's is the way to go as Dave's uses the view description which, if I'm not mistaken, is there for the admin's use, not for public view. This adds work as you have to go and edit the description of all the default views in your site to remove the developer's description of the view and add your own, SEO-friendly version.

gebiss’s picture

The patch #21 works but it generate error on multilingual taxonomy:

Notice: Undefined index: language in metatag_field_attach_view_alter() (line 458 of /home/cms/public_html/CS/sites/all/modules/metatag/metatag.module).

Jacek

danielcristian89’s picture

I just add a mini if to stop generate that problem. If somebody has a better ideea for it pls let me know
$context['language'] = isset($context['language']) ? $context['language'] : '';

nocean’s picture

Thanks Daniel, that cleared up the PHP notice for me too.

gebiss’s picture

danielcristian89, where did you add this code?

danielcristian89’s picture


function mymodule_page_alter(&$page) {
  $view = views_get_page_view();

  if (empty($view)) return;
  if (strpos($view->get_path(), 'taxonomy/term/') !== 0) return;
  if (!($view->display_handler->has_path())) return;
  if (!($view->display_handler->uses_breadcrumb())) return;
	global $language;

 
  $context = array();
  $context['language'] = isset($context['language']) ? $context['language'] : ''; 
  $context['entity_type'] = 'taxonomy_term';
  $context['entity'] = taxonomy_term_load(arg(2));
  $context['view_mode'] = 'full';
  metatag_field_attach_view_alter($page['content'], $context);

}
trunghaiy’s picture

I have same errors with #30 when use views_metatags!

dropbydrop’s picture

same problem here

csc4’s picture

Just wanted to say thank you to danielcristian89 the code in #38 was a massive help for a project I 'm working on

gebiss’s picture

Thanks danielcristian89 for the solution #38. It helped

trunghaiy’s picture

Thanks #38 but #38 will not work if you use Taxonomy Term Views with Allow multiple values , it will make error
EntityMalformedException: Missing bundle property on entity of type taxonomy_term. in entity_extract_ids() (line 7539 of /includes/common.inc).
You should check if term id is number to avoid this error :)

maximpodorov’s picture

How is it possible to assign metatags for multiple term page? Maybe first term's metatags is the best solution?

trunghaiy’s picture

Hi maxim,
I don't think it is best solution, it will duplicate meta tag with other term.
In my case, I avoid use multiple term and changed from multiple term to hierarchy term and use DefaultContextual Filter of Taxonomy Term Views.

Summit’s picture

@trunghai, can you explain this in code please? Thanks a lot in advance! Greetings, Martijn

trunghaiy’s picture

@Summit, I just config Views not code :)
My taxonomy term is:
Toy
--Barbie (tid = 1)
--Auldey (tid = 2)
--Vikings (tid = 3)
.....
--Fischertip (tid = 4)
--Fischertechnik (tid = 5)
I used taxonomy term to group terms 1, 2, 3 is Funny Land toy; 4,5 is smart toy and used Views taxonomy term to create mysite/taxonomy/term/1,2,3 and mysite/taxonomy/term/4,5.
But my site have bug #43.
Now, I create new terms and revert view Taxonomy term to default and change Depth to 1.
Toy
-Funny Land
--Barbie (tid = 1)
--Auldey (tid = 2)
--Vikings (tid = 3)
.....
-Smart toy
--Fischertip (tid = 4)
--Fischertechnik (tid = 5)

maximpodorov’s picture

@trunghai, what's your suggestion of combining metatags for the pages like 1+2+3 or 1,2,3?

osysltd’s picture

#21 Works fine except exposed filters.

EntityMalformedException: Отсутствует связующее свойство у сущности типа taxonomy_term. в функции entity_extract_ids() (строка 7539 в файле public_html/includes/common.inc).

osysltd’s picture

More reliable variant of #21 Does not throw exceptions as described in #43 when Exposed Filters are applied.

/**
 * Implements hook_page_alter().
 */
function metatag_page_alter(&$page) {
  $view = views_get_page_view();

  if (empty($view)) return;
  if (strpos($view->get_path(), 'taxonomy/term/') !== 0) return;
  if (!($view->display_handler->has_path())) return;
  if (!($view->display_handler->uses_breadcrumb())) return;
  $taxonomy_id = taxonomy_term_load(arg(2));
  if(!$taxonomy_id) return;
  $context = array();
  $context['entity_type'] = 'taxonomy_term';
  $context['entity'] = $taxonomy_id;
  $context['view_mode'] = 'full';
  metatag_field_attach_view_alter($page['content'], $context);
}
adelka’s picture

subscribe

smitty’s picture

This works fine for me. Except the php-error: "Notice: Undefined index: language in metatag_field_attach_view_alter() (Zeile 440 von www.myserver.com\sites\all\modules\metatag\metatag.module)."

This can be fixed by adding

$context['language'] = 'und';

previous to metatag_field_attach_view_alter.

agoradesign’s picture

Call me pedantic, but plz do not use 'und', use LANGUAGE_NONE constant instead:

$context['language'] = LANGUAGE_NONE;

ref: http://api.drupal.org/api/drupal/includes!bootstrap.inc/constant/LANGUAGE_NONE/7

smitty’s picture

@agoradesign: good catch!

@all: Found a small bug: If using the token current-page:page-number for displaying a page-number in the title, all pages of the view get the same number.

Seems to be a caching problem. If switching off caching in metatag_field_attach_view_alter the page-numers are correct.

How can we tell metatag_page_alter not to cache the taxonomy term views pages metadata?

jackhutton’s picture

implementing solution outlined in #38 .. works for me thank you danielcristian89
subscribing.

Jorge Navarro’s picture

Not working after alpha8

NikGleb’s picture

This much-needed patch simply must be included in the core of the module.
Dear Dave Reid, we look forward to you!

facine’s picture

function MY_MODULE_NAME_metatag_metatags_view_alter(&$output) {
  $view = views_get_page_view();

  if (empty($view)) return;
  if (strpos($view->get_path(), 'taxonomy/term/') !== 0) return;
  if (!($view->display_handler->has_path())) return;
  if (!($view->display_handler->uses_breadcrumb())) return;

  // Build options for meta tag rendering.
  $options = array(
    'entity_type' => 'taxonomy_term',
    'view_mode' => 'full',
  );

  // Ensure we actually pass a language object rather than language code.
  $languages = language_list();
  if (isset($langcode) && isset($languages[$langcode])) {
    $options['language'] = $languages[$langcode];
  }

  // Reload the entity object from cache as it may have been altered.
  $token_type = token_get_entity_mapping('entity', $options['entity_type']);
  $entities = entity_load($options['entity_type'], array(arg(2)));
  $options['token data'][$token_type] = $entities[arg(2)];
  $options['entity'] = $entities[arg(2)];

  $metatags = isset($options['entity']->metatags) ? $options['entity']->metatags : array();
  $metatags += metatag_config_load_with_defaults($options['entity_type']);

  // Convert language codes to a language object.
  if (isset($options['language']) && is_string($options['language'])) {
    $languages = language_list();
    $options['language'] = isset($languages[$options['language']]) ? $languages[$options['language']] : NULL;
  }

  foreach ($metatags as $metatag => $data) {
    if ($metatag_instance = metatag_get_instance($metatag, $data)) {
      $output[$metatag] = $metatag_instance->getElement($options);
    }
  }
}
hdennen’s picture

#50 is working form me (almost completely) in a custom module, #58 not at all.

The only problem with #50 is that I'm still not pulling og:image from a taxonomy view. Not sure what's going on; if I use [field_image:file] I can pull the file name, just not the path to it. weird.

DamienMcKenna’s picture

Assigned: Dave Reid » DamienMcKenna

I'll review this prior to the next beta.

DamienMcKenna’s picture

Title: Integrate with Views » Ensure Metatag works OOTB using Views to display Drupal core's entities
Assigned: DamienMcKenna » Unassigned
Status: Needs review » Active

Lets get this back to ensuring that the core entity pages still work correctly when using Views as the display engine, we'll leave Dave's sandbox as the starting codebase for adding Metatag functionality directly inside the Views UI.

Taxonomy term pages should work once the core patch from #1067120: Missing hook_taxonomy_term_view() or hook_entity_view() when viewing a term is committed, so I'm not in favor of adding more hacks until deemed absolutely necessary once other normal hooks are not working.

Has anyone tried using Views for displaying nodes, did Metatags work with that scenario?

DamienMcKenna’s picture

(yes, I hijacked the issue.)

DamienMcKenna’s picture

This is needed for the 1.0 stable release.

DamienMcKenna’s picture

DamienMcKenna’s picture

Test scenario #1:

  • Create a node, override the node's description, keywords and page title fields.
  • Create new view that uses the path node/%, thus taking over the node display page (the rest of the view's configuration is not relevant).
  • Viewing the node results in:
    • The view triggers correctly and loads the page as configured.
    • The description and keywords fields are assigned correctly.
    • Without the patch from #1760494: the page title is assigned the title configured in the view.
    • With the patch from #176094: if the title in the view is blank, the page title is still blank.

So, the page title still does not work correctly, even if Views is set to not output the title.

vood002’s picture

Am I missing something here? Trying to use code from #50, getting error

Fatal error: Call to undefined function metatag_field_attach_view_alter() in /var/www/drupal7/sites/all/modules/custom/siena/siena.module on line 425

Assuming this is calling an implementation of hook_view_alter()? If that's the case what is the metatag_field_attach module? Is this one of the sandbox projects mentioned above?

DamienMcKenna’s picture

The code snippet above won't work as it depends upon code that has since been removed from Metatag.

bartclarkson’s picture

Honestly, I just needed the metatags to act more like fields, both for internal site searching and a Views feed that allows another site to selectively synchronize nodes, and to that end, I employed the Computed Field module. For the Computed Code (PHP), I added this for a computed field I called meta_description.

$metatags = isset($entity->metatags) ? $entity->metatags : array();
list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
// Reload the entity object from cache as it may have been altered by Panels.
$token_type = token_get_entity_mapping('entity', $entity_type);
$entities = entity_load($entity_type, array($entity_id));
$options['token data'][$token_type] = $entities[$entity_id];
$options['entity'] = $entities[$entity_id];
foreach ($metatags as $metatag => $data) {
  if ($metatag_instance = metatag_get_instance($metatag, $data)) {
    $output[$metatag] = $metatag_instance->getValue($options);
  }
}
$entity_field[0]['value'] = $output['description'];

I did another for $output['keywords'];

I figure the beauty of it is that when we get a production-grade solve, I can just drop off the computed fields without much heartache.

DamienMcKenna’s picture

Status: Active » Postponed
colan’s picture

Status: Postponed » Active

Nope. None of my global tags are showing up on Views pages with that patch.

DamienMcKenna’s picture

Priority: Normal » Critical

This is a must-have item for 1.0.

maximpodorov’s picture

I created new project http://drupal.org/project/metatag_term_view with the following code for view-based taxonomy term pages:

function metatag_term_view_page_alter(&$page) {
  $view = views_get_page_view();
  if ((!empty($view))
    && (strpos($view->get_path(), 'taxonomy/term/') === 0)
    && ($view->display_handler->has_path())
    && ($view->display_handler->uses_breadcrumb())) {

    $term = taxonomy_term_load(intval(arg(2)));
    if ($term !== FALSE) {
      metatag_entity_view($term, 'taxonomy_term', 'full', NULL);
      $page['content']['metatags'] = metatag_page_get_metatags();
    }
  }
}

It works for me with new dev version of Meta tags module.

DamienMcKenna’s picture

I'm not sure when I'll get to it, so could someone please test the current -dev version of Drupal core along with the current -dev version of Metatag and let me know if the combination work for term pages? It should.

DamienMcKenna’s picture

I can confirm that with the current -dev codebase and the latest D7 -dev release, that Views taxonomy term pages do not work. Bummer.

maximpodorov’s picture

@DamienMcKenna, could you try my module (http://drupal.org/project/metatag_term_view)?

DamienMcKenna’s picture

I've built a patch for Views that triggers hook_entity_view (thus allowing Metatag to work) when the current View's path matches an entity's path: #1809862: Trigger hook_entity_view on entity view pages; I'm awaiting feedback to let me know if this is the correct approach to take, or if I'm going in the wrong direction and should implement something in Metatag.

DamienMcKenna’s picture

@maximpodorov: The goal is for this to work out of the box, lets wait for some feedback on the Views issue and we'll see what needs to be done.

DamienMcKenna’s picture

Status: Active » Needs review
FileSize
1.3 KB

This is based on maximpodorov's code and works for all meta tags except the page title, which is being controlled by Views.

1mundus’s picture

Considering that D 7.16 is a security maintenance release, will this patch wait until 7.17 to be commited or we can use it in the next dev version?

Thank you for your reply and efforts to implement it!

DamienMcKenna’s picture

@1mundus: Which patch? The only core patch mentioned here is in the current drupal-7.x-dev codebase and will be in the 7.17 release soon. The other patches mentioned here are for Metatag or Views, none of which have been committed yet.

1mundus’s picture

@DamienMckenna

Your patch from your previous post :) That patch should fix the issue with views pages, right?

After all that I have read about this issue, we are waiting until Drupal core bug gets fixed until you can commit this one, or?

DamienMcKenna’s picture

@1mundus: The patch in #78 does not depend on any changes to core or Views.

1mundus’s picture

Excellent, I will test it in the following days.

Lostmonkey’s picture

Should the patch in #78 also work for 7.x-1.0-beta1? I have tried applying it with no luck.

maximpodorov’s picture

I confirm patch #78 can be applied to 7.x-1.0-beta1 and the resulting code produces meta tags on my view pages. Hip-hip-hurrah!

Lostmonkey’s picture

Made a mistake. Yes the patch work.

DamienMcKenna’s picture

Status: Needs review » Needs work

In hindsight, I think that if Views won't be updated to fix this problem internally, that it should be handled via custom Views hooks rather than having any effect on other page requests.

DamienMcKenna’s picture

Per #1819670: Global Frontpage Page Title not working when using a view to display content on frontpage. (closed as a duplicate), the page title not working is a problem :-\

ultimike’s picture

The patch in #78 worked for me with beta1 as well.

Thanks,
-mike

Drupal Centric’s picture

The patch in #78 worked for me to thanks.

DamienMcKenna’s picture

Status: Needs work » Needs review
FileSize
1001 bytes

How would this work instead?

DamienMcKenna’s picture

FileSize
988 bytes

This is a little better - instead of triggering all entity_view hooks it just runs metatag_entity_view().

DamienMcKenna’s picture

FileSize
1.68 KB

Updated to only run the custom logic on Views pages rather than on all views.

DamienMcKenna’s picture

FYI the code in #93 is generic that will work with all entity types, not just taxonomy terms.

DamienMcKenna’s picture

Status: Needs review » Fixed

Committed! Thanks for everyone's help!

maximpodorov’s picture

Excuse me for the stupid question. What is the best way to attach metatags to a entity-like page generated by a view if the path of this page is not the standard path for that entity?

facine’s picture

+1 -> What is the best way to attach metatags to a entity-like page generated by a view if the path of this page is not the standard path for that entity?

DamienMcKenna’s picture

Status: Fixed » Closed (fixed)

This has been released in 1.0-beta2: http://drupal.org/node/1826908

osysltd’s picture

Hey! Beta-2 Is still pushing errors like EntityMalformedException: Missing bundle property on entity of type taxonomy_term. in entity_extract_ids() (line 7562 of /home/web/www/includes/common.inc).with exposed filter rather #50.

DamienMcKenna’s picture

@osysltd: Please open a new issue for this and describe the view's configuration so that I can best understand what might be going on. Thanks.

DamienMcKenna’s picture

@facine: please follow #1837660: Integration with Metatag.

shoufeng.xi’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta6
Status: Closed (fixed) » Active

not work with TVI

DamienMcKenna’s picture

Status: Active » Closed (fixed)

@shoufeng.xi: Please open a new issue for that.

DamienMcKenna’s picture

Issue summary: View changes

Yes, I'm hijacking the issue.