44

From different sources I found different way of adding meta tags in Drupal 8, hence compiling all ways. 1. Using THEME.theme file I think same question has been asked on stakeoverflow: add meta tag to head in drupal 8 and if you see the answer of @Danielishko, He has provided the following code, Just add following code in your THEME.theme file, clear ...


13

For anyone else looking (from drupal.org/node/2676164): /** * Implements hook_page_attachments_alter(). */ function HOOK_page_attachments_alter(array &$attachments) { foreach ($attachments['#attached']['html_head'] as $key => $attachment) { if ($attachment[1] == 'system_meta_generator') { unset($attachments['#attached']['html_head'][$...


12

The Metatag module has views integration from the box since 7.x-1.0-beta2. Install the metatags_views submodule and you'll get metatag options in the views UI for any views page display (Paul Querol's comment below).


12

The drupal 7 Metatags module has built-in Facebook opengraph support as an add-on module. If your specific metatag is not covered, the Meta tags module allows you to add it manualy. Although not recommended, if you need custom code for specific pages, you may use the theme's preprocess hook. For example, here is the one I wrote on the preprocess-page.inc ...


12

The invocation of the hook_html_head_alter was removed from Drupal core. Issue - Remove invocation of hook_html_head_alter(). D8 has a new hook to achieve it : hook_page_attachments_alter(). Code from the change record : function MYTHEME_page_attachments_alter(array &$page) { $viewport = [ '#type' => 'html_tag', '#tag' => 'meta', ...


11

Metatag does support custom tags by implementing hook_metatag_metatags_view_alter(). See the following hook implementation to add the Open Graph video:duration metatag from a custom entity: /** * Alter metatags before being cached. * * This hook is invoked prior to the meta tags for a given page are cached. * * @param array $output * All of the meta ...


11

Below are the steps without touching the files and using the drupal admin interface. Go to admin/config/search/metatags/config/global:frontpage/edit Add the Page title, Description etc and Save Refresh the front page and check. Done !!


10

Current version of Metatag module includes a submodule "Metatag: Views". Enabling this, you get a new section in your views editor to set all metatags available (specific tags are available in other submodules in a very drupal way).


9

To alter the order of specific meta tags, a hook_html_head_alter() implementation in your theme's template.php file would be your best bet: function MYTHEME_html_head_alter(&$head_elements) { // Combined with the devel module this line will tell you what elements you have to work with dpm($head_elements); // Any you can just alter the weight like ...


8

Drupal 7 You can implement hook_html_head_alter() to change head tags; the following is untested but should do the trick: function MYMODULE_html_head_alter(&$head_elements) { foreach ($head_elements as $key => &$tag) { if (strpos($key, 'drupal_add_html_head_link:canonical:') === 0) { if (strpos('https://', $tag['#attributes']['href'])...


8

When the metatag module is installed, you can create a metatag field in drupal just like creating a textfield, and can be reused in other content types.


8

You can use the Metatag module for this, in order to add meta tags to every page of your site: If you want to do it by coding, it is possible to implement the theme_preprocess_html hook to add the meta tag manually: function theme_preprocess_html(&$variables) { $noindex = [ '#tag' => 'meta', '#attributes' => [ ...


7

PHP can define a string with ' or ". Whichever one you choose, you can't have those characters in the string without escaping them: echo "need to escape any \" in this string" You can use this instead to avoid PHP strings altogether: <?php if ($is_front): ?> <META NAME="KEYWORDS" CONTENT="Geek community,stuffs for geeks,geek contents,tech updates,...


7

You don't have $build in preprocess, so can't work like that. $build needs to be a render array that is rendered somewhere. I also don't see how this could have worked before, as this is already converted in template_preprocess_html() before your function runs. Add hook_page_attachments(_alter)() and deprecate hook_page_build/alter() was just committed, and ...


7

The tags are stored by key, so using a different key for each should do the trick: $delta = 0; foreach ($images as $image) { $element = array( '#tag' => 'meta', '#attributes' => array( "property" => "og:image", "content" => $image, ), ); drupal_add_html_head($element,'...


7

If I understand your question correctly you'll need to do the following: Check on your modules page that the "Metatag: Open Graph" submodule is enabled. Go to metatag config page (Configuration > Search and metadata > Metatag). Add a new "default metatag" and select your content type in the "Type" selectbox. Fill in the rest of the fields similar like ...


6

You can use hook_preprocess_html() to override this value: function MYTHEME_preprocess_html(&$vars) { $vars['head_title'] = 'Some other title'; } The title for the content won't be affected by that, just the <title> tag.


6

I see its one of those things that you once you know. Enable modules: Token Help (core) Navigate to the page: /admin/help/token.


5

Replying late but this may be helpful for others: I have written about my solution here http://mymixerone.blogspot.in/2012/11/adding-meta-tags-in-drupal-node.html Just have a look if its helpful. Here is the solution in brief, Try adding this in your templet.php file: <?php function <theme-name>_preprocess_html(&$variables) { switch(arg(1))...


5

I use the following function within my template.php file: /** * Used to remove certain elements from the $head output within html.tpl.php * * @see http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_html_head_alter/7 * @param array $head_elements */ function YOUR_THEME_NAME_html_head_alter(&$head_elements) { $remove = ...


5

HTML head elements are defined in 2 places. First one, easiest to access, is your theme's html.tpl.php. In Professional Theme relevant part looks like that: <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <?php print $head; ?> <title><?php print $head_title; ?></title> <?php print $styles; ?> <?...


5

In your theme's template.php file, you could add something like /** * Implements hook_preprocess_html */ function THEME_NAME_preprocess_html(&$vars) { if (current_path() == 'my/custom/path') { $description = array( '#type' => 'html_tag', '#tag' => 'meta', '#attributes' => array( 'name' => '...


5

If you use the metatag module you can use the -favicon and -mobile submodules. If you visit admin/config/search/metatags (Configuration > Search and Metadata > Metatags), you can alter a metatag set. And you'll get this section: Here you can enter the paths to the files relative to your Drupal root. eg. sites/default/files/metatags/favicon.ico. Depending ...


4

I solved shortlink too, modyfying your code. Someone could tidy it up properly, because I don't know how to put it properly, and neither if it has side effects in this way or not. It makes the unwanted links disappear from the head, though. function nameof_mytheme_html_head_alter(&$head_elements) { unset($head_elements['system_meta_generator']); ...


4

I think you definitely could use hook_page_build() (it would work), but since that hook is really intended for use with page.tpl.php, and meta tags are outputted in html.tpl.php, I think it might not be the 'Drupal' way. Another option might be to use hook_html_head_alter(), which allows you to alter the <head> tags before they're rendered: function ...


4

Apparently the meta tag isn't the problem. The problem is the validation service and how you use it. After some digging I found this article. The author suggest using a different validation type namely: http://validator.w3.org/nu/ with the following preset set in the additional options: "HTML5 + SVG 1.1 + MathML 2.0 + RDFa 1.1 + Microdata". This will ...


4

There is now the Meta tags: Panels module. Works fine for me. This module extends the Meta tags module by adding support for meta tags configuration for panels pages.


4

If your meta tags aren't in your page source, search engines (and any other robots) won't be able to read them. You should never have anything sensitive in meta tags anyway so trying to hide them doesn't really make sense.


4

It's definitely possible, you could do this using hook_entity_view() for example: function MYMODULE_entity_view($entity, $type, $view_mode, $langcode) { if ($type == 'my_entity_type' && $view_mode == 'some_view_mode') { // Check the field items. if ($items = field_get_items('my_entity_type', $entity, 'field_visibility')) { if ($items[...


Only top voted, non community-wiki answers of a minimum length are eligible