407 lines
12 KiB
Plaintext
Executable File
407 lines
12 KiB
Plaintext
Executable File
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Add scripts and styles from the frontend on all over the site.
|
|
*/
|
|
|
|
/**
|
|
* Implements hook_help().
|
|
*/
|
|
function header_and_footer_scripts_help($route_name,
|
|
\Drupal\Core\Routing\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 don\'t 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 3 regions (Header, 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 -> HFS (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('hfs_body_scripts.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 (is_array($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 (is_array($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('hfs_footer_scripts.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 (is_array($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 (is_array($script_attr)) {
|
|
$page_bottom['bottom_scripts_' . $i]['#attributes'] = $script_attr;
|
|
}
|
|
$i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_page_attachments_alter().
|
|
*
|
|
* Alter CSS/JS files before they are output on the page.
|
|
* which are defined on the settings page.
|
|
*/
|
|
function header_and_footer_scripts_page_attachments_alter(array &$attachments) {
|
|
$header_section = \Drupal::config('hfs_header_scripts.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 (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];
|
|
}
|
|
}
|
|
}
|
|
|
|
$attachments['#attached']['html_head'][$i][0] = [
|
|
'#type' => 'html_tag',
|
|
'#tag' => $style_tag,
|
|
'#value' => $value,
|
|
];
|
|
if (is_array($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 (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];
|
|
}
|
|
}
|
|
}
|
|
|
|
$attachments['#attached']['html_head'][$i] = [
|
|
[
|
|
'#type' => 'html_tag',
|
|
'#tag' => $script_tag,
|
|
'#value' => $value,
|
|
],
|
|
'header-and-footer-scripts-' . $i,
|
|
];
|
|
if (is_array($script_attr)) {
|
|
$attachments['#attached']['html_head'][$i][0]['#attributes'] = $script_attr;
|
|
}
|
|
$i++;
|
|
}
|
|
}
|
|
}
|