Active
Project:
Metatag
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
17 Aug 2020 at 19:42 UTC
Updated:
17 Aug 2020 at 19:42 UTC
Jump to comment: Most recent
The api file shows various alterations that Metatag supports. See below for a simple example. However, the example will not work correctly when the site changes its frontpage path (for example). In general, it would be helpful if the API docs demonstrate how to bubble up cacheability metatag during an alter hook.
function hook_metatags_attachments_alter(array &$metatag_attachments) {
if (\Drupal::service('path.matcher')->isFrontPage() && \Drupal::currentUser()->isAnonymous()) {
foreach ($metatag_attachments['#attached']['html_head'] as $id => $attachment) {
if ($attachment[1] == 'title') {
$metatag_attachments['#attached']['html_head'][$id][0]['#attributes']['content'] = 'Front Page Title for Anonymous Users';
}
}
}
}
Comments