Commit 249b21b0 authored by Tim Hayward's avatar Tim Hayward
Browse files

Issue #3261676: Error on search page after deleting groups.

parent 210846fa
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
<?php

/**
 * Re-Assign orphaned GroupContent entities.
 */
function ekan_core_update_9001(&$sandbox) {

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $storage_handler = $entity_type_manager->getStorage('group_content');

  // Set up the batch by retrieving all of the group content IDs.
  if (!isset($sandbox['progress'])) {
    $sandbox['ids'] = $storage_handler->getQuery()->accessCheck(FALSE)->execute();
    $sandbox['max'] = count($sandbox['ids']);
    $sandbox['progress'] = 0;
    $sandbox['reassigned'] = 0;
    $dummy_group = \Drupal\group\Entity\Group::create(['type' => 'publisher', 'label' => "[[dummy group]]", 'status' => 0]);
    $dummy_group->save();
    $sandbox['dummy_group'] = $dummy_group;
  }

  // Try to update 25 GroupContent entities at a time.
  $ids = array_slice($sandbox['ids'], $sandbox['progress'], 25);
  $dummy_group = $sandbox['dummy_group'];

  /** @var \Drupal\group\Entity\GroupContentInterface $group_content */
  foreach ($storage_handler->loadMultiple($ids) as $group_content) {
    // Delete any group content linked to a deleted group.
    if (!$group_content->getGroup()) {
      $group_content->set('gid', $dummy_group);
      $group_content->save();
      $sandbox['reassigned']++;
    }
    $sandbox['progress']++;
  }

  // Try to update the percentage but avoid division by zero.
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);

  // Show a status update for the current progress.
  return t("Re-assingned @reassigned orphaned GroupContent entities while checking @progress of @max.", [
    '@progress' => $sandbox['progress'],
    '@reassigned' => $sandbox['reassigned'],
    '@max' => $sandbox['max'],
  ]);
}

/**
 * Delete temporary group.
 */
function ekan_core_update_9002(&$sandbox) {
  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $storage_handler = $entity_type_manager->getStorage('group');
  foreach ($storage_handler->loadByProperties(['label' => '[[dummy group]]']) as $group) {
    $group->delete();
  }
}
+22 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
 */

use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\ekan_core\Entity\EkanResourceEntity;
use Drupal\facets\FacetInterface;
@@ -13,6 +14,7 @@ use Drupal\group\Entity\GroupContentInterface;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\ekan_core\Entity\EkanDatasetEntity;
use Drupal\group\Entity\GroupInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
@@ -271,3 +273,23 @@ function ekan_core_query_ekan_grouped_distinct_alter(SelectInterface $query) {
function ekan_core_toolbar_alter(&$items) {
  $items['administration']['#attached']['library'][] = 'ekan_core/menuicons';
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function ekan_core_group_delete(EntityInterface $entity) {
  assert($entity instanceof GroupInterface);
  // We need to delete group contents when deleting groups.
  // The groups module does do this, but it does it in Group::preDelete which
  // then deletes group content, but also re-saves the content entities. This
  // means that EkanEntityGroupSynchronised::groupSynchronise re-creates group
  // relationships based on the publishers which haven't /quite/ been deleted
  // yet.
  //
  // See GroupContent::postDelete() and  https://www.drupal.org/node/2754399
  //
  // So we re-delete the group content entities in this delete hook;
  foreach ($entity->getContent() as $group_content) {
    $group_content->delete();
  }
}
+2 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\group\Entity\Group;

/**
 * Trait applies to resources and datasets to enable group synchronisation.
@@ -62,7 +63,7 @@ trait EkanEntityGroupSynchronised {
    $current_memberships = $group_content_storage->loadByEntity($this);
    foreach ($current_memberships as $membership) {
      $group = $membership->getGroup();
      if (!in_array($group->id(), $add_group_ids)) {
      if (!$group || !in_array($group->id(), $add_group_ids)) {
        try {
          $membership->delete();
        }