I need to add a metatag(og:title) for a path in drupal 8 programatically? 

Comments

vishakraman’s picture

i need it for only specific path, for example /contact

wombatbuddy’s picture

/**
 * Implements hook_page_attachments().
 */
function YOUR_MODULE_page_attachments(array &$page) {
  $current_path = \Drupal::service('path.current')->getPath();

  if ($current_path == '/contact') {
    $metatag = [
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'keywords',
        'content' => 'peace, calmness, the grace of God, love, joy',
      ],
    ];

    $page['#attached']['html_head'][] = [$metatag, 'keywords'];
  }
}
vishakraman’s picture

Thanks