PageSpecificClassSettingsForm.php in Page Specific Class 8
File
src/Form/PageSpecificClassSettingsForm.php
View source
<?php
namespace Drupal\page_specific_class\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class PageSpecificClassSettingsForm extends ConfigFormBase {
public function getFormId() {
return 'page_specific_class_settings_form';
}
protected function getEditableConfigNames() {
return [
'page_specific_class.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('page_specific_class.settings');
$description = $this
->t('Mention path of pages where you want to add class in body tag.');
$description .= '<ul>';
$description .= '<li>' . $this
->t('An example <b>path is /hello-world</b> and you want to <b>add class "xyz"</b> on body tag of this page then enter <b>/hello-world|xyz</b> in text-area') . '</li>';
$description .= '<li>' . $this
->t('An example <b>path is /page-example</b> and you want to <b>add multiple classes like "xyz1 xyz2 xyz3"</b> on body tag of this page then enter <b>/hello-world|xyz1 xyz2 xyz3</b> in text-area') . '</li>';
$description .= '<li>' . $this
->t('If you want to <b>add class "home-page"</b> in <b>home page</b> body tag then enter <b>/<front>|home-page</b>') . '</li>';
$description .= '<li>' . $this
->t('To <b>add class "all-page"</b> in <b>each page</b> body tag then enter <b>/*|all-page</b>') . '</li>';
$description .= '<li>' . $this
->t('Enter <b>one path</b> along with class <b>per line</b>') . '</li>';
$description .= '<li>' . $this
->t('Path should <b>start with "/"</b> as well as path and class should <b>seperated with "|".</b>') . '</li>';
$description .= '</ul>';
$form['url_with_class'] = [
'#type' => 'textarea',
'#title' => $this
->t('Enter url along with class "|" seperated and path should start with "/"'),
'#description' => $description,
'#rows' => 10,
'#default_value' => $config
->get('url_with_class'),
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$enteredArr = explode(PHP_EOL, $form_state
->getValue('url_with_class'));
foreach ($enteredArr as $values) {
$urlWithClassArr = explode("|", $values);
$url = $urlWithClassArr[0];
if ($url[0] !== '/') {
$form_state
->setErrorByName('url_with_class', $this
->t("@url path needs to start with a slash.", [
'@url' => $url,
]));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$urlWithClass = $form_state
->getValue('url_with_class');
$config = $this
->config('page_specific_class.settings');
$config
->set('url_with_class', $urlWithClass);
$config
->save();
parent::submitForm($form, $form_state);
}
}