diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..36444ed --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# folders and files to be ignored by git + +############ +## OSes +############ + +[Tt]humbs.db +[Dd]esktop.ini +*.DS_store +.DS_store? diff --git a/README.txt b/README.txt old mode 100644 new mode 100755 diff --git a/header_and_footer_scripts.info.yml b/header_and_footer_scripts.info.yml old mode 100644 new mode 100755 diff --git a/header_and_footer_scripts.install b/header_and_footer_scripts.install old mode 100644 new mode 100755 index 32a8802..81a7c0c --- a/header_and_footer_scripts.install +++ b/header_and_footer_scripts.install @@ -15,4 +15,7 @@ function header_and_footer_scripts_uninstall() { \Drupal::service('config.factory') ->getEditable('hfs_footer_scripts.settings') ->delete(); + \Drupal::service('config.factory') + ->getEditable('hfs_header_scripts.settings') + ->delete(); } diff --git a/header_and_footer_scripts.links.menu.yml b/header_and_footer_scripts.links.menu.yml old mode 100644 new mode 100755 diff --git a/header_and_footer_scripts.links.task.yml b/header_and_footer_scripts.links.task.yml old mode 100644 new mode 100755 index 77710e1..f1f24ea --- a/header_and_footer_scripts.links.task.yml +++ b/header_and_footer_scripts.links.task.yml @@ -1,3 +1,7 @@ +hfs.admin.header_settings: + route_name: hfs.admin.header_settings + title: 'Header Scripts' + base_route: hfs.admin.body_settings hfs.admin.body_settings: route_name: hfs.admin.body_settings title: 'Body Scripts' diff --git a/header_and_footer_scripts.module b/header_and_footer_scripts.module old mode 100644 new mode 100755 index 5ccf714..fea7805 --- a/header_and_footer_scripts.module +++ b/header_and_footer_scripts.module @@ -256,3 +256,135 @@ function header_and_footer_scripts_page_bottom(array &$page_bottom) { } } } + +/** + * 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_attributes, 2); + + if (isset($get_style_attr[1])) { + $value = $get_style_attr[1]; + } + + $get_style_tag = preg_split('/ '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_attributes, 2); + + if (isset($get_script_attr[1])) { + $value = $get_script_attr[1]; + } + + $get_script_tag = preg_split('/