Error when checking whether group is a tree root

Created on 26 October 2021, about 3 years ago
Updated 26 July 2023, over 1 year ago

Problem/Motivation

When programmatically creating a new subgroup a check is first made to determine whether the parent group is a tree root using

if (!$subgroup_handler->isRoot($project))

This worked fine under Drupal core 8.9.19 but after an upgrade to 9.2.7, the same code resulted in an error:

Trying to create a group leaf but the "subgroup_depth" value is missing

The replacement test

if ($project->get('subgroup_tree')->isEmpty())

is working fine however.

Steps to reproduce

I have a two level group hierarchy, with groups of type
"project" as parents, and groups of type
"subproject" as children.

Project groups are created as normal, and sometimes a subproject group is created depending on the requirement of the project manager.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇳🇿New Zealand jlscott

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪Germany kmetz

    I've encountered this issue when I called isRoot() on a not-yet saved subgroup (during insert).

    I think that isRoot() should catch MalformedLeafException and return FALSE, same as isLeaf() does when it calls wrapLeaf().

    Both methods for reference:

      /**
       * {@inheritdoc}
       */
      public function isLeaf(EntityInterface $entity) {
        $this->verify($entity);
    
        try {
          $this->wrapLeaf($entity);
        }
        catch (MalformedLeafException $e) {
          return FALSE;
        }
    
        return TRUE;
      }
    
      /**
       * {@inheritdoc}
       */
      public function isRoot(EntityInterface $entity) {
        $this->verify($entity);
        return $this->wrapLeaf($entity)->getDepth() === 0;
      }
    
  • 🇩🇪Germany kmetz

    Also when calling getParent() on an unsaved subgroup - getParent() does call wrapLeaf() indirectly via isRoot(), and directly afterwards, in which case a MalformedLeafException should maybe be catched as well?

    Unsure, since the unsaved subgroup technically does (or a bit later will) have a parent, but all of subgroup_depth / subgroup_left / subgroup_right / subgroup_tree are empty at the point of checking for a parent group.

    I'm now use a workaround, by catching InvalidLeafException | MalformedLeafException when I check for parents of (potenitally) unsaved (isNew() === true) groups, which I know can be ignored in my special case.

Production build 0.71.5 2024