From eac6f1730f27afc6a8a71f7780fc91547e8d4db7 Mon Sep 17 00:00:00 2001 From: samiahmedsiddiqui Date: Mon, 8 Oct 2018 14:46:54 +0500 Subject: [PATCH] Issue #3002872 Support to add styles and scripts in head tag --- .gitignore | 10 ++ README.txt | 0 header_and_footer_scripts.info.yml | 0 header_and_footer_scripts.install | 3 + header_and_footer_scripts.links.menu.yml | 0 header_and_footer_scripts.links.task.yml | 4 + header_and_footer_scripts.module | 132 ++++++++++++++++++++++ header_and_footer_scripts.permissions.yml | 0 header_and_footer_scripts.routing.yml | 9 +- src/Form/BodyForm.php | 0 src/Form/FooterForm.php | 0 src/Form/HeaderForm.php | 76 +++++++++++++ 12 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 .gitignore mode change 100644 => 100755 README.txt mode change 100644 => 100755 header_and_footer_scripts.info.yml mode change 100644 => 100755 header_and_footer_scripts.install mode change 100644 => 100755 header_and_footer_scripts.links.menu.yml mode change 100644 => 100755 header_and_footer_scripts.links.task.yml mode change 100644 => 100755 header_and_footer_scripts.module mode change 100644 => 100755 header_and_footer_scripts.permissions.yml mode change 100644 => 100755 header_and_footer_scripts.routing.yml mode change 100644 => 100755 src/Form/BodyForm.php mode change 100644 => 100755 src/Form/FooterForm.php create mode 100755 src/Form/HeaderForm.php 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('/