-1

I created a custom module to automatically flag new entities and update entities. It works :-)

Here is the problem :

If I update a neud it is flagged. Perfect that's what I want to do, but if I update the node again, I get a PHP error. This is normal because the custom module tries to flag an already flagged node.

I have the same problem for all entities.

How to fix this ? I am completely stuck with this problem.

Here are the error messages in the logs :

Type    php
Date    Dimanche, 20 septembre, 2020 - 17:43
Utilisateur Mathieu
Emplacement https://dev.exemple.com/fr/node/360/edit
Référent    https://dev.exemple.com/
Message Drupal\Core\Entity\EntityStorageException : The user has already flagged the entity with the flag. dans Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (ligne 846 de /var/www/www-exemple-com/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Severity    Erreur

and

Type    nœud
Date    Dimanche, 20 septembre, 2020 - 17:43
Utilisateur Mathieu
Emplacement https://dev.example.com/fr/node/360/edit
Référent    https://dev.example.com/
Message LogicException : The user has already flagged the entity with the flag. dans Drupal\flag\FlagService->flag() (ligne 253 de /var/www/www-example-com/web/modules/contrib/flag/src/FlagService.php).
Severity    Erreur

Here is the code for my custom module :

<?php

/**
 * @file
 * Holds hook implementation for the Admin Task Notify module.
 */

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;

/**
 * Implements hook_user_insert() and hook_user_update().
 */
function admin_task_notify_user_insert(User $account) {
  $flag_id = 'moderate_user';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $user_admin = \Drupal\user\Entity\User::load(1);
  $flag_service->flag($flag, $account, $user_admin);
}

function admin_task_notify_user_update(User $account) {
  $flag_id = 'moderate_user';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $user_admin = \Drupal\user\Entity\User::load(1);
  $flag_service->flag($flag, $account, $user_admin);
}

/**
 * Implements hook_store_insert() and hook_store_update().
 */
function admin_task_notify_commerce_store_insert(Store $commerce_store) {
  if ($commerce_store->bundle() == 'ephemere' || $commerce_store->bundle() == 'personnel' || $commerce_store->bundle() == 'professionnel') {
    $flag_id = 'moderate_store';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $commerce_store, $user_admin);
  }
}

function admin_task_notify_commerce_store_update(Store $commerce_store) {
  if ($commerce_store->bundle() == 'ephemere' || $commerce_store->bundle() == 'personnel' || $commerce_store->bundle() == 'professionnel') {
    $flag_id = 'moderate_store';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $commerce_store, $user_admin);
  }
}

/**
 * Implements hook_product_insert() and hook_product_update().
 */
function admin_task_notify_commerce_product_insert(Product $commerce_product) {
  if ($commerce_product->bundle() == 'produit' || $commerce_product->bundle() == 'service') {
    $flag_id = 'moderate_product';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $commerce_product, $user_admin);
  }
}

function admin_task_notify_commerce_product_update(Product $commerce_product) {
  if ($commerce_product->bundle() == 'produit' || $commerce_product->bundle() == 'service') {
    $flag_id = 'moderate_product';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $commerce_product, $user_admin);
  }
}

/**
 * Implements hook_group_insert() and hook_group_update().
 */
function admin_task_notify_group_insert(Group $group) {
  if ($group->bundle() == 'personnel' || $group->bundle() == 'professionnel') {
    $flag_id = 'moderate_group';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $group, $user_admin);
  }
}

function admin_task_notify_group_update(Group $group) {
  if ($group->bundle() == 'personnel' || $group->bundle() == 'professionnel') {
    $flag_id = 'moderate_group';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $group, $user_admin);
  }
}

/**
 * Implements hook_node_insert() and hook_node_update().
 */
function admin_task_notify_node_update(Node $node) {
  if ($node->bundle() == 'annonce' || $node->bundle() == 'article' || $node->bundle() == 'demande' || $node->bundle() == 'discussion' || $node->bundle() == 'evenement' || $node->bundle() == 'itineraire' || $node->bundle() == 'recipe') {
    $flag_id = 'moderate_node';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $node, $user_admin);
  }
}

/**
 * Implements hook_comment_insert() and hook_comment_update().
 */
function admin_task_notify_comment_insert(Comment $comment) {
  if ($comment->bundle() == 'annonce' || $comment->bundle() == 'article' || $comment->bundle() == 'demande' || $comment->bundle() == 'discussion' || $comment->bundle() == 'evenement' || $comment->bundle() == 'itineraire' || $comment->bundle() == 'recipe' || $comment->bundle() == 'produit' || $comment->bundle() == 'service') {
    $flag_id = 'moderate_comment';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $comment, $user_admin);
  }
}

function admin_task_notify_comment_update(Comment $comment) {
  if ($comment->bundle() == 'annonce' || $comment->bundle() == 'article' || $comment->bundle() == 'demande' || $comment->bundle() == 'discussion' || $comment->bundle() == 'evenement' || $comment->bundle() == 'itineraire' || $comment->bundle() == 'recipe' || $comment->bundle() == 'produit' || $comment->bundle() == 'service') {
    $flag_id = 'moderate_comment';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $user_admin = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $comment, $user_admin);
  }
}
2
$flag_status = $flag_service->getFlagging($flag, $comment, $user_admin); // get flag status
if (!$flag_status) { // if not TRUE, is FALSE, not flagged
  $flag_service->flag($flag, $comment, $user_admin);
}
1
  • Comments are not for extended discussion; this conversation has been moved to chat. – Clive Sep 21 '20 at 6:35

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.