I have a content type with a field that lets the users decide which group to add the content to.

The content is added to the group via hook_node_insert().

function tm_wiki_node_insert(NodeInterface $node) {
  $type = $node->getType();
  if ($type == 'mycontent') {
    $group = Group::load('1');
    $group->addContent($node, 'group_node:mycontent');

It works: I can see the node is properly added to the group. However, users who aren't members of the group can still view the content, even though group permissions would not allow this.

When I manually edit and save the node once, the permissions are correctly set; users who aren't group members cannot view the node.

What code should I add to make the node only viewable from group members?

I don't understand the problem, since hook_node_insert() should be invoked after the entity is saved, and $group->addContent() should not require an additional save to set permissions properly.

1 Answer 1


I found the problem - it probably has to do with social_groups submodule, which adds a field for content visibility (field_content_visibility) - this field is not set properly when just adding the content via group->add content.

Your Answer

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

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