Toggling Group Functionality On/Off Causes "Entity Already Exists" Error
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3554916. --> Reported by: [loze](https://www.drupal.org/user/116906) Related to !56 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>When toggling group functionality off and then back on for a content type, the system throws an <code>EntityStorageException</code> claiming that OG roles already exist. Even though the group functionality is successfully re-enabled, the error prevents the save operation from completing cleanly and displays an error message to the user.</p> <h4 id="summary-steps-reproduce">Steps to reproduce</h4> <ol> <li>Enable group functionality on a content type</li> <li>Save the content type</li> <li>Toggle group functionality OFF for the same content type</li> <li>Save the content type</li> <li>Toggle group functionality back ON</li> <li>Save the content type</li> </ol> <p><strong>Expected Result:</strong> Group functionality is successfully re-enabled with a success message.<br> <strong>Actual Result:</strong> Group functionality is re-enabled, but an exception error is thrown:</p> <pre>Drupal\Core\Entity\EntityStorageException: 'og_role' entity with ID 'node-group-non-member' already exists.</pre><h3>Root Cause</h3> <p>When group functionality is disabled via <code>GroupTypeManager-&gt;removeGroup()</code>, the bundle-specific OG roles (member, non-member, administrator) are not deleted. They remain in the system.</p> <p>When group functionality is re-enabled via <code>GroupTypeManager-&gt;addGroup()</code>, the system calls <code>OgRoleManager-&gt;createPerBundleRoles()</code> which attempts to create these roles again without checking if they already exist, resulting in the EntityStorageException due to the duplicate ID.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>1. Check if the roles exist before creating in createPerBundleRoles()<br> Pros: simple fix to check if the config exists.<br> Cons: Unused config entities remain in the system when disabled.</p> <p>2. Delete the roles when group functionality is toggled off.<br> Pros: This is the same as what happens when the bundle is deleted.<br> Cons: </p> <ul> <li>Memberships would then have references to non-existent roles, and I suppose we would need to decide what to do with the orphaned memberships.</li> <li>If you are just temporarily disabling, any custom roles you created would be gone when its re-enabled.</li> </ul> <p>I think #1 is a simple fix, but I'll leave it up to the maintainers to chime in.</p>
issue