One requirement for our new Drupal 8 site was to implement the necessary markup to provide enough semantic data for services like Twitter, LinkedIn and Facebook to grab the appropriate node data (title, body and image) when sharing posts.
The Twitter developers documentation states that "with Twitter Cards, you can attach rich photos, videos and media experience to Tweets that drive traffic to your website. Simply add a few lines of HTML to your webpage, and users who Tweet links to your content will have a "Card" added to the Tweet that's visible to all of their followers." Facebook have a similar feature and corresponding documentation, as do LinkedIn.
Fortunately with the Metatag module enabling this functionality in Drupal 8 was a relatively painless task! All that was required was to install and enable the Metatag and Metatag: Twitter Cards modules (we're using 1.0-beta10 at the time of writing) and configure some basic, content-global settings.
Essentially, we ensure the card type is set to "Summary card" and that the description uses the [node:summary] token. We also want to provide an image for the summary card. We could provide the "Image URL" in the Twitter card setup which defines the twitter:image meta tag, however Facebook and LinkedIn use Open Graph for cards and we’d also have to provide the og:image metatag too.
This would cause two meta tags to be present in the header containing the same image URL, wasting bandwidth. Luckily, Twitter will fall back to the Open Graph tag if the twitter:image tag is not present so we only have to configure og:image tag.
We also configured several other Open Graph meta tags to support Facebook and LinkedIn.
We have only configured a subset of the tags available, but this gave us the basic card functionality we were after.
There are tools available to aid in development - allowing you to validate the meta tags and how they will render on social networks.. Your site has to be publicly accessible for these to work.
There are plenty of other guides available if you want more details about what you can do with the Metatag module, try these links:
- Adding Meta Tags to Drupal Content - written for Drupal 7, but still a good overview of the main features and benefits of using the Metatag module.
- "Meta tags drupal 8" video - an overview of installing the module.
- "Drupal 7 Meta Tags module - Daily Dose of Drupal episode 28" video - again written for Drupal 7, but a good walkthrough on how to configure Metatag module for the front page and nodes.