View source
<?php
namespace Drupal\panelizer\Form;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\panelizer\PanelizerInterface;
use Drupal\panels\PanelsDisplayManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelizerDefaultDelete extends ConfirmFormBase {
protected $entityTypeId;
protected $bundle;
protected $viewMode;
protected $displayId;
protected $entityTypeManager;
protected $panelizer;
protected $panelsDisplayManager;
protected $invalidator;
public function __construct(EntityTypeManagerInterface $entity_type_manager, PanelizerInterface $panelizer, PanelsDisplayManagerInterface $panels_display_manager, CacheTagsInvalidatorInterface $invalidator) {
$this->entityTypeManager = $entity_type_manager;
$this->panelizer = $panelizer;
$this->panelsDisplayManager = $panels_display_manager;
$this->invalidator = $invalidator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('panelizer'), $container
->get('panels.display_manager'), $container
->get('cache_tags.invalidator'));
}
public function getQuestion() {
return 'Are you certain you want to delete this panelizer default?.';
}
public function getCancelUrl() {
$bundle_entity_type = $this->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);
$displays = $this->panelizer
->getDefaultPanelsDisplays($this->entityTypeId, $this->bundle, $this->viewMode, $display);
unset($displays[$this->displayId]);
foreach ($displays as $key => $value) {
$displays[$key] = $this->panelsDisplayManager
->exportDisplay($value);
}
$display
->setThirdPartySetting('panelizer', 'displays', $displays);
$display
->save();
$form_state
->setRedirectUrl($this
->getCancelUrl());
$tag = "panelizer_default:{$this->entityTypeId}:{$this->bundle}:{$this->viewMode}:{$this->displayId}";
$this->invalidator
->invalidateTags([
$tag,
]);
}
}