Skip to content

Instantly share code, notes, and snippets.

@damiankloip
Created May 29, 2015 08:38
Show Gist options
  • Save damiankloip/435210b69c194cb15b6a to your computer and use it in GitHub Desktop.
Save damiankloip/435210b69c194cb15b6a to your computer and use it in GitHub Desktop.
diff --git a/core/modules/system/src/Plugin/views/field/BulkForm.php b/core/modules/system/src/Plugin/views/field/BulkForm.php
index 5587ffa..4e42203 100644
--- a/core/modules/system/src/Plugin/views/field/BulkForm.php
+++ b/core/modules/system/src/Plugin/views/field/BulkForm.php
@@ -7,9 +7,13 @@
namespace Drupal\system\Plugin\views\field;
+use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
+use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
+use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\field\UncacheableFieldHandlerTrait;
@@ -175,6 +179,8 @@ public function viewsForm(&$form, FormStateInterface $form_state) {
// Render checkboxes for all rows.
$form[$this->options['id']]['#tree'] = TRUE;
foreach ($this->view->result as $row_index => $row) {
+ $entity = $this->getEntity($row);
+
$form[$this->options['id']][$row_index] = array(
'#type' => 'checkbox',
// We are not able to determine a main "title" for each row, so we can
@@ -182,6 +188,7 @@ public function viewsForm(&$form, FormStateInterface $form_state) {
'#title' => $this->t('Update this item'),
'#title_display' => 'invisible',
'#default_value' => !empty($form_state->getValue($this->options['id'])[$row_index]) ? 1 : NULL,
+ '#return_value' => $this->calculateEntityBulkFormKey($entity),
);
}
@@ -264,8 +271,9 @@ public function viewsFormSubmit(&$form, FormStateInterface $form_state) {
$entities = array();
$action = $this->actions[$form_state->getValue('action')];
$count = 0;
- foreach (array_intersect_key($this->view->result, $selected) as $row) {
- $entity = $this->getEntity($row);
+
+ foreach ($selected as $bulk_form_key) {
+ $entity = $this->loadEntityFormBulkFormKey($bulk_form_key);
// Skip execution if the user did not have access.
if (!$action->getPlugin()->access($entity, $this->view->getUser())) {
@@ -344,4 +352,47 @@ protected function drupalSetMessage($message = NULL, $type = 'status', $repeat =
drupal_set_message($message, $type, $repeat);
}
+ /**
+ * @return string
+ */
+ protected function calculateEntityBulkFormKey(EntityInterface $entity) {
+ $key_parts = [$entity->language()->getId(), $entity->id()];
+
+ if ($entity instanceof RevisionableInterface) {
+ $key_parts[] = $entity->getRevisionId();
+ }
+
+ return implode('-', $key_parts);
+ }
+
+ /**
+ * @param string $bulk_form_key
+ */
+ protected function loadEntityFormBulkFormKey($bulk_form_key) {
+ $key_parts = explode('-', $bulk_form_key);
+ $vid = NULL;
+
+ // If there are 3 items, vid will be last.
+ if (count($key_parts) === 3) {
+ $vid = array_pop($key_parts);
+ }
+
+ // The first two items will always be langcode and ID.
+ $id = array_pop($key_parts);
+ $langcode = array_pop($key_parts);
+
+ if ($vid) {
+ $entity = \Drupal::entityManager()->getStorage($this->getEntityType())->loadRevision($vid);
+ }
+ else {
+ $entity = \Drupal::entityManager()->getStorage($this->getEntityType())->load($id);
+ }
+
+ if ($entity instanceof TranslatableInterface) {
+ $entity = $entity->getTranslation($langcode);
+ }
+
+ return $entity;
+ }
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment