FlagListsActionsController.php in Flag Lists 4.0.x
File
modules/flag_lists_actions/src/Controller/FlagListsActionsController.php
View source
<?php
namespace Drupal\flag_lists_actions\Controller;
use Drupal\system\Entity\Action;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
class FlagListsActionsController extends ControllerBase implements ContainerInjectionInterface {
public static function createActions(EntityInterface $entity) {
$flag = $entity
->getRelatedFlag();
$flag_id = 'flag_action.' . $flag
->id() . '_flag';
if (!Action::load($flag_id)) {
$action = Action::create([
'id' => $flag_id,
'type' => $flag
->getFlaggableEntityTypeId(),
'label' => \Drupal::token()
->replace($flag
->getShortText('flag'), [
'flagging_collection' => $entity,
], [
'clear',
]),
'plugin' => 'flag_action:' . $flag
->id() . '_flag',
'configuration' => [
'flag_id' => $flag
->id(),
'flag_action' => 'flag',
],
]);
$action
->trustData()
->save();
}
$unflag_id = 'flag_action.' . $flag
->id() . '_unflag';
if (!Action::load($unflag_id)) {
$action = Action::create([
'id' => $unflag_id,
'type' => $flag
->getFlaggableEntityTypeId(),
'label' => \Drupal::token()
->replace($flag
->getShortText('unflag'), [
'flagging_collection' => $entity,
], [
'clear',
]),
'plugin' => 'flag_action:' . $flag
->id() . '_unflag',
'configuration' => [
'flag_id' => $flag
->id(),
'flag_action' => 'unflag',
],
]);
$action
->trustData()
->save();
}
}
public static function deleteActions(EntityInterface $entity) {
$flag = $entity
->getRelatedFlag();
$actions = Action::loadMultiple([
'flag_action.' . $flag
->id() . '_flag',
'flag_action.' . $flag
->id() . '_unflag',
]);
foreach ($actions as $action) {
$action
->delete();
}
}
}