Add two checkboxes to plugin config:

  • Delete the entity along with the group-entity relation (GroupContent)
  • Only delete when the entity is not part of any other group

Then implement those.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kristiaanvandeneynde created an issue. See original summary.

imclean’s picture

Just found this which I think relates to a problem I'm having: #2854068: Relation not deleted when node is deleted

From an entity/node object, is it possible to determine if the entity is part of a group?

kristiaanvandeneynde’s picture

Not related, this issue is about the option to also delete the entity the relation represents when you delete the relationship.

markconroy’s picture

Hi imclean,

Here's the code we use to check if a node is part of a group, and if so to redirect to the group alias version of the node:

<?php

namespace Drupal\trinity_group_node_redirect\EventSubscriber;

use Drupal\group\Entity\GroupContent;
use Drupal\node\Entity\Node;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Path\CurrentPathStack;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Drupal\group\Entity\GroupContentInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Class RequestSubscriber.
 *
 * @package Drupal\trinity_group_node_redirect
 */
class RequestSubscriber implements EventSubscriberInterface {

  /**
   * Drupal\Core\Path\CurrentPathStack definition.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $pathCurrent;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Path\CurrentPathStack $path_current
   *   Current path object.
   */
  public function __construct(CurrentPathStack $path_current) {
    $this->pathCurrent = $path_current;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events['kernel.request'] = ['kernelRequest'];

    return $events;
  }

  /**
   * This method is called whenever the kernel.request event is dispatched.
   *
   * @param GetResponseEvent $event
   *   The requested event.
   */
  public function kernelRequest(GetResponseEvent $event) {
    $canonical_path = \Drupal::service('path.current')->getPath();
    $canonical_path_args = array_values(array_filter(explode('/', $canonical_path)));

    if (count($canonical_path_args) == 2 && $canonical_path_args[0] == 'node' && is_numeric($canonical_path_args[1])) {
      $node = Node::load($canonical_path_args[1]);
      if (!empty($node)) {
        /** @var GroupContentInterface $content_group */
        foreach (GroupContent::loadByEntity($node) as $content_group) {
          $source_path = $content_group->toUrl()->toString();
          $response = new RedirectResponse($source_path);
          $response->send();
        }
      }
    }
  }

}

Hopefully that can be some help to you.

sandeepguntaka’s picture

I ve done some code for this.

sandeepguntaka’s picture

Status: Active » Needs review
sandeepguntaka’s picture

mvogel’s picture

Patch #7 works for me.

Maybe it would be nice too if the user could decide what should happen with the related content after the group deletion. (keep or delete) ?

tomasbarej’s picture

I had to update the patch with the current 8.x-1.x state.

Status: Needs review » Needs work

The last submitted patch, 9: plugin_config_delete-2754399-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

moshe weitzman’s picture

I just ran into this issue. My use case is in an automated test using Drupal Test Traits https://gitlab.com/weitzman/drupal-test-traits. The test automatically cleans up created entities during tearDown(), in the order that they were created. In my test, a user was being deleted before its content and that led to GroupContent deletions which led to re-saving of the content (e.g. a node-page). The re-save failed with Drupal\Core\Entity\EntityStorageException: Update existing 'node' entity while changing the ID is not supported.

I also saw this happenning when users are deleted, as they own nodes that get deleted which leads to GroupContent getting deleted and so it goes.

sergiuteaca’s picture

Status: Needs work » Needs review
FileSize
3.63 KB

A re-roll of patch #9 to meet the current state of the dev branch

@moshe weitzman I couldn't reproduce the error you are receiving.

matslats’s picture

I just wanted to say that I'm having trouble with GroupContent::postDelete during migrate-rollback.
Here's my error message:
Drupal\Core\Entity\EntityStorageException: Update existing 'user' entity while changing the ID is not supported.

Boobaa’s picture

Component: Group (group) » Code
FileSize
3.46 KB

My use case is similar to #11 but not exactly the same: an automated Behat test automatically cleans up created users during tearDown(). In my test, a user was being deleted before its membership and that led to GroupContent deletions which led to re-saving of the user. The re-save failed with Notice: Trying to get property 'pass' of non-object in web/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php line 50 (Drupal\Core\Entity\EntityStorageException). In my case, the user-to-be deleted was not the owner of any group, but only a member of one. I could not reproduce the problem using Drupal's web UI either.

Re-rolled #12 anyway to meet the current state of the 8.x-1.x branch so it's also applicable to 8.x-1.4 (the latest stable release).

msnassar’s picture

FileSize
3.39 KB

Here is a patch for group 2 and 3...

msnassar’s picture

Version: 8.x-1.x-dev » 3.1.x-dev
msnassar’s picture

The patch in #15 is working for our website as we do not allow sharing content between groups.

Anyway, the proposed solution, is good when:

  • Content is not shared at all
  • Content is shared between same group type
  • Their is no conflict in the delete settings -for the same relation plugin- in the multiple group types

But, I wonder what should happen in case an entity is shared between 2 groups of different group type; where the relation plugins in each group type has different settings for entity deletion. For example:

  • Having group type A and group type B
  • Enable the relation plugin for Page content type in both A and B group types
  • In group type A: The configs for the plugin is > "Delete target entity when group relation is deleted" (So we always want to delete - No matter if the entity is being shared or not.)
  • In group type B: We keep the default > Do not delete the entity (The option do delete the target entity is not selected).
  • Create 2 groups: Group X of type A and Group Y of type B
  • Create a page node in group X and share it with group Y
  • 1. If the user deleted the relation in group X, the node will be deleted
  • 2. If the user deleted the relation in group Y, the node won't be deleted

Case #1:
It is deleting an entity that is being shared with other group (of different type) that assumes an entity should not be deleted when deleting its relation.

I think we should have a third option e.g. "Only delete when all same relation plugins in other group type allow to delete".
When this option is selected, Case #1 won't delete the target entity as it is being shared with a group of another type that prevents deleting the target entities!