function header_and_footer_scripts_page_bottom in Header and Footer Scripts 8.2
Same name and namespace in other branches
- 8 header_and_footer_scripts.module \header_and_footer_scripts_page_bottom()
Implements hook_page_bottom().
Add scripts before the Footer tag on overall the site which are defined on the settings page.
File
- ./
header_and_footer_scripts.module, line 157 - Add scripts and styles from the frontend on all over the site.
Code
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 (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];
}
}
}
$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 (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];
}
}
}
$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++;
}
}
}