Change a metatag dynamicly | Drupal 8

Change a metatag dynamicly

Submitted by editor on Fri, 07/28/2017 - 16:22
Question

How to change a metatag programaticly on drupal ?

Add meta tag from controller

Example: Add <meta http-equiv="refresh" content="30">
$data = [
  '#tag' => 'meta',
  '#attributes' => [
    'http-equiv' => 'refresh',
    'content' => '30',
  ],
];
$output['#attached']['html_head'][] = [$data, 'refresh'];

 

And also:

The metatag module provide an API (hook) to alter all meta tags : hook_metatag_metatags_view_alter

Example:

NOTE : This is not the drupal best practice.

/**
 *
 * Implement hook_metatag_metatags_view_alter().
 *  - Translate 'description' metatag of the home page.
 * @param type $output
 * @param type $instance
 * @param type $options
 */
function mymodule_metatag_metatags_view_alter(&$output, $instance, $options) {
  if ($instance == 'global:frontpage') {
    // Use dpm or kint to get the field to change.    
    if (!empty($value = $output['description']['#attached']['drupal_add_html_head'][0][0]['#value'])) {
      $output['description']['#attached']['drupal_add_html_head'][0][0]['#value'] = "The new value";
    }
  }
}

Comments

R HILDAN M (not verified)

Fri, 12/14/2018 - 17:08

ini ke 14 pabrik plat stainless ss304 pabrik plat stainless ss310 pabrik round bar besi aisi 440s45c pabrik round bar scm440 pabrik steel rail pabrik steel sheet pile pusat astm a283 c pusat pipa boiler pusat plat abrex 400 pusat plat asme516 grade70 pusat plat astm a285 c Toko Atap Spandek Toko Atap Spandek Distributor atap spandek Distributor besi siku undangan pernikahan klasik Jual Plat Kapal Bki Krakatau Steel Jual Besi Wf Baja Toko sch 40 Jual besi beton krakatau steel ks undangan pernikahan kalender undangan pernikahan bahasa inggris islam undangan pernikahan islami undangan pernikahan unik dan murah undangan pernikahan modern undangan pernikahan online undangan pernikahan undangan pernikahan amplop surat undangan pernikahan simple undangan pernikahan murah undangan pernikahan unik undangan pernikahan artis undangan pernikahan modern undangan pernikahan artis undangan pernikahan arab undangan pernikahan undangan pernikahan Agen Atap Spandek Agen Atap Spandek Distributor baja ringan Distributor Plat besi plat kapal undangan pernikahan aneh Jual Jual Besi Siku Baja Harga Besi Wf Baja Agen sch 40 Jual besi beton jcac Pabrik Atap Spandek Pabrik Atap Spandek Distributor bondek Distributor besi h beam baja undangan pernikahan mewah Jual Besi Unp Baja Profil Kanal Pabrik Besi Wf Baja Pabrik sch 40 Jual besi beton interworld steel is Distributor Bondek Cor Distributor Bondek Cor Distributor Plat kapal besi baja bki krakatau steel Distributor besi cnp undangan pernikahan cute Jual Besi Cnp Profil Baja Gording Distributor Plat Besi Baja Distributor besi Pipa sch 80 baja Jual besi beton gunung garuda Harga Bondek Cor Harga Bondek Cor Distributor besi siku www.gudangbesibaja.com undangan pernikahan online Jual Besi Hollow Supplier Plat Besi Baja Harga besi Pipa sch 80 baja Jual besi beton delcoprima Jual Bondek Cor Jual Bondek Cor Distributor pipa besi baja sch 40 sch 80 Jual besi Wf Baja undangan pernikahan buatan sendiri Jual Wiremesh

Add new comment