GroupDeleteForm.php in Context groups 8.2
File
src/Form/GroupDeleteForm.php
View source
<?php
namespace Drupal\context_groups\Form;
use Drupal\context\ContextManager;
use Drupal\context\Entity\Context;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManager;
class GroupDeleteForm extends ConfirmFormBase {
protected $entityTypeManager;
protected $contextManager;
protected $contextGroup;
protected $context;
public function __construct(EntityTypeManager $entity_type_manager, ContextManager $contextManager) {
$this->entityTypeManager = $entity_type_manager;
$this->contextManager = $contextManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('context.manager'));
}
public function getFormId() {
return 'context-group-delete-form';
}
public function getCancelUrl() {
}
public function getQuestion() {
return $this
->t('Are you sure you want to remove the "@label" group?', [
'@label' => $this->contextGroup['label'],
]);
}
public function buildForm(array $form, FormStateInterface $form_state, $context = NULL, $context_group = NULL) {
$this->context = $this->entityTypeManager
->getStorage('context')
->load($context);
$group = $this->context
->getThirdPartySetting('context_groups', $context_group);
$this->contextGroup = $group;
$form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#ajax'] = [
'callback' => '::submitFormAjax',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->context
->unsetThirdPartySetting('context_groups', $this->contextGroup['name']);
$list_of_groups = $this->context
->getThirdPartySettings('context_groups');
foreach ($list_of_groups as $group) {
if ($group['parent'] == $this->contextGroup['name']) {
$group['parent'] = '';
$this->context
->setThirdPartySetting('context_groups', $group['name'], $group);
}
}
$blocks = $this->context
->getReactions()
->get('blocks');
$list_of_blocks = $blocks
->getBlocks();
foreach ($list_of_blocks as $uid => $block_plugin) {
$configuration = $block_plugin
->getConfiguration();
if (isset($configuration['parent_wrapper']['parent']) && $configuration['parent_wrapper']['parent'] == $this->contextGroup['name']) {
$configuration['parent_wrapper']['parent'] = '';
$blocks
->updateBlock($uid, $configuration);
}
}
$this->context
->save();
}
public function submitFormAjax() {
$contextForm = $this->contextManager
->getForm($this->context, 'edit');
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
$response
->addCommand(new ReplaceCommand('#context-reactions', $contextForm['reactions']));
return $response;
}
}