How to implements hook_flag_flag() / hook_flag_unflag in flag 8.x-4.X ?
I read the documentation but can not found it.

Thanks in advance.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

anjjriit created an issue. See original summary.

martin107’s picture

Hi anjjriit

Can you add an outline of what task you want to perform?

The answer to your question will involve calling FlagServiceInterface::flag()/unflag()

In some way, but that is only a small piece of the puzzle

I will try and help out where possible.

An example for something that calls FlugService Interface::flag() see ActionLinkController::flag()

anjjriit’s picture

@martin107 Thanks a lot for quick reply

a simple action what i want to perform when a node was flagged is send an email to specific person, not as node author or current user

socketwench’s picture

Both hook_flag_flag() and hook_flag_unflag() were replaced with an event: #2409859: [Policy, No Patch] Where did the Flag 7.x hooks go in 8.x?

martin107’s picture

Please ignore #2, Socketwench is correct ... as usual

Can I ask a few questions!

(A) Are we speaking about one node and there will be many types of flags -- each flag causing an email to be sent

OR

(B) Are we talking about one special flag with the property that an email will be sent

I think my advice changes depending on A or B

The solution might involve creating a new service similar to FlagCountManager

What is key is the EventSubscriberInterface part

and the getSubscribedEvents method which binds methods which will trigger the email generation.

Anyway I hope this is food for thought.

anjjriit’s picture

In this issue we are speaking about special flag, or option (B).
I have flag named needs_review and when its flagged I want to send an email
but i do not want to send email when bookmark is flagged.

martin107’s picture

Issue tags: +Needs tests
FileSize
4.68 KB

For some time I have wanted a example sub-module that makes use of the event system.

it is better to discuss performance issues publically to ensure the pattern we think is good is actually good.

So I have included a new submodule called flag_email

It is an unfinished example

if you install it for example with drush en flag_email

Then on every article you will see a new "trigger email" flag.

\Drupal\contact\MailerHandler has a good example of how to generate emails

No email is currently sent - at the moment is just adds a log message every time the link is clicked.

The new Mail handler class needs setup methods to define the email address to send a message etc.

If you think this will answer your problem, then I will continue.

@anjjriit can you look at MailManager::generateEmail()

Can you supply/describe a test condition that identifies your special flag.

I would like to fit the example code to match a real world example.

anjjriit’s picture

@martin107 a tons of Thanks !

This sample sub module is work fine for me.

  1. This file sucssessfully installed without error message as in image 01.
  2. I saw trigger email flag in my content type article
  3. I just add 2 lines of code in MailManager::generateEmail()
    <?php
    $message = t('Content %title just transitioned to %flag by %username, Please review', array('%title' => $entity->Link(), '%flag' => $flag->label(), '%username' => $flagging->getOwner()->getUsername()));
    $this->logger->notice($message);
    ?>

    and my log is in image 02

  4. Detail of this log message is in image 03
anjjriit’s picture

Forgot about IF Condition this my continued test.

i put logger command in two diffrerent place

<?php
$this->logger->notice('The flag event was fired.');

// Performance issue:  Return early if this is not the flag we are looking for.
$flagging = $event->getFlagging();

$flag = $flagging->getFlag();
if ($flag->id() != 'needs_review' /* Add test condition here. */) {
  return;
}

// Note entity can also be extracted.
$entity = $flagging->getFlaggable();

$message = t('Content %title just transitioned to %flag by %username, Please review', array('%title' => $entity->Link(), '%flag' => $flag->label(), '%username' => $flagging->getOwner()->getUsername()));
$this->logger->notice($message);
?>
  1. Before add condition if i flagged bookmark flag, two log messages was fired as in image 04
  2. After add condition if i flagged bookmark flag, only log message was fired as in image 05
ptmkenny’s picture

Status: Active » Closed (works as designed)

Closing this issue because the original question was answered in #4 and four years have passed with no updates.