Problem/Motivation

After installing the module. I set up a group type with two fields. When saving it I get the "Attempt to create a field without a field_name" error (https://www.drupal.org/project/group/issues/3391452). It does create the group type. When I try and create a group, I get this error:

InvalidArgumentException: Field group_roles is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 616 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).

Drupal\Core\Entity\ContentEntityBase->get() (Line: 91)
Drupal\group\Entity\Storage\GroupRoleStorage->loadByUserAndGroup() (Line: 41)
Drupal\group\Entity\GroupMembership->getRoles() (Line: 90)
Drupal\group\GroupMembership->getRoles() (Line: 64)
Drupal\group\Access\IndividualGroupPermissionCalculator->calculatePermissions() (Line: 138)
Drupal\flexible_permissions\ChainPermissionCalculator->calculatePermissions() (Line: 41)
Drupal\group\Access\GroupPermissionCalculator->calculateFullPermissions() (Line: 40)
Drupal\group\QueryAccess\GroupQueryAlter->doAlter() (Line: 143)

I am unable to use the module as this error seems unavoidable.

Solution

This issue occurs when group relationship types are created during config sync. The group_roles field is required for all group_membership relationship types. When a new relationship type is created, GroupRelationshipType::postSave() runs post-install tasks including GroupMembershipPostInstall::installGroupRolesField() to create the bundle-specific field config. However, this method intentionally skips field creation when \Drupal::isConfigSyncing() returns TRUE (see line 43 of GroupMembershipPostInstall.php) to avoid creating config objects during import. This assumes the field configs will be included in the imported configuration, but if relationship types are created during config sync without corresponding field configs in the import, the fields are never created. The merge request adds update hook group_update_11402() that scans all existing group_membership relationship types and creates any missing group_roles field configurations.

Issue fork group-3505906

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jeffreysmattson created an issue. See original summary.

jeffmattson’s picture

Issue summary: View changes
jeffmattson’s picture

Issue summary: View changes
jeffmattson’s picture

Issue summary: View changes
kristiaanvandeneynde’s picture

Can you provide steps to reproduce on a clean install?

jeffmattson’s picture

I had installed the module using composer and then enabled this module using config. This doesn't run the install hook so the module was not installed correctly.

The solution was this:

1. Installed the module with composer.
2. In the UI or using Drush enabled the module.
3. Exported the configs.
4. Module works as expected after site creation and config import.
5. Profit.

This is something that people may not know about Drupal. I sure didn't.

jeffmattson’s picture

Status: Active » Closed (works as designed)
kristiaanvandeneynde’s picture

Thanks for updating the issue with your findings. Not everyone does that so I really appreciate it.

barbarae’s picture

3 months to figure out this issue:

Here is the solution we used:

drush ev "
use Drupal\field\Entity\FieldConfig;

FieldConfig::create([
  'field_name' => 'group_roles',
  'entity_type' => 'group_relationship',
  'bundle' => 'administrators-group_membership',
  'label' => 'Group roles',
  'required' => FALSE,
  'settings' => [],
])->save();
"

This worked!

so, replace "administrators" with your role machine name and run the script. It creates the missing group_roles . Only do this if you are actually missing the group_roles.

The need for this fix was due to a botched hook update execution along the way somewhere , or botched upgrade. We ran this script to avoid having to re-install from scratch. Now the /admin/group route loads without crashing although we did have to reload our opcache (apcu) and rebuild drupal cache also.

The groups module could ideally supply a hook_update to check for missing group_roles and add them if missing.

barbarae’s picture

Version: 3.3.3 » 3.3.4
Category: Bug report » Support request
Status: Closed (works as designed) » Needs review

Ideally the hook_update would handle sanity checks and ensure the expected group_roles exist.

vlyalko’s picture

I am having the same issue with the upgrade of the group module to 3.3.5 version.
On the first load of the site i got the error for the flexible_permissions discussed here https://www.drupal.org/project/group/issues/3484817.
Cleared cache and was able to get to the login screen. After login in, I am getting the same error from this issue. Tried to do what was recommended in #6, but i am getting warning that 'field_config' entity with ID 'group_relationship.ahrc_pl_hrc_g_3-group_membership.group_roles' already exists.'

drush updb -y -> no pending updates

Trying to clear cache multiple times. Still the same error.

ben.hamelin’s picture

Thanks @barbarae for comment in #9. I ran into this today and that helped guide me to a solution. Below is an update hook I worked on locally to help get past this. But before sharing that, I think this is at least partially related to proper configuration around group roles. This was a clean install of Groups 3.3.5, not an upgrade. This is a new build. I had created a new group type and enabled gnode to support the content type relation for managing access to that type. I then ran into this error when trying to create a new Group of that type. What I think was wrong was that I had not defined any group admin roles for this group type.

/**
 * Create group_roles field for all group membership bundles.
 */
function group_update_10306(&$sandbox) {
  $config_factory = \Drupal::configFactory();
  $entity_type_manager = \Drupal::entityTypeManager();
  $field_config_storage = $entity_type_manager->getStorage('field_config');

  // Find all group relationship types that use the group_membership plugin.
  $relationship_type_configs = $config_factory->listAll('group.relationship_type.');
  $bundles_updated = [];

  foreach ($relationship_type_configs as $config_name) {
    $relationship_config = $config_factory->get($config_name);
    $content_plugin = $relationship_config->get('content_plugin');

    // Only process group_membership relationship types.
    if ($content_plugin !== 'group_membership') {
      continue;
    }

    $bundle = $relationship_config->get('id');

    // Check if the field already exists for this bundle.
    $existing_field = $field_config_storage->load('group_relationship.' . $bundle . '.group_roles');

    if ($existing_field) {
      continue;
    }

    // Create the field.
    FieldConfig::create([
      'field_name' => 'group_roles',
      'entity_type' => 'group_relationship',
      'bundle' => $bundle,
      'label' => 'Group roles',
      'required' => FALSE,
      'settings' => [],
    ])->save();

    $bundles_updated[] = $bundle;
  }

  if (empty($bundles_updated)) {
    return t('No group membership bundles required the group_roles field.');
  }

  return t('Created group_roles field for bundles: @bundles', [
    '@bundles' => implode(', ', $bundles_updated),
  ]);
}
jeffmattson’s picture

Issue summary: View changes

jeffmattson’s picture

Issue summary: View changes
kristiaanvandeneynde’s picture

Status: Needs review » Fixed

There was a bug where a module install could trigger the creation of a group_roles field instance before the storage was there. This was fixed in #3611557: Figure out which core update broke Group2to3UpdateTest.

From the IS:

When a new relationship type is created, GroupRelationshipType::postSave() runs post-install tasks including GroupMembershipPostInstall::installGroupRolesField() to create the bundle-specific field config. However, this method intentionally skips field creation when \Drupal::isConfigSyncing() returns TRUE (see line 43 of GroupMembershipPostInstall.php) to avoid creating config objects during import. This assumes the field configs will be included in the imported configuration, but if relationship types are created during config sync without corresponding field configs in the import, the fields are never created.

That is the issue at hand, people were using incomplete exports. So while I appreciate the OP adding an MR here, this issue is a support request and will not see any code committed under that status.

It seems the answer has been provided already, so will close as fixed. I strongly suggest people who suffer from the IS run the code here once, but then remove it and reset the update hook count to what it's supposed to be.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.