Development Version On Drupal 8

This commit is contained in:
samiahmedsiddiqui
2017-09-16 01:30:06 +05:00
parent a2bee5f980
commit 80fb9de84f
14 changed files with 417 additions and 286 deletions

70
src/Form/BodyForm.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
namespace Drupal\header_and_footer_scripts\Form;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
class BodyForm extends ConfigFormBase {
/**
* Implements FormBuilder::getFormId
*/
public function getFormId() {
return 'hfs_body_settings';
}
/**
* Implements ConfigFormBase::getEditableConfigNames
*/
protected function getEditableConfigNames() {
return ['hfs_body_scripts.settings'];
}
/**
* Implements FormBuilder::buildForm
*/
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$body_section = \Drupal::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']['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']['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,
);
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();
\Drupal::service('config.factory')->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');
}
}

70
src/Form/FooterForm.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
namespace Drupal\header_and_footer_scripts\Form;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
class FooterForm extends ConfigFormBase {
/**
* Implements FormBuilder::getFormId
*/
public function getFormId() {
return 'hfs_footer_settings';
}
/**
* Implements ConfigFormBase::getEditableConfigNames
*/
protected function getEditableConfigNames() {
return ['hfs_footer_scripts.settings'];
}
/**
* Implements FormBuilder::buildForm
*/
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$footer_section = \Drupal::config('hfs_footer_scripts.settings')->get();
$form['hfs_footer'] = array(
'#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.'),
);
$form['hfs_footer']['styles'] = array(
'#type' => 'textarea',
'#title' => 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>'),
'#rows' => 10,
);
$form['hfs_footer']['scripts'] = array(
'#type' => 'textarea',
'#title' => 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>'),
'#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();
\Drupal::service('config.factory')->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');
}
}