PanelsDeleteBlockForm.php in Panels 8.3
File
src/Form/PanelsDeleteBlockForm.php
View source
<?php
namespace Drupal\panels\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\panels\CachedValuesGetterTrait;
use Drupal\user\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelsDeleteBlockForm extends ConfirmFormBase {
use CachedValuesGetterTrait;
protected $tempstore;
protected $tempstore_id;
protected $plugin;
protected $block;
public function __construct(SharedTempStoreFactory $tempstore) {
$this->tempstore = $tempstore;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('user.shared_tempstore'));
}
protected function getTempstoreId() {
return $this->tempstore_id;
}
public function getFormId() {
return 'panels_delete_block_form';
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the block %label?', [
'%label' => $this->block
->label(),
]);
}
public function getCancelUrl() {
return $this
->getRequest()->attributes
->get('destination');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state, $tempstore_id = NULL, $machine_name = NULL, $block_id = NULL) {
$this->tempstore_id = $tempstore_id;
$cached_values = $this
->getCachedValues($this->tempstore, $tempstore_id, $machine_name);
$this->plugin = $cached_values['plugin'];
$this->block = $this->plugin
->getBlock($block_id);
$form['block_display'] = [
'#type' => 'value',
'#value' => $machine_name,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->plugin
->removeBlock($this->block
->getConfiguration()['uuid']);
$cached_values = $this
->getCachedValues($this->tempstore, $this
->getTempstoreId(), $form_state
->getValue('block_display'));
$cached_values['plugin'] = $this->plugin;
if (isset($cached_values['page_variant'])) {
$cached_values['page_variant']
->getVariantPlugin()
->setConfiguration($cached_values['plugin']
->getConfiguration());
}
$this->tempstore
->get($this
->getTempstoreId())
->set($cached_values['id'], $cached_values);
drupal_set_message($this
->t('The block %label has been removed.', [
'%label' => $this->block
->label(),
]));
}
}