MikheevDesign's picture

Hello, I am trying to redefine meta tags for a web page and use this function function YOURTHEME_preprocess_html (& $ variables) {, I already realized that this does not work, but I have not found an analogue yet, how can I replace it?

Most helpful answers

Comments

MikheevDesign's picture

Thank you, I looked ngo in the meta tag module folder there is no such file, there is an API there, but only an example with a title

MikheevDesign's picture

I apologize immediately, I did not figure it out, now I will understand and I will try, the link to the github is really useful in this case

MikheevDesign's picture

I understand that this code no longer works function YOURTHEME_preprocess_html (& $ variables) {

  // Set specific metatag description to Contact page, for avoiding
  // Google Webmaster Tools warning about duplicate description with homepage
  if (drupal_get_path_alias () == 'contact') {
    $ metag = array (
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => array (
         'name' => 'description',
         'content' => 'Your contact page description here.',
      ),
    );
    drupal_add_html_head ($ metag, 'metatag_description_0');
  }
}

even if using function template_preprocess_page (& $ variables), as soon as I add function backdrop_get_path_alias after this function, I get a "white screen". It's a pity that there are no examples of working with preprocessors.

The examples that I found in system themes are too simple, they just add style or a font.

MikheevDesign's picture

I just can't figure out how to get the page address in the function template_preprocess_page, I tried it like this function template_preprocess_page (& $ variables) {
  if (backdrop_get_path_alias ($ path = 'contact')) {to no avail, get error

Make sure you replace 'template' (in the function name) with your theme name, like you mentioned in your first post. For example:

YOURTHEME_preprocess_page(&$variables) {
  if (backdrop_get_path_alias() == 'contact') {
    // Set custom Metatags here...
  }
}
MikheevDesign's picture

I write that, but for some reason it does not work, I figured out the error, but the meta description is not displayed


function lateral_preprocess_page(&$variables) {
   if (backdrop_get_path_alias() == 'contact') {
    $metatag_description = array(
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => array(
         'name' => 'description',
         'content' => 'Your contact page description here.',
      ),
    );
    backdrop_add_html_head($metatag_description, 'description');
  }
jenlampton's picture

We don't have the function theme_html_tag in Backdrop, we've switched to using theme_head_tag instead: https://docs.backdropcms.org/change-records/themehtmltag-becomes-themehe...

Your code would need to look like this:

function lateral_preprocess_page(&$variables) {
  if (backdrop_get_path_alias() == 'contact') {
    $metatag_description = array( 
      '#type' => 'head_tag', 
      '#tag' => 'meta', 
      '#attributes' => array( 
        'name' => 'description', 
        'content' => 'Your contact page description here.', 
      ), 
    ); 
    backdrop_add_html_head($metatag_description, 'description');
  }
}

I often like to look at examples of how this is done in core, so here's an example of the viewport head tag being added:

https://docs.backdropcms.org/api/backdrop/core%21includes%21common.inc/f...