Hi, sorry if this is a noob question... but having gone through the documentation and other issues i cant seem to find an working example for d8 and flags.

I am trying to unflag all current users nodes when they submit a webform. I have my form alter working on the webform submit, but subsequently i cant seem to unflag the current users flags. Anon user may also flag nodes, so this example below doesnt do the trick for me in that context: Having looked at the documentation, most of it is for d7.... unless im missing something?

$flag_service = \Drupal::service('flag');
$flag = $flag_service->getFlagById('flag_machine_name');
// $entity can be $node or user, $account is the acting user.
$flag_service->unflag($flag, $entity, $account);

Any help would be greatly appreciated.

Comments

Ddroid_za created an issue. See original summary.

socketwench’s picture

There's a few more methods on FlagService that might help:

<?php
  /**
   * Remove all flaggings from a flag.
   *
   * @param \Drupal\Flag\FlagInterface $flag
   *   The flag object.
   */
  public function unflagAllByFlag(FlagInterface $flag);

  /**
   * Remove all flaggings from an entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity object.
   */
  public function unflagAllByEntity(EntityInterface $entity);

  /**
   * Remove all of a user's flaggings.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The user object.
   * @param string $session_id
   *   (optional) The session ID. This must be specified if $account is the
   *   anonymous user.
   *
   * @throws \LogicException
   *   Thrown when $account is anonymous but no associated session ID is
   *   specified.
   */
  public function unflagAllByUser(AccountInterface $account, $session_id = NULL);

?>

The documentation is on the interface, FlagServiceInterface, rather than the FlagService class itself. Does that help?

ptmkenny’s picture

Status: Active » Closed (outdated)

Closing because #2 gives some hints and there hasn't been a follow-up for 3 years.