StaticPreviewForm.php in Tome 8
File
modules/tome_static/src/Form/StaticPreviewForm.php
View source
<?php
namespace Drupal\tome_static\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\tome_static\EventSubscriber\StaticPreviewRequestSubscriber;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class StaticPreviewForm extends FormBase {
protected $session;
public function __construct(Session $session) {
$this->session = $session;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('session'));
}
public function getFormId() {
return 'tome_static_generator_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = [
'#markup' => '<p>' . $this
->t('Submitting this form will initiate a preview session of your static site. You can exit the preview by clicking the link at the top of preview pages.') . '</p>',
];
$form['warning'] = [
'#markup' => '<p>' . $this
->t('Note that static assets (CSS, JS, images, etc.) will not necessarily be served from the static directory, so please make a final review on a staging static domain if possible.') . '</p>',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Preview'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->session
->set(StaticPreviewRequestSubscriber::SESSION_KEY, TRUE);
$form_state
->setRedirectUrl(Url::fromUserInput('/'));
}
}