-1

I created a code to automatically flag nodes and comments. When I put only the code to flag the nodes, it works. But when I add the code to flag the comments it doesn't work anymore.

Which code should I use ?

<?php

function admin_task_notify_entity_update(Drupal\Core\Entity\EntityInterface $entity) {

  // Flag node.
  if (in_array($entity->bundle(), ['annonce', 'article', 'demande', 'discussion', 'evenement', 'itineraire', 'recette'])) {
    $flag_id = 'report_node';

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

    $flag_service->flag($flag, $entity);
  }

  // Flag comment.
  if (in_array($entity->bundle(), ['annonce', 'article', 'demande', 'discussion', 'evenement', 'itineraire', 'recette', 'produit', 'service'])) {
    $flag_id = 'report_comment';

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

    $flag_service->flag($flag, $entity);
  }

}
  • You check for $entity->bundle(), but remember that $entity can be a node or a comment. The part for the comment will not work because I am quite sure that when your second if statement is true YOU expect $entity to be a comment but it is a node - just nodes have bundles, comments do not have these configurable bundles. Your flag with id 'report_comment' will surely expect the entity to be a comment. – Tobias Krause Sep 15 at 16:39
  • Thank you for the explanations. What should be the code to correct the problem? Thank you – sdfff Sep 15 at 16:49
  • 1
    Does this answer your question? How do I flag new user accounts or updated accounts? – Neograph734 Sep 19 at 8:25
  • @Neograph734 The questions are on different entity for which I am blocked – sdfff Sep 19 at 12:16
  • I guess you have to check $entity->getEntityTypeId() instead of bundle. This code will return the entity type (node, comment). – sanzante 2 days ago
1

Change the code in the following way so that it works as you expect:

  // Flag comment.
  if ($entity->getEntityTypeId() == 'comment') {
    $flag_id = 'report_comment';

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

    $flag_service->flag($flag, $entity);
  }
| improve this answer | |
  • Thanks for your reply, but your code is going to flag any kind of comment I think. How can I specify the types of comments to be flagged? For example if I have the machine name "page", "article", "reciepe". I only want to flag the "page" and "reciepe" comment type. – sdfff Sep 15 at 17:06
  • I tested your code but it does not flag the comment – sdfff Sep 15 at 22:23
  • You need to get the commented node (I do not know how the comment entity brings that to you) and check against the bundle of the commented node - not the comment itself. When the code does not flag your comment there might be some other issue. Is the flag id correct? Is the flag report_comment correctly configured? – Tobias Krause Sep 16 at 7:11
  • Yes the flag is correct, but it doesn't work. I just tested with stores (Commerce module) and it doesn't work. pastebin.com/ieRMuH2Y – sdfff Sep 16 at 10:11
  • Any logs? Any error messages? You write you tried it now with stores so you use another code as you wrote here?! As the same code works with nodes I think that there is something else mis-configured but it may not be the code posted here – Tobias Krause Sep 17 at 8:09

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.