Score:1

How can I set "advanced" metatags programmatically?

cn flag

The Metatag documentation gives an example of setting meta tags like this:

$node->set('field_meta_tags', serialize([
  'title' => 'Some title',
  'description' => 'Some description.',
  'keywords' => 'Some,Keywords',
]));

However, I want to set the "advanced" fields, such as Canonical URL. (Use case: I am trying to import several hundred pages with the Feeds module, some of which are very similar, so I want to automatically set the canonical URL for the similar pages.)

I tried to get the name of the key to serialize by setting the canonical URL for a node in the UI, saving the node, and then dumping the result of field_metatags, but this was the result:

www.example.com a:4:{s:5:"@type";s:6:"Person";s:3:"@id";s:39:"https://www.example.com/bio";s:4:"name";s:12:"Person's name";s:3:"url";s:39:"https://www.example.com/my-bio";}

Here, the first www.example.com is the value I set for canonical URL, but this does not give me a hint of what to use to set the canonical URL.

How can I set the values of the "advanced" metatags programmatically?

Similar questions

This question asks about setting the canonical URL but it is for Drupal 7 and concerns pre-processing the canonical URL before display, whereas I want to set the metatag canonical URL when saving the node in Drupal 9.

ru flag
The parts with `letter:number` usually are PHP serialized data structures (complex data structures serialized into one single string), e.g. `a:4` means an `a`rray of length `4`, an `s:5` is a `s`tring of length `5`, etc. You should be able to decode and get read-/var_dump-able keynames and values using [PHP unserialize()](https://www.php.net/manual/en/function.unserialize.php) first.
Score:0
cn flag

Thanks to @Hudri, I was able to figure this out with unserialize().

How to set the advanced metatags by key

The advanced metatags are set the same way as the standard metatags, but you need to know the key to set.

In the case of the canonical URL, the key is canonical_url, so it can be set like this:

  $this->set('field_metatags', serialize([
    'canonical_url' => $canonical_link,
  ]));

How to get the value of the advanced metatags key?

First, in the UI, set the value for a specific entity (for example, at /node/1/edit). I needed to set it for an individual entity because the values that are set globally for entities in the admin UI (at /admin/config/search/metatag) do not show up when you get the value of the field_metatags field for your entity (because Metatag module only stores the overrides for that specific entity in the field).

Then, in your code, you can use Xdebug or something like this:

hook_entity_presave() {
  $metatags = $this->get('field_metatags')->value;
  $unserialized = unserialize($metatags);
  print_r($unserialized);
  die();
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.