Score:2

$group->addContent() in hook_node_insert() doesn't update the access permissions for the node

cn flag

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.

Score:0
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.