You are here

function header_and_footer_scripts_page_attachments_alter in Header and Footer Scripts 8.2

Implements hook_page_attachments_alter().

Alter CSS/JS files before they are output on the page. which are defined on the settings page.

File

./header_and_footer_scripts.module, line 283
Add scripts and styles from the frontend on all over the site.

Code

function header_and_footer_scripts_page_attachments_alter(array &$attachments) {
  $header_section = \Drupal::config('header_and_footer_scripts.header.settings')
    ->get();
  if (isset($header_section['styles']) && !empty($header_section['styles'])) {
    $output_styles = preg_split("/(<\\/style>|\\/>)/", $header_section['styles']);
    $i = 1;
    $i = count($attachments['#attached']['html_head']) + 1;
    foreach ($output_styles as $row) {
      if (empty($row)) {
        continue;
      }
      $style_tag = 'style';
      $style_attr = [];
      $value = '';
      $style_attributes = preg_replace('/(<style|<link)/', '', $row, 1);
      $get_style_attr = preg_split('/(>)/', $style_attributes, 2);
      if (isset($get_style_attr[1])) {
        $value = $get_style_attr[1];
      }
      $get_style_tag = preg_split('/<link/', $row, 2);
      if (isset($get_style_tag[1])) {
        $style_tag = 'link';
      }
      if (isset($get_style_attr[0]) && !empty($get_style_attr[0])) {
        $get_attr = preg_replace('/(\'|\\")/', '', $get_style_attr[0]);
        $get_attr = preg_replace('/\\s+/', ',', $get_attr);
        $get_attr = preg_replace('/(,=,|,=|=,)/', '=', $get_attr);
        $fetch_attr = explode(',', $get_attr);
        foreach ($fetch_attr as $attr) {
          if (empty($attr)) {
            continue;
          }
          $attr_key_value = explode('=', $attr);
          if (2 <= count($attr_key_value)) {
            $style_attr[$attr_key_value[0]] = preg_replace('/' . $attr_key_value[0] . '=/', '', $attr, 1);
          }
          else {
            $style_attr[$attr_key_value[0]] = $attr_key_value[0];
          }
        }
      }
      $attachments['#attached']['html_head'][$i][0] = [
        '#type' => 'html_tag',
        '#tag' => $style_tag,
        '#value' => $value,
      ];
      if (!empty($style_attr)) {
        $attachments['#attached']['html_head'][$i][0]['#attributes'] = $style_attr;
      }
      $attachments['#attached']['html_head'][$i][1] = 'header-and-footer-css-' . $i;
      $i++;
    }
  }
  if (isset($header_section['scripts']) && !empty($header_section['scripts'])) {
    $output_scripts = preg_split("/(<\\/script>|<\\/noscript>)/", $header_section['scripts']);
    $i = 1;
    $i = count($attachments['#attached']['html_head']) + 1;
    foreach ($output_scripts as $row) {
      if (empty($row)) {
        continue;
      }
      $script_tag = 'script';
      $script_attr = [];
      $value = '';
      $script_attributes = preg_replace('/(<script|<noscript)/', '', $row, 1);
      $get_script_attr = preg_split('/(>)/', $script_attributes, 2);
      if (isset($get_script_attr[1])) {
        $value = $get_script_attr[1];
      }
      $get_script_tag = preg_split('/<noscript/', $row, 2);
      if (isset($get_script_tag[1])) {
        $script_tag = 'noscript';
      }
      if (isset($get_script_attr[0]) && !empty($get_script_attr[0])) {
        $get_attr = preg_replace('/(\'|\\")/', '', $get_script_attr[0]);
        $get_attr = preg_replace('/\\s+/', ',', $get_attr);
        $get_attr = preg_replace('/(,=,|,=|=,)/', '=', $get_attr);
        $fetch_attr = explode(',', $get_attr);
        foreach ($fetch_attr as $attr) {
          if (empty($attr)) {
            continue;
          }
          $attr_key_value = explode('=', $attr);
          if (2 <= count($attr_key_value)) {
            $script_attr[$attr_key_value[0]] = preg_replace('/' . $attr_key_value[0] . '=/', '', $attr, 1);
          }
          else {
            $script_attr[$attr_key_value[0]] = $attr_key_value[0];
          }
        }
      }
      $attachments['#attached']['html_head'][$i] = [
        [
          '#type' => 'html_tag',
          '#tag' => $script_tag,
          '#value' => $value,
        ],
        'header-and-footer-scripts-' . $i,
      ];
      if (!empty($script_attr)) {
        $attachments['#attached']['html_head'][$i][0]['#attributes'] = $script_attr;
      }
      $i++;
    }
  }
}