header_and_footer_scripts.module in Header and Footer Scripts 8
Same filename and directory in other branches
Add scripts and styles from the frontend on all over the site.
File
header_and_footer_scripts.moduleView source
<?php
/**
* @file
* Add scripts and styles from the frontend on all over the site.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function header_and_footer_scripts_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.header_and_footer_scripts':
$output = '<h2>' . t('About') . '</h2>';
$output .= '<p>' . t('This module allows you to add style and scripts in your site or , You do not need to open any file for this purpose.') . '</p>';
$output .= '<h2>' . t('Uses') . '</h2>';
$output .= '<p>' . t('Header and Footer Scripts provides you the capability to quickly add the style and scripts in your site. It provides you 2 regions (Body, and Footer) of the page on which you can add the style and script. You can add custom style/script file, add Google or other Analytics code, inline css, inline js and so on. You can configure them from the Administer -> Configuration -> Development -> Header Footer Scripts') . '</p>';
return $output;
}
}
/**
* Implements hook_page_top().
*
* Add scripts after the body tag on overall the site
* which are defined on the settings page.
*/
function header_and_footer_scripts_page_top(array &$page_top) {
$body_section = \Drupal::config('header_and_footer_scripts.body.settings')
->get();
if (isset($body_section['styles']) && !empty($body_section['styles'])) {
$output_styles = preg_split("/(<\\/style>|\\/>)/", $body_section['styles']);
$i = 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 (isset($attr_key_value[0]) && isset($attr_key_value[1])) {
$style_attr[$attr_key_value[0]] = $attr_key_value[1];
}
else {
$style_attr[$attr_key_value[0]] = $attr_key_value[0];
}
}
}
$page_top['top_styles_' . $i] = [
'#type' => 'html_tag',
'#tag' => $style_tag,
'#value' => $value,
];
if (!empty($style_attr)) {
$page_top['top_styles_' . $i]['#attributes'] = $style_attr;
}
$i++;
}
}
if (isset($body_section['scripts']) && !empty($body_section['scripts'])) {
$output_scripts = preg_split("/(<\\/script>|<\\/noscript>)/", $body_section['scripts']);
$i = 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 (isset($attr_key_value[0]) && isset($attr_key_value[1])) {
$script_attr[$attr_key_value[0]] = $attr_key_value[1];
}
else {
$script_attr[$attr_key_value[0]] = $attr_key_value[0];
}
}
}
$page_top['top_scripts_' . $i] = [
'#type' => 'html_tag',
'#tag' => $script_tag,
'#value' => $value,
];
if (!empty($script_attr)) {
$page_top['top_scripts_' . $i]['#attributes'] = $script_attr;
}
$i++;
}
}
}
/**
* Implements hook_page_bottom().
*
* Add scripts before the Footer tag on overall the site
* which are defined on the settings page.
*/
function header_and_footer_scripts_page_bottom(array &$page_bottom) {
$footer_section = \Drupal::config('header_and_footer_scripts.footer.settings')
->get();
if (isset($footer_section['styles']) && !empty($footer_section['styles'])) {
$output_styles = preg_split("/(<\\/style>|\\/>)/", $footer_section['styles']);
$i = 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 (isset($attr_key_value[0]) && isset($attr_key_value[1])) {
$style_attr[$attr_key_value[0]] = $attr_key_value[1];
}
else {
$style_attr[$attr_key_value[0]] = $attr_key_value[0];
}
}
}
$page_bottom['bottom_styles_' . $i] = [
'#type' => 'html_tag',
'#tag' => $style_tag,
'#value' => $value,
];
if (!empty($style_attr)) {
$page_bottom['bottom_styles_' . $i]['#attributes'] = $style_attr;
}
$i++;
}
}
if (isset($footer_section['scripts']) && !empty($footer_section['scripts'])) {
$output_scripts = preg_split("/(<\\/script>|<\\/noscript>)/", $footer_section['scripts']);
$i = 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 (isset($attr_key_value[0]) && isset($attr_key_value[1])) {
$script_attr[$attr_key_value[0]] = $attr_key_value[1];
}
else {
$script_attr[$attr_key_value[0]] = $attr_key_value[0];
}
}
}
$page_bottom['bottom_scripts_' . $i] = [
'#type' => 'html_tag',
'#tag' => $script_tag,
'#value' => $value,
];
if (!empty($script_attr)) {
$page_bottom['bottom_scripts_' . $i]['#attributes'] = $script_attr;
}
$i++;
}
}
}
Functions
Name | Description |
---|---|
header_and_footer_scripts_help | Implements hook_help(). |
header_and_footer_scripts_page_bottom | Implements hook_page_bottom(). |
header_and_footer_scripts_page_top | Implements hook_page_top(). |