0

The group module is an alternative to the og module. The og module allows you to assign permissions to fields per group. I would like to be able to do the same thing with the group module. It appears that in order to do this, I need to add a field or fields as an entity in the Group module. All my searches seem to indicate that the GroupContentEnabler plugin will enable me to do this. But I can't find any clear example of how. Can someone point me to some documentation on how to go about coding this?

enter image description here

Follow up:

It appears that hook_entity_field_access() will allow me to do what I wish to do. I found an example of it's use in the group module:

This is the code used in group.module (not gnode.module):

/**
 * Implements hook_entity_field_access().
 *
 * {@inheritdoc}
 *
 * @todo Move this to a form controller so we can hide the field if it has no
 *       options available to it?
 */
function group_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  // Can't retrieve an entity from an empty item list.
  if (!isset($items)) {
    return AccessResult::neutral();
  }

  // We only care about the group_roles field when on a form.
  if ($field_definition->getName() != 'group_roles' || $operation !== 'edit') {
    return AccessResult::neutral();
  }

  **// We only care if it is attached to a group content entity.
  if ($items->getEntity()->getEntityTypeId() != 'group_content') {
    return AccessResult::neutral();
  }**

  /** @var \Drupal\group\Entity\GroupContentInterface $group_content */
  $group_content = $items->getEntity();

  // We only care if the group content entity is a group membership.
  if ($group_content->getContentPlugin()->getPluginId() != 'group_membership') {
    return AccessResult::neutral();
  }

  // Now that we know we're dealing with a group_roles field on a group
  // membership form, we need to check whether the group membership belongs to a
  // group yet. If not, we can't check for access and should always hide it.
  if (!$group = $group_content->getGroup()) {
    return AccessResult::forbidden();
  }

  // Only group administrators should be able to change the membership roles.
  return AccessResult::forbiddenIf(!$group->hasPermission('administer members', $account));
}

It appears that all I have to do is create a group permission, then check access to the field in question against that permission using hook_entity_field_access().

My only question is with this code:

// We only care if the group content entity is a group membership.
  if ($group_content->getContentPlugin()->getPluginId() != 'group_membership') {
    return AccessResult::neutral();
  }

Let's say I create a custom field called "my_test_field". And, I create a group permission called "access test field". I then change above code:

// We only care about the the my_test_field field.
          if ($field_definition->getName() != 'my_test_field'
...
...
// Only users with 'access test field' should be able to see my_test_field.
  return AccessResult::forbiddenIf(!$group->hasPermission('access test field, $account));

If this field is attached to a node that is in a group, I am assuming that it is now attached to the group node entity. But what about this?:

 if ($group_content->getContentPlugin()->getPluginId() != 'group_membership') {

Is the field also now automatically a group membership? I guess I don't understand what that means in this context.

0

2 Answers 2

0

I am not sure how you would approach this by adding the fields as entities via the GroupContentEnabler (that'd mean one custom plugin per field? Given a large number of fields that might not be the best solution). However, for an example how to implement your own custom group-content-type plugin see this answer.

Another possible approach that might be worth checking is to implement hook_entity_field_access() to define access checks for single fields programmatically (that's how I've solved a similar problem). See the group module's gnode.module file for an example how to check permissions/roles for users and add cache-tags to access results.

1
  • Thank you. I have modified my question to try and understand how to use hook_entity_field_access() to restrict access to a field attached to a group node. Commented May 28, 2019 at 2:57
0

The solution to this problem is to load the group content from the node to which the field entity is attached.

// This field is a node entity $node = $items->getEntity(); $type = $node->bundle(); $group_content_types = GroupContentType::loadByContentPluginId("group_node:$type"); $group_content = reset($group_contents);

This same logic also works for paragraph fields, with this additional code:

// This field is a paragraph entity $onode = $items->getEntity(); $otype = $onode->bundle(); // original type (paragraph) // Get the parent entity of the paragraph, which is a node $node = $onode->getParentEntity(); $type = $node->bundle(); // new type (node) $group_content_types = GroupContentType::loadByContentPluginId("group_node:$type"); $group_content = reset($group_contents);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.