Fix Drupal 8 Coding Standards

This commit is contained in:
samiahmedsiddiqui 2017-09-18 11:41:56 +05:00
parent 80fb9de84f
commit 98161a6384
9 changed files with 166 additions and 117 deletions

View File

@ -4,17 +4,17 @@ from the front-end. You don't need to open any file for this purpose.
INSTALLATION INSTRUCTIONS
-------------------------
1. Copy the files included in the tarball into a directory named "header_and_footer_scripts" in
your Drupal modules/ directory.
2. Login as site administrator.
3. Enable the HFS (Header Footer Scripts) module on the Administer -> Modules
page.
4. Add styles and scripts in settings on the Administer -> Configuration ->
Development -> HFS (Header Footer Scripts) Page.
5. Enjoy.
1. Copy the files included in the tarball into a directory named
"header_and_footer_scripts" in your Drupal modules/ directory.
2. Login as site administrator.
3. Enable the HFS (Header Footer Scripts) module on the
Administer -> Modules page.
4. Add styles and scripts in settings on the Administer ->
Configuration -> Development -> HFS (Header Footer Scripts) Page.
5. Enjoy.
NOTES
-----
This module adds the styles and scripts on all over the site. There are 2
setting pages.
This module adds the styles and scripts on all over the site.
There are 2 setting pages.
These setting pages are allow you add the scripts in the desired region.

View File

@ -9,6 +9,10 @@
* Implements hook_uninstall().
*/
function header_and_footer_scripts_uninstall() {
\Drupal::service('config.factory')->getEditable('hfs_body_scripts.settings')->delete();
\Drupal::service('config.factory')->getEditable('hfs_footer_scripts.settings')->delete();
\Drupal::service('config.factory')
->getEditable('hfs_body_scripts.settings')
->delete();
\Drupal::service('config.factory')
->getEditable('hfs_footer_scripts.settings')
->delete();
}

View File

@ -2,4 +2,4 @@ hfs.admin.body_settings:
title: 'HFS (Header Footer Scripts)'
description: 'Allow to add scripts and styles from the frontend at the start of body tag.'
parent: system.admin_config_development
route_name: hfs.admin.body_settings
route_name: hfs.admin.body_settings

View File

@ -5,4 +5,4 @@ hfs.admin.body_settings:
hfs.admin.footer_settings:
route_name: hfs.admin.footer_settings
title: 'Footer Scripts'
base_route: hfs.admin.body_settings
base_route: hfs.admin.body_settings

View File

@ -2,38 +2,44 @@
/**
* @file
* Enables Drupal to add scripts and styles from frontend on all over the site.
* Enables Drupal to add scripts and styles from frontend
* on all over the site.
*/
/**
* Implements hook_page_top().
*
* Add scripts after the body tag on overall the site which are defined on the settings page.
* 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();
$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;
if (empty($row)) {
continue;
}
$style_tag = 'style';
$style_attr = '';
$value = '';
$value = '';
$style_attributes = preg_replace('/(<style|<link)/', '', $row, 1);
$get_style_attr = preg_split('/(>)/', $style_attributes, 2);
if (isset($get_style_attr[1]))
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_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);
@ -41,23 +47,27 @@ function header_and_footer_scripts_page_top(array &$page_top) {
$fetch_attr = explode(',', $get_attr);
foreach ($fetch_attr as $attr) {
if (empty($attr)) continue;
if (empty($attr)) {
continue;
}
$attr_key_value = explode('=', $attr);
if (isset($attr_key_value[0]) && isset($attr_key_value[1]))
if (isset($attr_key_value[0]) && isset($attr_key_value[1])) {
$style_attr[$attr_key_value[0]] = $attr_key_value[1];
else
} else {
$style_attr[$attr_key_value[0]] = $attr_key_value[0];
}
}
}
$page_top['top_styles_'.$i] = [
'#type' => 'html_tag',
'#tag' => $style_tag,
'#value' => $value
$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;
if (is_array($style_attr)) {
$page_top['top_styles_' . $i]['#attributes'] = $style_attr;
}
$i++;
}
}
@ -66,22 +76,26 @@ function header_and_footer_scripts_page_top(array &$page_top) {
$i = 1;
foreach ($output_scripts as $row) {
if (empty($row)) continue;
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]))
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_tag[1])) {
$script_tag = 'noscript';
}
if (isset($get_script_attr[0]) && !empty($get_script_attr[0])) {
$get_attr = preg_replace('/(\'|\")/', '', $get_script_attr[0]);
@ -90,23 +104,27 @@ function header_and_footer_scripts_page_top(array &$page_top) {
$fetch_attr = explode(',', $get_attr);
foreach ($fetch_attr as $attr) {
if (empty($attr)) continue;
if (empty($attr)) {
continue;
}
$attr_key_value = explode('=', $attr);
if (isset($attr_key_value[0]) && isset($attr_key_value[1]))
if (isset($attr_key_value[0]) && isset($attr_key_value[1])) {
$script_attr[$attr_key_value[0]] = $attr_key_value[1];
else
} else {
$script_attr[$attr_key_value[0]] = $attr_key_value[0];
}
}
}
$page_top['top_scripts_'.$i] = [
'#type' => 'html_tag',
'#tag' => $script_tag,
'#value' => $value
$page_top['top_scripts_' . $i] = [
'#type' => 'html_tag',
'#tag' => $script_tag,
'#value' => $value,
];
if (is_array($script_attr))
if (is_array($script_attr)) {
$page_top['top_scripts_'.$i]['#attributes'] = $script_attr;
}
$i++;
}
}
@ -115,7 +133,8 @@ function header_and_footer_scripts_page_top(array &$page_top) {
/**
* Implements hook_page_bottom().
*
* Add scripts before the Footer tag on overall the site which are defined on the settings page.
* 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();
@ -124,22 +143,26 @@ function header_and_footer_scripts_page_bottom(array &$page_bottom) {
$i = 1;
foreach ($output_styles as $row) {
if (empty($row)) continue;
if (empty($row)) {
continue;
}
$style_tag = 'style';
$style_attr = '';
$value = '';
$value = '';
$style_attributes = preg_replace('/(<style|<link)/', '', $row, 1);
$get_style_attr = preg_split('/(>)/', $style_attributes, 2);
if (isset($get_style_attr[1]))
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_tag[1])) {
$style_tag = 'link';
}
if (isset($get_style_attr[0]) && !empty($get_style_attr[0])) {
$get_attr = preg_replace('/(\'|\")/', '', $get_style_attr[0]);
@ -148,23 +171,27 @@ function header_and_footer_scripts_page_bottom(array &$page_bottom) {
$fetch_attr = explode(',', $get_attr);
foreach ($fetch_attr as $attr) {
if (empty($attr)) continue;
if (empty($attr)) {
continue;
}
$attr_key_value = explode('=', $attr);
if (isset($attr_key_value[0]) && isset($attr_key_value[1]))
if (isset($attr_key_value[0]) && isset($attr_key_value[1])) {
$style_attr[$attr_key_value[0]] = $attr_key_value[1];
else
} else {
$style_attr[$attr_key_value[0]] = $attr_key_value[0];
}
}
}
$page_bottom['bottom_styles_'.$i] = [
'#type' => 'html_tag',
'#tag' => $style_tag,
'#value' => $value
$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;
if (is_array($style_attr)) {
$page_bottom['bottom_styles_' . $i]['#attributes'] = $style_attr;
}
$i++;
}
}
@ -182,13 +209,15 @@ function header_and_footer_scripts_page_bottom(array &$page_bottom) {
$script_attributes = preg_replace('/(<script|<noscript)/', '', $row, 1);
$get_script_attr = preg_split('/(>)/', $script_attributes, 2);
if (isset($get_script_attr[1]))
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_tag[1])) {
$script_tag = 'noscript';
}
if (isset($get_script_attr[0]) && !empty($get_script_attr[0])) {
$get_attr = preg_replace('/(\'|\")/', '', $get_script_attr[0]);
@ -197,23 +226,27 @@ function header_and_footer_scripts_page_bottom(array &$page_bottom) {
$fetch_attr = explode(',', $get_attr);
foreach ($fetch_attr as $attr) {
if (empty($attr)) continue;
if (empty($attr)) {
continue;
}
$attr_key_value = explode('=', $attr);
if (isset($attr_key_value[0]) && isset($attr_key_value[1]))
if (isset($attr_key_value[0]) && isset($attr_key_value[1])) {
$script_attr[$attr_key_value[0]] = $attr_key_value[1];
else
} else {
$script_attr[$attr_key_value[0]] = $attr_key_value[0];
}
}
}
$page_bottom['bottom_scripts_'.$i] = [
'#type' => 'html_tag',
'#tag' => $script_tag,
'#value' => $value
$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;
if (is_array($script_attr)) {
$page_bottom['bottom_scripts_' . $i]['#attributes'] = $script_attr;
}
$i++;
}
}

View File

@ -1,4 +1,4 @@
header_and_footer_scripts_settings:
title: 'Add Scripts all over the site'
description: 'Users who have this permission can add and remove the scripts from the site.'
restrict access: TRUE
restrict access: TRUE

View File

@ -11,4 +11,4 @@ hfs.admin.footer_settings:
_form: '\Drupal\header_and_footer_scripts\Form\FooterForm'
_title: 'HFS (Header Footer Scripts) for Footer'
requirements:
_permission: 'header_and_footer_scripts_settings'
_permission: 'header_and_footer_scripts_settings'

View File

@ -6,48 +6,52 @@ use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
/**
* This Class Provides the settings page for adding CSS/JS after the body tag on the site.
*/
class BodyForm extends ConfigFormBase {
/**
* Implements FormBuilder::getFormId
* Implements FormBuilder::getFormId.
*/
public function getFormId() {
return 'hfs_body_settings';
}
/**
* Implements ConfigFormBase::getEditableConfigNames
* Implements ConfigFormBase::getEditableConfigNames.
*/
protected function getEditableConfigNames() {
return ['hfs_body_scripts.settings'];
}
/**
* Implements FormBuilder::buildForm
* Implements FormBuilder::buildForm.
*/
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$body_section = \Drupal::config('hfs_body_scripts.settings')->get();
$body_section = $this->config('hfs_body_scripts.settings')->get();
$form['hfs_body'] = array(
'#type' => 'fieldset',
'#title' => t('Add Scripts and Styles in body'),
'#description' => t('All the defined scripts and styles in this section would be added next to <strong>body</strong> tag.'),
);
$form['hfs_body'] = [
'#type' => 'fieldset',
'#title' => t('Add Scripts and Styles in body'),
'#description' => t('All the defined scripts and styles in this section would be added next to <strong>body</strong> tag.'),
];
$form['hfs_body']['styles'] = array(
'#type' => 'textarea',
'#title' => t('Body Styles'),
'#default_value' => isset($body_section['styles']) ? $body_section['styles'] : '',
'#description' => 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_body']['styles'] = [
'#type' => 'textarea',
'#title' => $this->t('Body Styles'),
'#default_value' => isset($body_section['styles']) ? $body_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_body']['scripts'] = array(
'#type' => 'textarea',
'#title' => t('Body Scripts'),
'#default_value' => isset($body_section['scripts']) ? $body_section['scripts'] : '',
'#description' => t('<p>On mostly sites, this section is used to add the <strong>Google Tag Manager</strong>. <strong>Like:</strong></p><p>1. &lt;!-- Google Tag Manager --&gt;&lt;noscript&gt;<strong>Write Your code here</strong>&lt;/script&gt;&lt;!-- End Google Tag Manager --&gt;</p><p>You can also 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 Body");&lt;/script&gt;</p>'),
'#rows' => 10,
);
$form['hfs_body']['scripts'] = [
'#type' => 'textarea',
'#title' => $this->t('Body Scripts'),
'#default_value' => isset($body_section['scripts']) ? $body_section['scripts'] : '',
'#description' => $this->t('<p>On mostly sites, this section is used to add the <strong>Google Tag Manager</strong>. <strong>Like:</strong></p><p>1. &lt;!-- Google Tag Manager --&gt;&lt;noscript&gt;<strong>Write Your code here</strong>&lt;/script&gt;&lt;!-- End Google Tag Manager --&gt;</p><p>You can also 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 Body");&lt;/script&gt;</p>'),
'#rows' => 10,
];
return parent::buildForm($form, $form_state);
}
@ -60,11 +64,13 @@ class BodyForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
\Drupal::service('config.factory')->getEditable('hfs_body_scripts.settings')
$this->configFactory()
->getEditable('hfs_body_scripts.settings')
->set('styles', $values['styles'])
->set('scripts', $values['scripts'])
->save();
drupal_set_message($this->t('Your Settings have been saved.'), 'status');
}
}

View File

@ -6,48 +6,52 @@ use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
/**
* This Class Provides the settings page for adding CSS/JS before the end of body tag on the site.
*/
class FooterForm extends ConfigFormBase {
/**
* Implements FormBuilder::getFormId
* Implements FormBuilder::getFormId.
*/
public function getFormId() {
return 'hfs_footer_settings';
}
/**
* Implements ConfigFormBase::getEditableConfigNames
* Implements ConfigFormBase::getEditableConfigNames.
*/
protected function getEditableConfigNames() {
return ['hfs_footer_scripts.settings'];
}
/**
* Implements FormBuilder::buildForm
* Implements FormBuilder::buildForm.
*/
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$footer_section = \Drupal::config('hfs_footer_scripts.settings')->get();
$footer_section = $this->config('hfs_footer_scripts.settings')->get();
$form['hfs_footer'] = array(
$form['hfs_footer'] = [
'#type' => 'fieldset',
'#title' => t('Add Scripts and Styles in Footer'),
'#description' => t('All the defined scripts and styles in this section would be added just before closing the <strong>body</strong> tag.'),
);
'#title' => $this->t('Add Scripts and Styles in Footer'),
'#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_footer']['styles'] = array(
$form['hfs_footer']['styles'] = [
'#type' => 'textarea',
'#title' => t('Footer Styles'),
'#title' => $this->t('Footer Styles'),
'#default_value' => isset($footer_section['styles']) ? $footer_section['styles'] : '',
'#description' => 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>'),
'#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_footer']['scripts'] = array(
$form['hfs_footer']['scripts'] = [
'#type' => 'textarea',
'#title' => t('Footer Scripts'),
'#title' => $this->t('Footer Scripts'),
'#default_value' => isset($footer_section['scripts']) ? $footer_section['scripts'] : '',
'#description' => 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 Footer");&lt;/script&gt;</p>'),
'#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 Footer");&lt;/script&gt;</p>'),
'#rows' => 10,
);
];
return parent::buildForm($form, $form_state);
}
@ -60,11 +64,13 @@ class FooterForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
\Drupal::service('config.factory')->getEditable('hfs_footer_scripts.settings')
$this->configFactory()
->getEditable('hfs_footer_scripts.settings')
->set('styles', $values['styles'])
->set('scripts', $values['scripts'])
->save();
drupal_set_message($this->t('Your Settings have been saved.'), 'status');
}
}