View source
<?php
namespace Drupal\alternative_frontpage\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AlternativeFrontpageSettings extends ConfigFormBase {
protected $pathValidator;
public function __construct(ConfigFactoryInterface $config_factory, PathValidatorInterface $path_validator) {
parent::__construct($config_factory);
$this->pathValidator = $path_validator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('path.validator'));
}
protected function getEditableConfigNames() {
return [
'alternative_frontpage.settings',
];
}
public function getFormId() {
return 'alternative_frontpage_settings';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('alternative_frontpage.settings');
$site_config = $this
->config('system.site');
$form['frontpage_for_anonymous_users'] = [
'#type' => 'textfield',
'#title' => $this
->t('Frontpage for anonymous users'),
'#description' => $this
->t('Enter the frontpage for anonymous users. This setting will override the homepage which is set in the Site Configuration form. Enter the path starting with a forward slash. Default: /stream.'),
'#maxlength' => 64,
'#size' => 64,
'#default_value' => $site_config
->get('page.front'),
];
$form['frontpage_for_authenticated_user'] = [
'#type' => 'textfield',
'#title' => $this
->t('Frontpage for authenticated users'),
'#description' => $this
->t('Enter the frontpage for authenticated users. When the value is left empty it will use the anonymous homepage for authenticated users as well. Enter the path starting with a forward slash. Default: /stream.'),
'#maxlength' => 64,
'#size' => 64,
'#default_value' => $config
->get('frontpage_for_authenticated_user'),
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$frontpage_for_anonymous_user = $form_state
->getValue('frontpage_for_anonymous_users');
$frontpage_for_authenticated_user = $form_state
->getValue('frontpage_for_authenticated_user');
if ($frontpage_for_anonymous_user) {
if (!$this->pathValidator
->getUrlIfValidWithoutAccessCheck($frontpage_for_anonymous_user)) {
$form_state
->setErrorByName('frontpage_for_anonymous_users', $this
->t('The path for the anonymous frontpage is not valid.'));
}
elseif (substr($frontpage_for_anonymous_user, 0, 1) !== '/') {
$form_state
->setErrorByName('frontpage_for_anonymous_users', $this
->t('The path for the anonymous frontpage should start with a forward slash.'));
}
elseif (!$this
->isAllowedPath($frontpage_for_anonymous_user)) {
$form_state
->setErrorByName('frontpage_for_anonymous_users', $this
->t('The path for the anonymous frontpage is not allowed.'));
}
}
else {
$form_state
->setErrorByName('frontpage_for_anonymous_users', $this
->t('The path for the anonymous frontpage cannot be empty.'));
}
if ($frontpage_for_authenticated_user) {
if (!$this->pathValidator
->getUrlIfValidWithoutAccessCheck($frontpage_for_authenticated_user)) {
$form_state
->setErrorByName('frontpage_for_authenticated_user', $this
->t('The path for the authenticated frontpage is not valid.'));
}
elseif (substr($frontpage_for_authenticated_user, 0, 1) !== '/') {
$form_state
->setErrorByName('frontpage_for_authenticated_user', $this
->t('The path for the authenticated frontpage should start with a forward slash.'));
}
elseif (!$this
->isAllowedPath($frontpage_for_authenticated_user)) {
$form_state
->setErrorByName('frontpage_for_authenticated_user', $this
->t('The path for the authenticated frontpage is not allowed.'));
}
}
}
private function isAllowedPath($path) {
$unallowed_paths = [
'/user/logout',
'/ajax',
];
foreach ($unallowed_paths as $unallowed_path) {
if ($unallowed_path === substr($path, 0, strlen($unallowed_path))) {
return FALSE;
}
}
return TRUE;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->config('alternative_frontpage.settings')
->set('frontpage_for_authenticated_user', $form_state
->getValue('frontpage_for_authenticated_user'))
->save();
$this->configFactory
->getEditable('system.site')
->set('page.front', $form_state
->getValue('frontpage_for_anonymous_users'))
->save();
}
}