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?
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.