Having added an image field to a content type, it's being output successfully in the OG and Twitter image tags when using [node:field_image].

What I'd like to be able to do is set a default image on the image field and have this output when an image hasn't been added specifically.

Currently, the image metatags are only output when the field is populated on the node in question, rather than displaying the default image as expected.

I'm quite happy to use some kind of workaround, but thought that this would behave as expected out of the box.

Comments

kiwimind created an issue. See original summary.

kiwimind’s picture

Issue summary: View changes
kiwimind’s picture

By way of a workaround, we're currently using hook_tokens_alter() in a custom module.

Happy to share thoughts if anyone else gets stuck on this.

DamienMcKenna’s picture

Component: Open Graph » Code
Issue tags: +Needs tests

Need to get all of the image handling tests ported over from D7 to make sure it works correctly.

DamienMcKenna’s picture

This will be at least partly covered by #2628472: Write tests for image handling.

benstjohn’s picture

@kiwimind Would you be able to post a snippet on how you used hook_tokens_alter() to provide a default image when one does not exist?

kiwimind’s picture

@benstjohn perhaps I should have just posted them at the time. :)

I'm sorry but I've moved jobs since then so don't have access to the code.

BigEd’s picture

Guys, I Just had this crop up for an issue I am working on then I worked it out as its not that obvious I thought i would post back, you just need to do this..

Open Graph section > Image URL

[node:field_image:entity:url],[site:url]/sites/default/files/pathtologo.jpg

This will output both values but only show either in the open Graph, I hope this saves you some time faffing about.

idebr’s picture

The default image is only available when the field is rendered using a formatter. There is Drupal Core issue to apply the default value at the data level that should fix this use case as well: #3005528: Fields' "default value" functionality only works in formatters and widgets, not at the data level and hence not in HTTP APIs