-2

At WordPress there is a well-known way to add a custom function for the theme and hook the code to the like this:

function add_customcode_header(){
    if(ICL_LANGUAGE_CODE == 'en')
      {
        echo '<link rel="stylesheet" type="text/css" href="/cookie-consent/cookieconsent.min.css" />
<script src="/cookie-consent/cookieconsent.min.js"></script>
<script>
window.addEventListener("load", function(){
window.cookieconsent.initialise({
  "palette": {
    "popup": {
      "background": "#000000"
    },
    "button": {
      "background": "#ffffff"
    }
  },
  "showLink": false,
  "position": "bottom-left",
  "content": {
    "message": "This website uses cookies to give you the best, most relevant experience. Using this website means you are Ok with this. However, if you would like to, you can change your cookie settings at any time.",
    "dismiss": "Got it!",
    "link": "Learn more"
  }
})});
</script>';
    } 
      else
      {
        echo '<link rel="stylesheet" type="text/css" href="/cookie-consent/cookieconsent.min.css" />
<script src="/cookie-consent/cookieconsent.min.js"></script>
<script>
window.addEventListener("load", function(){
window.cookieconsent.initialise({
  "palette": {
    "popup": {
      "background": "#000000"
    },
    "button": {
      "background": "#ffffff"
    }
  },
  "showLink": false,
  "position": "bottom-left",
  "content": {
    "message": "This website uses cookies to give you the best, most relevant experience. Using this website means you are Ok with this. However, if you would like to, you can change your cookie settings at any time.",
    "dismiss": "Got it!",
    "link": "Learn more"
  }
})});
</script>';
      }
    }
    add_action('wp_head', 'add_customcode_header');

How to adjust this code to work on Drupal 7 theme? I need to place the code on all front-end pages header according to the language. Where do I put it?

Edit: Sorry, I that did not provided enough information. I need to add this code to the <head></head> section of every page:

<link rel="stylesheet" type="text/css" href="/cookie-consent/cookieconsent.min.css" />
<script src="/cookie-consent/cookieconsent.min.js"></script>
<script>
window.addEventListener("load", function(){
window.cookieconsent.initialise({
  "palette": {
    "popup": {
      "background": "#000000"
    },
    "button": {
      "background": "#ffffff"
    }
  },
  "showLink": false,
  "position": "bottom-left",
  "content": {
    "message": "This website uses cookies to give you the best, most relevant experience. Using this website means you are Ok with this. However, if you would like to, you can change your cookie settings at any time.",
    "dismiss": "Got it!",
    "link": "Learn more"
  }
})});
</script>

There are 3 languages on the website and I need to place a separate code for each of the language.

  • Not sure what the downvotes are for, but you should look over the D7 docs on theming and theme hooks, everything you'd want to know and transposing knowledge can be found there. – Kevin Feb 19 '18 at 21:58
  • @Kevin It could be the down-votes are for the OP not explaining correctly what the code does. I totally agree that reading the documentation about theming would be a good start, or the OP would have many problems with converting code to Drupal. – kiamlaluno Feb 20 '18 at 8:36
  • @Kevin @kiamlaluno sorry guys, I just updated the thread to be more clear. I`ve worked with WordPress previously but Drupal is a bit different CMS. – twelvell Feb 20 '18 at 22:38
  • Well... you certainly don't want to approach it like this. – Kevin Feb 21 '18 at 1:47
  • @Kevin What would be the best way to implement this? – twelvell Feb 21 '18 at 22:52
1

Since add_action('wp_head', 'add_customcode_header') would add the output of add_customcode_header() to the <head> tag, the Drupal 7 equivalent is implementing a preprocess function for the HTML output.

Supposing that mytheme is the machine name of the theme, and I want to add a <link> tag for Google's Font API inclusion, this the code I would use.

function mytheme_preprocess_html(&$variables) {
  if ($variables['language']->language == 'en') {
    // The page is using English.
    $element = array(
      '#tag' => 'link', 
      '#attributes' => array(
        'href' => 'http://fonts.googleapis.com/css?family=Cardo&subset=latin', 
        'rel' => 'stylesheet',
        'type' => 'text/css',
      ),
    );
    drupal_add_html_head($element, 'google_font_cardo');
  }
}

This would add <link href="http://fonts.googleapis.com/css?family=Cardo&amp;subset=latin" rel="stylesheet" type="text/css" /> inside the <head> tag when the language used for the page is English.

| improve this answer | |
0

There's a number of ways you can do this, but I would look into creating a custom block. Your logic that acts on the language would be in the block's output code, and you'd simply use the Block Manager to put it in the correct place in the header. This is a blog I've used before which should be useful (I'm not the author)

http://redcrackle.com/blog/how-create-block-drupal-7

In the tutorial where you see this line:

$block['content'] = t('Hello World!');

You'd instead call a function that returns the content as applicable based on the language.

| improve this answer | |
  • 1
    If he wants to add a tag in the head, OP will need template_preprocess_html, not a block – Kevin Feb 19 '18 at 22:03
  • He said "header", which is a region of the template, not "head". – Geat Feb 19 '18 at 23:12
  • 1
    Yes, the OP wrongly spoke of header, but the purpose of add_action('wp_head', 'add_customcode_header') is adding tags to <head>. – kiamlaluno Feb 20 '18 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.