Using the token [node:field_image] for a single image field's picture works, but if the field is set to accept multiple files, that token (or: [node:field-image:1]) doesn't get the first picture file.

Do I use the wrong token?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

traveller’s picture

As an example, this article lists all images in the metatag twitter:image:src. Twitter's Card Validator doesn't get the first picture.
My preferred solution would be to list just the first (or any specified) file.

DamienMcKenna’s picture

Component: Twitter Cards » Integration with other module
Category: Bug report » Support request
Issue tags: -twitter cards, -image, -metatag
attiks’s picture

Version: 7.x-1.0-rc2 » 7.x-1.x-dev
Status: Active » Needs review
FileSize
3.71 KB

I added a new theme function theme_metatag_twitter_cards_single which accepts an optional parameter offset, so the code can define which value to use if multiple values are provided, useful for twitter cards.

DamienMcKenna’s picture

Status: Needs review » Needs work

This needs to be handled at the Token level.

attiks’s picture

#4 True, but that means that users will need to know which fields are single value and which are multi value ones, since there's no validation this will be hard for most people. I had to look at the twitter card spec to see which fields are single value.

It would be nice if token has support to select a single value for multi values fields, but I don't have the time right now to write the necessary patches.

attiks’s picture

Status: Needs work » Needs review

I guess this is the related issue in the token issue queue: #1037524: Support array-format tokens in [current-page:query:x] tokens

DamienMcKenna’s picture

The forms fields indicate which of them accept multiple values, though we could improve the UX if needed (maybe add a suffix to the title?).

I need to dig into this.

attiks’s picture

I searched a bit, and according to #1037524-6: Support array-format tokens in [current-page:query:x] tokens the following should work [node:field-image:0:file:url], since my field is called field_images, I tried [node:field-images:0:file:url] but I got a validation error.

Updated: See following comment

attiks’s picture

#8 Strike that, just enable entity token and [node:field-images:0:file:url] works.

You need to install Entity and enable both entity and entity_token

attiks’s picture

I guess this is WAD, but I guess it might be a good idea to mention the existence of Entity tokens and the benefits, I still think adding an option in metatag to automatically select one value if the field is single value is a good improvement.

attiks’s picture

#9 It appears it only works for the first element, using [node:field-images:1:file:url] does not work for me

DamienMcKenna’s picture

BTW should the first element be element 0 instead of 1?

DamienMcKenna’s picture

Duh, sorry, I misread.

DamienMcKenna’s picture

Status: Needs review » Needs work
DamienMcKenna’s picture

Title: Twitter Cards image field's token » How to only use one item of a multi-item field?

Updating the title to better indicate the issue's focus.

MediaFormat’s picture

This is what works for field collections, if anyone needs a work around.
[node:field-collection-image:0:field_image]

AdamPS’s picture

Component: Integration with other module » Documentation

The comments here helped me figure out my problem. But I'm now a bit confused as to why the issue is still set to "needs work" - it works fine for me.

All you need to do is:

  • Enable entity_token
  • Use [node:field-image:0:file:url]

More detailed notes:

  • As I would expect, it won't give a value if no image is present
  • You could use [node:field-image:1:file:url] to get the second image. As I would expect, this won't give a value if there is only a single image.
  • NB node:field_image (with an underscore rather than a dash) exists too, but it doesn't solve this particular scenario of getting the first image. It returns a comma-separated list of URLs; it doesn't accept :0 nor :file.
  • NB many comments above have field-images (with an s) which is potentially not the default for most people, so be careful if you are block-copying.

So potentially all that is needed is some documentation updates. I've set the component accordingly. There are several image-based metatags, including og:image and image_src. We could clarify the description of each field. Or perhaps some text could be added to the top-level text $form['metatags']['intro_text'].

2pha’s picture

Thanks, #9 worked for 'image_src' metatag for me

jelo’s picture

I reported similar issues in this thread https://www.drupal.org/node/2623330 all related to multiple values in images, e.g. in which context they are allowed (OG) and not allowed (Twitter?). I think the discussion should be if this module should try to automatically validate where needed, despite relying on tokens to do the bulk of the work, i.e. if an image is used in Twitter Cards, can the module check if multiple values exist and should it automatically limit it to 1 in this case?

DamienMcKenna’s picture

Status: Needs work » Needs review

Would something like this work? Change metatag.inc from this:

      if (!empty($this->info['multiple'])) {
        $values = explode(',', $value);
      }
      else {
        $values = array($value);
      }

to this:

      if (!empty($this->info['multiple'])) {
        $values = explode(',', $value);
      }
      else {
        $values = array($value);
      }

That'd change image meta tags so that if more than one item was found on a non-multiple meta tag it would only output the first one.

jelo’s picture

For Twitter it looks like all cards that were able to handle multiple images have been deprecated as of July 2015:
https://dev.twitter.com/cards/types
In particular, gallery card was removed which could show a number of images. From the descriptions of the remaining cards it sounds as if all only take in one image. In contrast to Facebook which allows a user to pick from multiple images in a preview, I have not seen this with Twitter, i.e. I would think limiting the options to one may be desired at this stage.

We could either focus on providing this option through a token (which would require the person configuring the card to know that multiple images should be limited to 1, but may leave flexibility to decide which image to use) - functionality as provided by entity_token - or make this decision for the person configuring the card and limit to first image by default for Twitter?

Alternatively, we could add instructional text to the image field for Twitter to highlight the need to the user that they should think about multiple image values and that Twitter only expects 1 image, i.e. they should pick a token that only provides a single image.

One related problem with entity_token is though that it does not allow specification of the image style per image. This is important because Twitter has some format requirements for the image and we could tackle this with a specific image style to deliver a correctly formatted image if the token would allow this selection.

maxplus’s picture

Hi,

I used [node:field-images:0:file:url] like #9 in combination of entity_token.

Maybe this could help for using an image style: https://www.drupal.org/project/imagecache_token

DamienMcKenna’s picture

FileSize
794 bytes

So of course I messed up the code snippet in #20.

I think this patch is what I was thinking of - always explode the value (to turn it into an array) and then only take the first value if the tag doesn't allow multiple items.

DamienMcKenna’s picture

Component: Documentation » Code
DamienMcKenna’s picture

Updated patch with some additional comments.

DamienMcKenna’s picture

I'd really like to add some tests but I haven't worked on tests for images yet..

DamienMcKenna’s picture

Committed. Please follow up in new issues if there are any further issues with this. Thanks.

DamienMcKenna’s picture

Status: Needs review » Fixed

  • DamienMcKenna committed 3ca6cc8 on 7.x-1.x
    Issue #2338211 by DamienMcKenna: Only output the first item of a token...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.