I found many issues for "addind metatags for page display of views". But didn't found any for: displaying metatags as fields in Views.

I have administration view (table):
VBO-content | Nid | Title | Author | Published | Created | edit link

How to add there some Metatag as column?
I need display it and filter by it to easily find non-tagged nodes.

Or am i blind? Spent about 2 hours of trying.


Edit: A sandbox implementation can be found here: metatag_views_field.
Files: 

Comments

karlos007’s picture

Ok, now i found it as mostly impossible.
Hm, storing metatgs as serialized array is maybe good for performance, but a hell for working with that programatically and SQL...

I will let it open if someone has some proposal.

karlos007’s picture

StatusFileSize
new1.12 KB

First workaround attached.
Only thing it does is showing full serialized data as string, filter as normal views string.
Works only with nodes at this time.

I expect i will create more views handlers in future with unserialize data and also more entity_types support (at least taxonomy_term).

Dave Reid’s picture

Are you trying to do something like the following? I'm not exactly sure how we could support filtering on serialized data.

karlos007’s picture

Yes i am trying something like this. At the moment great result for me is: have metatags / dont have metatags.

I used this PHP field:

<?php
if(strlen($row->data)) {
 
$arr = unserialize($row->data);
}
if(
is_array($arr)) {
  foreach(
$arr as $delta => $tag) {
    if(
$delta == 'robots') {
     print
'<strong>' . $delta . '</strong>:<br />';
    
//dpm($tag);
    
foreach($tag['value'] as $d => $t) {
       print
'- ' . $d . ': ' . $t . '<br />';
     }
    } else {
      print
'<strong>' . $delta . '</strong>: ' . $tag['value'] . '<br />';
    }
  }
}
?>

Not ideal but better than nothing.

We are finishing website and 2 people working on metatags - i dont have any other idea how to help them with searching content without tags.

DamienMcKenna’s picture

Title:Views support» Expose meta tag data as a Views field

Clarified the title.

xurizaemon’s picture

I ran into this just now and thought I'd share an easy workaround using computed_field. We wanted to filter by (not display) metatag keywords in a Views search, so that people can type "schools" and see a result for "Education", and staff don't have to double-handle keywords.

  • On the content type, add a computed_field field named 'field_keywords' with text storage
  • On the content type, set display of the field to off (I only want it to produce results in search, don't do this if you want to display metatag values)
  • On the view, add display fields for the fields you want to filter against (title, body, keywords for me), setting Exclude from display
  • Also on the view, add a combine fields filter for the fields you want to filter by
  • Function to store the field is defined in custom module - see below
<?php
/**
 * Compute value for field_keywords, so we can use the result as a
 * filter result in views.
 *
 * Metatag module stores as serialized data, which makes keyword
 * filtering hard. Ref d.o #1906578.
 */
function computed_field_field_keywords_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
  if (isset(
$entity->metatags['keywords']['value'])) {
   
// split comma,separated,words to plain words (maybe redundant?)
   
$entity_field[0]['value'] = str_replace(',', ' ', $entity->metatags['keywords']['value']);
  }
  else {
   
$entity_field[0]['value'] = NULL;
  }
}
?>

EDIT: Code updated to include else block, otherwise your computed result won't clear if you zap the stored metatags for a node.

Because I wanted this specific filter result, I didn't check until after doing this if Metatag implemented hook_node_search_result() to produce keyword match results. Looks like it doesn't (#1368298: Index metatags for core search at time of writing).

You could easily build on the above to obtain displayable Metatag results. Sure, you're duplicating data in the DB, but consider it a cached display or search query table and that shouldn't keep you awake.

I've only tested this for about five minutes, I'll let you know if it's a wreck :)

Note that if the keywords value in your Metatags fields is empty, nothing will be stored - I expect this solution will only display *custom* keywords per-node, not site-wide keywords.

Paradoxetion’s picture

For someone who will face same problem (show metatags as field, like title or nid)

1) Install & Enable Views PHP module
2) Go to your view and add Node Id as field
3) Add field Global -> PHP code
4) Add this as output code

For keywords:

<?php
 
$result
=node_load($row->nid);
$metkey = $result->metatags['und']['keywords']['value'];
print (
$metkey);
?>

For description

<?php
 
$result
=node_load($row->nid);
$metkey = $result->metatags['und']['description']['value'];
print (
$metkey);
?>

And that's it! Hope it will help you to spend 2-3 hours of your life better ;)

karlos007’s picture

there should not be:

<?php
['und']
?>

but

<?php
[LANGUAGE_NONE]
?>

and yes, you are totally killing performance by using node_load() on all rows.

Paradoxetion’s picture

karlos007 ,

yes, you're right.
Better idea?

mgerbault’s picture

Title:Expose meta tag data as a Views field» Expose meta tag data as a Views field [SOLVED]
StatusFileSize
new2.4 KB

Thanks a lot, I have developped a little module inspired by #2.

See attached file

Mat

DamienMcKenna’s picture

@mgerbault: That's an excellent start, thank you! Would you mind if we take what you wrote and expand upon it with the goal of adding it to the main Metatag module?

mgerbault’s picture

@DamienMcKenna , yes if you want.
I need my module for a site where I use Metatag module ans sub-modules.

I suppose you can add meta keyword ...

Thanks

cameronbprince’s picture

I don't know that I would call this one solved. In my testing, the module from number 10 did not work for views displaying taxonomy terms. It seems to be specifically for nodes.

ciss’s picture

Title:Expose meta tag data as a Views field [SOLVED]» Expose meta tag data as a Views field (sandbox)
Issue summary:View changes

@cameronbprince: The initial request was to provide integration for nodes - in that regard the solution in #10 will probably work.

But I think we can all agree that we should aim for a full integration. I started a new sandbox and claimed the name "metatag_views_field" since mgerbault didn't create a sandbox of his own.
The basic idea is outlined in the description. I've also created issues for each 1.0 step to help in planning. I haven't looked much under metatag's hood yet, so any input on the provided issues is appreciated.

Version 1.0 (and probably also 1.1) are requirements for a current project, so expect results in the next 2-3 weeks.

DamienMcKenna’s picture

Component:Code» Views integration
pixelz.gmd’s picture

#7 helped me a lot! thanks for that Paradoxetion!

To fix all the error codes gotten, I implemented an isset() check:

<?php
    $result
=node_load($row->nid);
   
$keywordsSet = isset($result->metatags['und']['keywords']['value']);
    if (
$keywordsSet) {
       
$metkey = $result->metatags['und']['keywords']['value'];
        print (
$metkey);
    }
?>

Hope this helps others! Still would be cool to use the module described here though!

dillix’s picture

Will this submodule be a part of metatags?

DamienMcKenna’s picture

Status:Active» Needs work

@dillix: Potentially. First off, it needs to be rolled as a patch.

ciss’s picture

Issue summary:View changes

@dillix, @DamienMcKenna: I'd like to keep metatag_views_field a separate project until it gets a green light for the 1.0 milestone. Otherwise we'll have to cope with large patches and lots of interdiffs.

The main problem left (for which I haven't created an issue yet) is multiple languages: hook_views_data joins don't allow to define joins on multiple fields, which is why I initially forced LANGUAGE_NEUTRAL. The best approach would probably be to use the field language selected for the view (views_handler_field_field is not the easiest handler around though - might be difficult to identify and port the relevant parts).

As for filtering and sorting: I'd strongly recommend to postpone those features, as I don't see a way yet to implement them without maintaining an additional index table for all metatag values.

drupalerocant’s picture

Hello,

I need this module you are doing for a project in which I need to create taxonomy terms with the metatags already filled in the articles.
I think this is the best way.
First with views I get the metatags and I can export them with views_data_export in a csv file and then import it with feeds.
The problem is that when using your module in the sandbox I get a WSOD (blank page).
Do you know Why is is happening?
Are you planning to create a usable release? It would be much appreciated.
Thanks in advance!

ciss’s picture

drupalerocant’s picture

Thank you very much, ciss, after following the issue you pointied me, I used the module in this issue and was able to show the metatag description in views. After that I changed in the module description for keywords and was able to show the keywords in views.
Problem solved!