2

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.

1
  • 2
    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 array of length 4, an s:5 is a string of length 5, etc. You should be able to decode and get read-/var_dump-able keynames and values using PHP unserialize() first.
    – Hudri
    Commented May 16, 2022 at 9:17

2 Answers 2

2

You might also consider using tokens to fill in the meta tags at runtime instead of saving them directly on each node. This is particularly useful as it allows you adjust the settings to all such nodes at once, instead of having to deal with one-off customizations, so if in six months you decide to change them you just change the global defaults for that content type, instead of having to modify the values saved on each node.

1

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.