ActionLinkHelper.php in Flag Lists 4.0.x
File
src/Controller/ActionLinkHelper.php
View source
<?php
namespace Drupal\flag_lists\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\flag_lists\FlagListsServiceInterface;
use Drupal\flag_lists\Entity\FlagListItem;
use Drupal\flag\FlagInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ActionLinkHelper implements ContainerInjectionInterface {
protected $flagListsService;
public function __construct(FlagListsServiceInterface $flag_lists) {
$this->flagListsService = $flag_lists;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('flaglists'));
}
public function flagHelper(FlagInterface $flag, $entity_id, $flag_list) {
$flag_list_name = $this->flagListsService
->getFlaggingCollectionById($flag_list)
->getName();
$flag_list_item = FlagListItem::create([
'entity_id' => $entity_id,
'type' => $flag
->getFlaggableEntityTypeId(),
'baseflag' => $flag
->id(),
'flag_list' => $flag_list,
'name' => $flag_list_name . ' ' . $entity_id,
]);
$flag_list_item
->save();
}
public function unflagHelper(FlagInterface $flag, $entity_id, $flag_list) {
$baseflag = $flag
->id();
$ids = $this->flagListsService
->getFlagListItemIds($baseflag, $flag_list, $entity_id);
foreach ($ids as $id) {
$flagListItem = \Drupal::entityTypeManager()
->getStorage('flag_list_item')
->load($id);
$flagListItem
->delete();
}
}
}