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.