0

I created a flag to flag the stores. I want when a user updates a store, the store is automatically flagged. The code below does not work and there is no error message.

I don't want to use the Rules module.

How do I flag the update store automatically?

use Drupal\flag\FlaggingInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\user\Entity\User;
use Drupal\commerce_store\Entity\Store;
use Drupal\commerce_product\Entity\Product;
use Drupal\group\Entity\Group;
use Drupal\node\Entity\Node;
use Drupal\comment\Entity\Comment;

/**
 * Flag store.
 */
function admin_task_notify_commerce_store_update(Store $commerce_store) {

    $flag_id = 'report_store';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $flag_service->flag($flag, $commerce_store);
  
}
  • Your codes matches the example from the FlagServiceInterface so that should work. Did you test if the function admin_task_notify_entity_update() is being called? – Neograph734 Sep 16 at 19:53
  • @Neograph734 I updated the code for the question. What exactly should I put on ? – sdfff Sep 16 at 20:45
  • If you get no error and no result, are you sure that the function (I suppose hook_entity_update for custom module admin_tasks_notify) is called? Have you tested it with some other code to see if that works? Did you clear the cache? It might be unrelated to flag. (Besides a lot of those use statements are not needed for this code) – Neograph734 Sep 16 at 20:51
  • @Neograph734 I don't know, because I'm new to the custom module. My module is here: git.drupalcode.org/sandbox/zenimagine-3170711/-/tree/main There are only 2 files and I just want to flag the created or updated entity. – sdfff Sep 16 at 21:22
  • 1
    Does this answer your question? How do I flag new user accounts or updated accounts? – Neograph734 Sep 19 at 8:02
1

The code looks ok, and you confirm it works for nodes. Then I can only guess that the used flag_ids are wrong, the flag is not enabled for the given entity, or the user does not have permission to place the flag .

| improve this answer | |
  • Everything is OK, the machine names are correct and the permissions too. The problem comes from my module. Only the nodes work. If I create a group, the user is flagged, why? git.drupalcode.org/sandbox/zenimagine-3170711/-/blob/main/… – sdfff Sep 17 at 17:47
  • It works for updating stores, but it doesn't work for creating stores. I want to be flagged automatically for creating and updating stores. – sdfff Sep 19 at 14:21
  • That was never part of the question. Please do not use comments for follow up questions. But you might try hook_entity_insert(). – Neograph734 Sep 19 at 15:07
  • I have the same error as for the comments ` Message Error : Call to undefined method Drupal\commerce_store\Entity\Store::getType() dans admin_task_notify_commerce_store_insert() (/var/www/www-example-com/web/modules/custom/admin_task_notify/admin_task_notify.module ligne 33)` Here is my code pastebin.com/gqPAHzKA – sdfff Sep 19 at 20:50
  • 1
    You'll need to learn some basic debugging. Apparently stores do not have the getType() method, so you'll need something else. Dig into commerce store/src/entity/Store.php and see what you can use. – Neograph734 Sep 19 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.