ExportConfirmForm.php in Fixed Block Content 8
File
src/Form/ExportConfirmForm.php
View source
<?php
namespace Drupal\fixed_block_content\Form;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityConfirmFormBase;
class ExportConfirmForm extends EntityConfirmFormBase {
public function getDescription() {
return $this
->t('The current content of the block will be lost.') . ' ' . parent::getDescription();
}
public function getQuestion() {
return $this
->t('Are you sure you want to restore the %block to its default content?', [
'%block' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return Url::fromRoute('entity.fixed_block_content.collection');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity = $this
->getEntity();
if ($block_content = $entity
->getBlockContent(FALSE)) {
$form['update_existing'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Update the existing block content'),
'#description' => $this
->t('The existing block %title (@id) will be updated rather than replace it with a new one.', [
'%title' => $block_content
->label(),
'@id' => $block_content
->id(),
]),
'#default_value' => FALSE,
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity
->exportDefaultContent(!empty($form_state
->getValue('update_existing')));
$this
->messenger()
->addMessage($this
->t('Block %label restored to default content.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}