Skip to main content
removed the irrelevant code, since the question is about nodes; removed the updated basing on the given answer, as questions aren't for a back-and-forth with the users who answer
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

How do I get the administrator to automatically flag  ?

<?php

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;

/**
 * Flag user.
 */
function admin_task_notify_user_update(User $account) {
  $flag_id = 'moderate_user';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $flag_service->flag($flag, $account);
}

/**
 * Flag store.
 */
function admin_task_notify_commerce_store_update(Store $commerce_store) {

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

/**
 * Flag product.
 */
function admin_task_notify_commerce_product_update(Product $commerce_product) {

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

/**
 * Flag group.
 */
function admin_task_notify_group_update(Group $group) {

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

/**
 * Flag node.
 */
function admin_task_notify_node_update(Node $node) {

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

}

/**
 * Flag comment.
 */
function admin_task_notify_comment_update(Comment $comment) {

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

}

UPDATE

I applied Clive's answer but I got a PHP error :

TypeError : Argument 3 passed to Drupal\flag\FlagService::flag() must implement interface Drupal\Core\Session\AccountInterface or be null, instance of Drupal\node\Entity\Node given, called in /var/www/www-example-com/web/modules/custom/admin_task_notify/admin_task_notify.module on line 71 dans Drupal\flag\FlagService->flag() (/var/www/www-example-com/web/modules/contrib/flag/src/FlagService.php ligne 234)

<?php

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;

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

/**
 * Flag store.
 */
function admin_task_notify_commerce_store_update(Store $commerce_store) {

    $flag_id = 'moderate_store';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $commerce_store);
  
}

/**
 * Flag product.
 */
function admin_task_notify_commerce_product_update(Product $commerce_product) {

    $flag_id = 'moderate_product';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $commerce_product);
  
}

/**
 * Flag group.
 */
function admin_task_notify_group_update(Group $group) {

    $flag_id = 'moderate_group';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $group);
  
}

/**
 * Flag node.
 */
function admin_task_notify_node_update(Node $node) {
 
    $flag_id = 'moderate_node';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $node);

}

/**
 * Flag comment.
 */
function admin_task_notify_comment_update(Comment $comment) {

    $flag_id = 'moderate_comment';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $comment);

}

How do I get the administrator to automatically flag  ?

<?php

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;

/**
 * Flag user.
 */
function admin_task_notify_user_update(User $account) {
  $flag_id = 'moderate_user';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $flag_service->flag($flag, $account);
}

/**
 * Flag store.
 */
function admin_task_notify_commerce_store_update(Store $commerce_store) {

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

/**
 * Flag product.
 */
function admin_task_notify_commerce_product_update(Product $commerce_product) {

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

/**
 * Flag group.
 */
function admin_task_notify_group_update(Group $group) {

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

/**
 * Flag node.
 */
function admin_task_notify_node_update(Node $node) {

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

}

/**
 * Flag comment.
 */
function admin_task_notify_comment_update(Comment $comment) {

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

}

UPDATE

I applied Clive's answer but I got a PHP error :

TypeError : Argument 3 passed to Drupal\flag\FlagService::flag() must implement interface Drupal\Core\Session\AccountInterface or be null, instance of Drupal\node\Entity\Node given, called in /var/www/www-example-com/web/modules/custom/admin_task_notify/admin_task_notify.module on line 71 dans Drupal\flag\FlagService->flag() (/var/www/www-example-com/web/modules/contrib/flag/src/FlagService.php ligne 234)

<?php

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;

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

/**
 * Flag store.
 */
function admin_task_notify_commerce_store_update(Store $commerce_store) {

    $flag_id = 'moderate_store';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $commerce_store);
  
}

/**
 * Flag product.
 */
function admin_task_notify_commerce_product_update(Product $commerce_product) {

    $flag_id = 'moderate_product';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $commerce_product);
  
}

/**
 * Flag group.
 */
function admin_task_notify_group_update(Group $group) {

    $flag_id = 'moderate_group';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $group);
  
}

/**
 * Flag node.
 */
function admin_task_notify_node_update(Node $node) {
 
    $flag_id = 'moderate_node';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $node);

}

/**
 * Flag comment.
 */
function admin_task_notify_comment_update(Comment $comment) {

    $flag_id = 'moderate_comment';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $comment);

}

How do I get the administrator to automatically flag?

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;

/**
 * Flag node.
 */
function admin_task_notify_node_update(Node $node) {
    $flag_id = 'moderate_node';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $flag_service->flag($flag, $node);
}
added 3166 characters in body
Source Link
sdfff
  • 61
  • 6

UPDATE

I applied Clive's answer but I got a PHP error :

TypeError : Argument 3 passed to Drupal\flag\FlagService::flag() must implement interface Drupal\Core\Session\AccountInterface or be null, instance of Drupal\node\Entity\Node given, called in /var/www/www-example-com/web/modules/custom/admin_task_notify/admin_task_notify.module on line 71 dans Drupal\flag\FlagService->flag() (/var/www/www-example-com/web/modules/contrib/flag/src/FlagService.php ligne 234)

<?php

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;

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

/**
 * Flag store.
 */
function admin_task_notify_commerce_store_update(Store $commerce_store) {

    $flag_id = 'moderate_store';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $commerce_store);
  
}

/**
 * Flag product.
 */
function admin_task_notify_commerce_product_update(Product $commerce_product) {

    $flag_id = 'moderate_product';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $commerce_product);
  
}

/**
 * Flag group.
 */
function admin_task_notify_group_update(Group $group) {

    $flag_id = 'moderate_group';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $group);
  
}

/**
 * Flag node.
 */
function admin_task_notify_node_update(Node $node) {

    $flag_id = 'moderate_node';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $node);

}

/**
 * Flag comment.
 */
function admin_task_notify_comment_update(Comment $comment) {

    $flag_id = 'moderate_comment';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $comment);

}

UPDATE

I applied Clive's answer but I got a PHP error :

TypeError : Argument 3 passed to Drupal\flag\FlagService::flag() must implement interface Drupal\Core\Session\AccountInterface or be null, instance of Drupal\node\Entity\Node given, called in /var/www/www-example-com/web/modules/custom/admin_task_notify/admin_task_notify.module on line 71 dans Drupal\flag\FlagService->flag() (/var/www/www-example-com/web/modules/contrib/flag/src/FlagService.php ligne 234)

<?php

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;

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

/**
 * Flag store.
 */
function admin_task_notify_commerce_store_update(Store $commerce_store) {

    $flag_id = 'moderate_store';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $commerce_store);
  
}

/**
 * Flag product.
 */
function admin_task_notify_commerce_product_update(Product $commerce_product) {

    $flag_id = 'moderate_product';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $commerce_product);
  
}

/**
 * Flag group.
 */
function admin_task_notify_group_update(Group $group) {

    $flag_id = 'moderate_group';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $group);
  
}

/**
 * Flag node.
 */
function admin_task_notify_node_update(Node $node) {

    $flag_id = 'moderate_node';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $node);

}

/**
 * Flag comment.
 */
function admin_task_notify_comment_update(Comment $comment) {

    $flag_id = 'moderate_comment';
    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $account = \Drupal\user\Entity\User::load(1);
    $flag_service->flag($flag, $account, $comment);

}
Source Link
sdfff
  • 61
  • 6

How do I get the administrator to automatically flag?

I created a module for flagging entities automatically when created or updated.

There are some errors in my code because only nodes work.

If a user "user1" creates a node, the flag will be in his name. I want the flag to be done automatically on behalf of the site administrator.

How do I get the administrator to automatically flag ?

<?php

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;

/**
 * Flag user.
 */
function admin_task_notify_user_update(User $account) {
  $flag_id = 'moderate_user';
  $flag_service = \Drupal::service('flag');
  $flag = $flag_service->getFlagById($flag_id);
  $flag_service->flag($flag, $account);
}

/**
 * Flag store.
 */
function admin_task_notify_commerce_store_update(Store $commerce_store) {

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

/**
 * Flag product.
 */
function admin_task_notify_commerce_product_update(Product $commerce_product) {

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

/**
 * Flag group.
 */
function admin_task_notify_group_update(Group $group) {

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

/**
 * Flag node.
 */
function admin_task_notify_node_update(Node $node) {

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

}

/**
 * Flag comment.
 */
function admin_task_notify_comment_update(Comment $comment) {

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

}