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.

2 Answers 2


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.


This was annoying because you can't use feeds once they're not added correctly.

However, I managed to do it by setting a default value of "community" for the group, exporting the configuration, changing the value from the community into "group," doing config import, and tada! You are welcome guys! :)

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.