Issue #3002872 Support to add styles and scripts in head tag

This commit is contained in:
samiahmedsiddiqui 2018-10-08 14:46:54 +05:00
parent 7b233ff0af
commit eac6f1730f
12 changed files with 233 additions and 1 deletions

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
# folders and files to be ignored by git
############
## OSes
############
[Tt]humbs.db
[Dd]esktop.ini
*.DS_store
.DS_store?

0
README.txt Normal file → Executable file
View File

0
header_and_footer_scripts.info.yml Normal file → Executable file
View File

3
header_and_footer_scripts.install Normal file → Executable file
View File

@ -15,4 +15,7 @@ function header_and_footer_scripts_uninstall() {
\Drupal::service('config.factory') \Drupal::service('config.factory')
->getEditable('hfs_footer_scripts.settings') ->getEditable('hfs_footer_scripts.settings')
->delete(); ->delete();
\Drupal::service('config.factory')
->getEditable('hfs_header_scripts.settings')
->delete();
} }

0
header_and_footer_scripts.links.menu.yml Normal file → Executable file
View File

4
header_and_footer_scripts.links.task.yml Normal file → Executable file
View File

@ -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: hfs.admin.body_settings:
route_name: hfs.admin.body_settings route_name: hfs.admin.body_settings
title: 'Body Scripts' title: 'Body Scripts'

132
header_and_footer_scripts.module Normal file → Executable file
View File

@ -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|<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] = array(
'#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] = array(
array(
'#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++;
}
}
}

0
header_and_footer_scripts.permissions.yml Normal file → Executable file
View File

9
header_and_footer_scripts.routing.yml Normal file → Executable file
View File

@ -1,8 +1,15 @@
hfs.admin.header_settings:
path: '/admin/config/development/hfs/header'
defaults:
_form: '\Drupal\header_and_footer_scripts\Form\HeaderForm'
_title: 'HFS (Header Footer Scripts)'
requirements:
_permission: 'header_and_footer_scripts_settings'
hfs.admin.body_settings: hfs.admin.body_settings:
path: '/admin/config/development/hfs/body' path: '/admin/config/development/hfs/body'
defaults: defaults:
_form: '\Drupal\header_and_footer_scripts\Form\BodyForm' _form: '\Drupal\header_and_footer_scripts\Form\BodyForm'
_title: 'HFS (Header Footer Scripts)' _title: 'HFS (Header Footer Scripts) for Body'
requirements: requirements:
_permission: 'header_and_footer_scripts_settings' _permission: 'header_and_footer_scripts_settings'
hfs.admin.footer_settings: hfs.admin.footer_settings:

0
src/Form/BodyForm.php Normal file → Executable file
View File

0
src/Form/FooterForm.php Normal file → Executable file
View File

76
src/Form/HeaderForm.php Executable file
View File

@ -0,0 +1,76 @@
<?php
namespace Drupal\header_and_footer_scripts\Form;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
/**
* Provide settings page for adding CSS/JS before the end of body tag.
*/
class HeaderForm extends ConfigFormBase {
/**
* Implements FormBuilder::getFormId.
*/
public function getFormId() {
return 'hfs_header_settings';
}
/**
* Implements ConfigFormBase::getEditableConfigNames.
*/
protected function getEditableConfigNames() {
return ['hfs_header_scripts.settings'];
}
/**
* Implements FormBuilder::buildForm.
*/
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$header_section = $this->config('hfs_header_scripts.settings')->get();
$form['hfs_header'] = [
'#type' => 'fieldset',
'#title' => $this->t('Add Scripts and Styles in Header'),
'#description' => $this->t('All the defined scripts and styles in this section would be added just before closing the <strong>body</strong> tag.'),
];
$form['hfs_header']['styles'] = [
'#type' => 'textarea',
'#title' => $this->t('Header Styles'),
'#default_value' => isset($header_section['styles']) ? $header_section['styles'] : '',
'#description' => $this->t('<p>You can add multiple <strong>stylesheets</strong> here with multiple ways, For example: </p><p>1. &lt;link type="text/css" rel="stylesheet" href="http://www.example.com/style.css" media="all" /&gt;</p><p> 2. &lt;link type="text/css" rel="stylesheet" href="/style.css" media="all" /&gt;</p><p> 3. &lt;style&gt;#header { color: grey; }&lt;/style&gt;</p>'),
'#rows' => 10,
];
$form['hfs_header']['scripts'] = [
'#type' => 'textarea',
'#title' => $this->t('Header Scripts'),
'#default_value' => isset($header_section['scripts']) ? $header_section['scripts'] : '',
'#description' => $this->t('<p>You can add multiple <strong>scripts</strong> here with multiple ways, For example: </p><p>1. &lt;script type="text/javascript" src="http://www.example.com/script.js"&gt;&lt;/script&gt;</p><p> 2. &lt;script type="text/javascript" src="/script.js"&gt;&lt;/script&gt;</p><p> 3. &lt;script type="text/javascript"&gt;console.log("HFS Header");&lt;/script&gt;</p>'),
'#rows' => 10,
];
return parent::buildForm($form, $form_state);
}
/**
* Implements FormBuilder::submitForm().
*
* Serialize the user's settings and save it to the Drupal's config Table.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$this->configFactory()
->getEditable('hfs_header_scripts.settings')
->set('styles', $values['styles'])
->set('scripts', $values['scripts'])
->save();
drupal_set_message($this->t('Your Settings have been saved.'), 'status');
}
}