Skip to content
Snippets Groups Projects

fix: do not store the form context

Files
4
@@ -3,7 +3,6 @@
namespace Drupal\entity_reference_actions;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\MessageCommand;
@@ -57,13 +56,6 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
*/
protected $requestStack;
/**
* The UUID service.
*
* @var \Drupal\Component\Uuid\UuidInterface
*/
protected $uuidGenerator;
/**
* The HTTP kernel service.
*
@@ -101,16 +93,13 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
* The current user.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack.
* @param \Drupal\Component\Uuid\UuidInterface $uuidGenerator
* The UUID generator service.
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
* The HTTP kernel service.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser, RequestStack $requestStack, UuidInterface $uuidGenerator, HttpKernelInterface $httpKernel) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser, RequestStack $requestStack, HttpKernelInterface $httpKernel) {
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
$this->requestStack = $requestStack;
$this->uuidGenerator = $uuidGenerator;
$this->httpKernel = $httpKernel;
}
@@ -118,7 +107,7 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'), $container->get('current_user'), $container->get('request_stack'), $container->get('uuid'), $container->get('http_kernel'));
return new static($container->get('entity_type.manager'), $container->get('current_user'), $container->get('request_stack'), $container->get('http_kernel'));
}
/**
@@ -170,10 +159,6 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
$items = $context['items'];
$field_definition = $items->getFieldDefinition();
$uuid = 'entity_reference_actions-' . $this->uuidGenerator->generate();
$form_state->set($uuid, $context);
$element['entity_reference_actions_messages'] = [
'#type' => 'container',
'#attributes' => ['data-entity-reference-actions-messages' => ''],
@@ -181,7 +166,8 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
$element['entity_reference_actions'] = [
'#type' => 'simple_actions',
'#uuid' => $uuid,
'#target_ids' => $items->getValue(),
'#target_type' => $items->getSettings()['target_type'],
'#attached' => [
'library' => [
'core/drupal.dialog.ajax',
@@ -196,9 +182,8 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
$bulk_options = $this->getBulkOptions();
foreach ($bulk_options as $id => $label) {
$element['entity_reference_actions'][$id] = [
'#type' => 'submit',
'#type' => 'button',
'#limit_validation_errors' => [$element['widget']['#parents']],
'#submit' => [],
'#id' => $field_definition->getName() . '_' . $id . '_button',
'#name' => $field_definition->getName() . '_' . $id . '_button',
'#value' => $label,
@@ -227,26 +212,14 @@ class EntityReferenceActionsHandler implements ContainerInjectionInterface {
$button = $form_state->getTriggeringElement();
$parents = array_slice($button['#array_parents'], 0, -2);
// The field name we are acting on, deep from the form structure.
$field_name = end($parents);
$parents = array_slice($parents, 0, -1);
$parents = array_slice($button['#array_parents'], 0, -1);
$values = NestedArray::getValue($form, $parents);
$context = $form_state->get($values[$field_name]['entity_reference_actions']['#uuid']);
/** @var \Drupal\Core\Field\FieldItemListInterface $items */
$items = $context['items'];
$context['widget']->extractFormValues($items, $values, $form_state);
$action = $this->actions[end($button['#array_parents'])];
$ids = array_filter(!$items->isEmpty() ? array_column($items->getValue(), 'target_id') : []);
$ids = array_filter(array_column($values['#target_ids'], 'target_id'));
$entities = $this->entityTypeManager->getStorage($items->getSettings()['target_type'])
->loadMultiple($ids);
$entities = $this->entityTypeManager->getStorage($values['#target_type'])->loadMultiple($ids);
$commands = [];
$entities = array_filter($entities, function ($entity) use ($action, &$commands) {
Loading