FixedBlockContentDeleteForm.php in Fixed Block Content 8
File
src/Form/FixedBlockContentDeleteForm.php
View source
<?php
namespace Drupal\fixed_block_content\Form;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityDeleteFormTrait;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FixedBlockContentDeleteForm extends EntityConfirmFormBase {
use EntityDeleteFormTrait {
submitForm as traitSubmitForm;
}
protected $configManager;
public function __construct(ConfigManagerInterface $config_manager) {
$this->configManager = $config_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.manager'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity = $this
->getEntity();
$this
->addDependencyListsToForm($form, $entity
->getConfigDependencyKey(), [
$entity
->getConfigDependencyName(),
], $this->configManager, $this->entityTypeManager);
if (!$entity
->isProtected() && ($block_content = $entity
->getBlockContent(FALSE))) {
$form['delete_linked_block'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Delete the linked custom block as well'),
'#description' => $this
->t('Check to delete the custom block %title (@id).', [
'%title' => $block_content
->label(),
'@id' => $block_content
->id(),
]),
'#default_value' => FALSE,
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $this
->getEntity();
if (!$entity
->isProtected() && $form_state
->getValue('delete_linked_block') && ($block_content = $entity
->getBlockContent(FALSE))) {
$block_content
->delete();
}
$this
->traitSubmitForm($form, $form_state);
}
}