PanelizerDefaultSelect.php in Panelizer 8.3
File
src/Form/PanelizerDefaultSelect.php
View source
<?php
namespace Drupal\panelizer\Form;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\panelizer\PanelizerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelizerDefaultSelect extends ConfirmFormBase {
protected $entityTypeId;
protected $bundle;
protected $viewMode;
protected $displayId;
protected $panelizer;
protected $invalidator;
public function __construct(PanelizerInterface $panelizer, CacheTagsInvalidatorInterface $invalidator) {
$this->panelizer = $panelizer;
$this->invalidator = $invalidator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('panelizer'), $container
->get('cache_tags.invalidator'));
}
public function getQuestion() {
return 'Are you certain you want to set this panelizer default as the default for this bundle?.';
}
public function getCancelUrl() {
$bundle_entity_type = \Drupal::entityTypeManager()
->getDefinition($this->entityTypeId)
->getBundleEntityType();
if ($this->viewMode == 'default') {
$route = "entity.entity_view_display.{$this->entityTypeId}.default";
$arguments = [
$bundle_entity_type => $this->bundle,
];
}
else {
$route = "entity.entity_view_display.{$this->entityTypeId}.view_mode";
$arguments = [
$bundle_entity_type => $this->bundle,
'view_mode_name' => $this->viewMode,
];
}
return new Url($route, $arguments);
}
public function getFormId() {
return 'panelizer_default_delete';
}
public function buildForm(array $form, FormStateInterface $form_state, $machine_name = NULL) {
list($this->entityTypeId, $this->bundle, $this->viewMode, $this->displayId) = explode('__', $machine_name);
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$display = $this->panelizer
->getEntityViewDisplay($this->entityTypeId, $this->bundle, $this->viewMode);
$settings = $this->panelizer
->getPanelizerSettings($this->entityTypeId, $this->bundle, $this->viewMode, $display);
$settings['default'] = $this->displayId;
$this->panelizer
->setPanelizerSettings($this->entityTypeId, $this->bundle, $this->viewMode, $settings, $display);
$form_state
->setRedirectUrl($this
->getCancelUrl());
$tag = "panelizer_default:{$this->entityTypeId}:{$this->bundle}:{$this->viewMode}";
$this->invalidator
->invalidateTags([
$tag,
]);
}
}