Problem/Motivation

The example code for MYMODULE_add_to_head_profiles_alter() on the project page is only valid for Drupal 7. The structure of the profile definition has changed for the Drupal 8 version.

Proposed resolution

Provide an additional code example for Drupal 8.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

reinvented created an issue. See original summary.

nicholasthompson’s picture

Ah good point!

vitaliyb98’s picture

Status: Active » Needs review

Hi @nicholasthompson,

Those examples on the module’s info page are outdated (they're for Drupal 7), and the old examples may even mislead or negatively impact users. Let's update them with examples compatible with Drupal 8–11.

/**
 * Implements hook_add_to_head_profiles_alter().
 */
function MYMODULE_add_to_head_profiles_alter(&$profiles) {
  // Add the Facebook remarketing pixel.
  $profiles['facebook-remarketing-pixel'] = [
    'name' => 'facebook-remarketing-pixel',
    'scope' => 'scripts',
    'paths' => [
      'visibility' => 'include',
      'paths' => '<front>',
    ],
    'roles' => [
      'visibility' => 'exclude',
      'list' => [],
    ],
    'code' => <<<'CODE'
<!-- start of Facebook pixel tag-->
<script>(function() {
var _fbq = window._fbq || (window._fbq = []);
if (!_fbq.loaded) {
var fbds = document.createElement('script');
fbds.async = true;
fbds.src = '//connect.facebook.net/en_US/fbds.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(fbds, s);
_fbq.loaded = true;
}
_fbq.push(['addPixelId', 'XXXXXXXXXXXXXXX']);
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', 'PixelInitialized', {}]);
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?id=XXXXXXXXXXXXXXX&amp;ev=PixelInitialized" /></noscript>
CODE
  ];
}

baikho made their first commit to this issue’s fork.

  • baikho committed c8a9a2b6 on 8.x-1.x
    docs: #3239524 Update example code for Drupal 8
    
    By: reinvented
    By:...
baikho’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.