Score:0

How to use hook_node_insert to automatically flag nodes of a specific content type

pe flag

All nodes of one content type in my project need to get automatically flagged (by the Flags module) when created. I'm aware that there is probably a way to accomplish this using Rules module, but I'm attempting to build my first-ever custom module to do it, because a) I don't think I'm going to need Rules for anything else in this project and b) want to get my feet wet on making custom modules. (I've got a basic 'hello world' module working.)

A support request in the Flag module seems to provide a simple way to automatically flag nodes, but it looks to me like it would do this to nodes of all content types. https://www.drupal.org/project/flag/issues/3030288

For convenience here's the code there, for a flag called bookmark...

function your_module_node_insert(Drupal\Core\Entity\EntityInterface $entity){
 $flag = \Drupal::entityTypeManager()->getStorage('flag')->load('bookmark');
 \Drupal::service('flag')->flag($flag, $entity);
}

The api page for this hook doesn't seem to tell me what I need to know. (https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_insert/9.0.x)

Questions: Is this even the right hook for this need? If so, how might I restrict it to a specific content type?

sonfd avatar
in flag
See [the Node docs](https://api.drupal.org/api/drupal/core%21modules%21node%21src%21Entity%21Node.php/class/Node/9.2.x). You can get the the content type (aka bundle) of the node with `$entity->bundle()` which is generic and will work for all content entities.
sea26.2 avatar
om flag
Will this link help? https://drupal.stackexchange.com/questions/185442/how-can-i-programmatically-create-a-node
pe flag
These are both helpful for filling in gaps. With my close-to-zero background on working with objects, classes, methods etc., I mainly need to understand the syntax. The right way to do that is work through a course from foundation concepts up, but, yes, looking for shortcuts. Any additional code examples would be great. Some will be closer to what I'm trying to do. I can look for patterns then trial-and-error it the rest of the way. Probably.
pe flag
I have updated the question in light of my latest tests.
Jaypan avatar
de flag
Have you configured the entity? https://git.drupalcode.org/project/flag/-/tree/8.x-4.x#configuration. If not, then you may be misdiagnosing the issue, try running update.php.
pe flag
Thanks. After some more digging, I determined that my code works, so I'm moving it to the Answer. The reason I thought it was failing is that the flag involved has an entity reference field and of course this code isn't setting that field. It does set the flag though.
Score:0
pe flag

This is working for me...

'rf_tweaks' is the module machine name, 'research_note' is the node content type, and 'research_item' is the name of the flag.

use Drupal\node\Entity\Node;

function rf_tweaks_node_insert(Node $node) {
  if ($node->bundle() == 'research_note'){
 $flag = \Drupal::entityTypeManager()->getStorage('flag')->load('research_item');
 \Drupal::service('flag')->flag($flag, $node);
}
}

For the benefit of other first time module builders, this is in the rf_tweaks.module file and the file begins with the <?php required in those.

pe flag
Well, it was working but now it's not. Errors... "The flag does not apply to the bundle of the entity. in Drupal\flag\FlagService->flag() (line 294 of ...modules/contrib/flag/src/FlagService.php)" followed by "Drupal\Core\Entity\EntityStorageException: The flag does not apply to the bundle of the entity. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 810 of ...core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php)."
pe flag
Actually still works but must be sure the content type stays enabled in the config for that flag. This is because the module calls the flag api. (Or so I am led to understand.)
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.