0

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?

6
  • See the Node docs. 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.
    – sonfd
    Dec 6, 2021 at 0:06
  • Will this link help? drupal.stackexchange.com/questions/185442/…
    – sea26.2
    Dec 6, 2021 at 0:37
  • 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.
    – aharown07
    Dec 6, 2021 at 15:27
  • I have updated the question in light of my latest tests.
    – aharown07
    Dec 7, 2021 at 18:49
  • Have you configured the entity? git.drupalcode.org/project/flag/-/tree/8.x-4.x#configuration. If not, then you may be misdiagnosing the issue, try running update.php.
    – Jaypan
    Dec 7, 2021 at 18:57

1 Answer 1

0

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.

2
  • 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)."
    – aharown07
    Dec 7, 2021 at 23:04
  • 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.)
    – aharown07
    Dec 7, 2021 at 23:09

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.