Issue #3002872 Support to add styles and scripts in head tag
This commit is contained in:
0
src/Form/BodyForm.php
Normal file → Executable file
0
src/Form/BodyForm.php
Normal file → Executable file
0
src/Form/FooterForm.php
Normal file → Executable file
0
src/Form/FooterForm.php
Normal file → Executable file
76
src/Form/HeaderForm.php
Executable file
76
src/Form/HeaderForm.php
Executable 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. <link type="text/css" rel="stylesheet" href="http://www.example.com/style.css" media="all" /></p><p> 2. <link type="text/css" rel="stylesheet" href="/style.css" media="all" /></p><p> 3. <style>#header { color: grey; }</style></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. <script type="text/javascript" src="http://www.example.com/script.js"></script></p><p> 2. <script type="text/javascript" src="/script.js"></script></p><p> 3. <script type="text/javascript">console.log("HFS Header");</script></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');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user