1

I'm using the 8.x-4.0-beta3 release of the Flag module in a custom D9 module to flag a newly programmatically created node using a flag I created in the UI.

$flag = flag_get_flag('new_call');
$flag->flag('flag', $call_node->nid);
$call_node->save();

But this results in this error when it is called:

Error: Call to undefined function Drupal\zoom_call\flag_get_flag() in Drupal\zoom_call\ZoomApiWebhookEventSubscriber->clientcall() (line 75 of /var/www/html/modules/zoom_call/src/ZoomApiWebhookEventSubscriber.php)

Does someone know a solution to this? Is flag_get_flag() deprecated in D9 perhaps?

2
  • Try \flag_get_flag
    – mona lisa
    Commented Jul 27, 2022 at 23:57
  • @cilefen Gives the same undefined function error.
    – Riley Lutz
    Commented Jul 28, 2022 at 0:20

1 Answer 1

2

You need to use the flag service:

$flag_id = 'bookmark';

$flag_service = \Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);

// Flag an entity with a specific flag.
$flag_service->flag($flag, $entity);

// Unflag an entity with a specific flag.
$flag_service->unflag($flag, $entity);

Documentation for Flag 4.x is lacking but I made a list of the basics.

1
  • That works, thank you, for both the solution and list.
    – Riley Lutz
    Commented Jul 28, 2022 at 0:36

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.