View source
<?php
namespace Drupal\dashboards\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityForm;
use Drupal\user\UserDataInterface;
use Drupal\dashboards\Entity\Dashboard;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\layout_builder\Form\PreviewToggleTrait;
use Drupal\layout_builder\SectionStorageInterface;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\dashboards\Plugin\SectionStorage\UserDashboardSectionStorage;
class DashboardLayoutBuilderForm extends EntityForm {
use PreviewToggleTrait;
protected $config;
protected $layoutTempstoreRepository;
protected $sectionStorage;
protected $userData;
protected $account;
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_builder.tempstore_repository'), $container
->get('user.data'), $container
->get('current_user'), $container
->get('config.factory'));
}
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, UserDataInterface $user_data, AccountInterface $account, ConfigFactoryInterface $config_factory) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
$this->userData = $user_data;
$this->account = $account;
$this->config = $config_factory
->get('dashboards.settings');
}
public function getBaseFormId() {
return 'dashboards_layout_builder_form';
}
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
$form['layout_builder'] = [
'#type' => 'layout_builder',
'#section_storage' => $section_storage,
'#attached' => [
'library' => [
'dashboards/core',
],
'drupalSettings' => [
'dashboards' => [
'colormap' => $this->config
->get('colormap') ? $this->config
->get('colormap') : 'summer',
'alpha' => $this->config
->get('alpha') ? $this->config
->get('alpha') / 100 : 80,
'shades' => $this->config
->get('shades') ? $this->config
->get('shades') : 15,
],
],
],
];
$this->sectionStorage = $section_storage;
$form = parent::buildForm($form, $form_state);
if ($section_storage instanceof UserDashboardSectionStorage) {
$form['actions']['reset'] = [
'#type' => 'submit',
'#value' => $this
->t('Reset to default'),
'#weight' => 10,
'#submit' => [
'::resetToDefault',
],
];
}
return $form;
}
public function resetToDefault(array $form, FormStateInterface $form_state) {
$dashboard = $this->sectionStorage
->getContextValue(Dashboard::CONTEXT_TYPE);
$this->userData
->delete('dashboards', $this->account
->id(), $dashboard
->id());
$this->layoutTempstoreRepository
->delete($this->sectionStorage);
$response = new TrustedRedirectResponse(Url::fromRoute('entity.dashboard.canonical', [
'dashboard' => $dashboard
->id(),
])
->toString());
$metadata = $response
->getCacheableMetadata();
$metadata
->setCacheTags($dashboard
->getCacheTags());
$metadata
->setCacheContexts($dashboard
->getCacheContexts());
Cache::invalidateTags($dashboard
->getCacheTags());
$form_state
->setResponse($response);
}
public function buildEntity(array $form, FormStateInterface $form_state) {
$entity = $this->sectionStorage
->getContextValue(Dashboard::CONTEXT_TYPE);
$entity->isOverriden = TRUE;
$this
->setEntity($this->sectionStorage
->getContextValue(Dashboard::CONTEXT_TYPE));
$entity = parent::buildEntity($form, $form_state);
$this->sectionStorage
->setContextValue(Dashboard::CONTEXT_TYPE, $entity);
return $entity;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['#attributes']['role'] = 'region';
$actions['#attributes']['aria-label'] = $this
->t('Layout Builder tools');
$actions['submit']['#value'] = $this
->t('Save layout');
$actions['#weight'] = -1000;
$actions['discard_changes'] = [
'#type' => 'submit',
'#value' => $this
->t('Discard changes'),
'#submit' => [
'::redirectOnSubmit',
],
'#redirect' => 'discard_changes',
];
$actions['preview_toggle'] = $this
->buildContentPreviewToggle();
return $actions;
}
public function redirectOnSubmit(array $form, FormStateInterface $form_state) {
$form_state
->setRedirectUrl($this->sectionStorage
->getLayoutBuilderUrl($form_state
->getTriggeringElement()['#redirect']));
}
public function save(array $form, FormStateInterface $form_state) {
$return = $this->sectionStorage
->save();
$this->layoutTempstoreRepository
->delete($this->sectionStorage);
$this
->messenger()
->addMessage($this
->t('The layout has been saved.'));
$form_state
->setRedirectUrl($this->sectionStorage
->getRedirectUrl());
return $return;
}
public function getSectionStorage() {
return $this->sectionStorage;
}
}