materio-d9/web/modules/custom/materio_flag/src/Controller/MaterioFlagActionsControlle...

119 lines
3.8 KiB
PHP

<?php
// https://developpeur-drupal.com/article/injection-dependances-lors-heritage-classe-qui-implemente-controllerbase
namespace Drupal\materio_flag\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\flag\FlagInterface;
use Drupal\flag\FlagServiceInterface;
use Drupal\flag_lists\FlagListsService;
use Drupal\flag_lists\FlagListsServiceInterface;
use Drupal\flag_lists\Entity\FlaggingCollection;
use Drupal\flag_lists\Controller\ActionLinkController;
use Drupal\flag_lists\Controller\ActionLinkHelper;
use Drupal\flag_lists\Entity\FlagListItem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class AjaxHomeController.
*/
class MaterioFlagActionsController extends ActionLinkController {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('flag'),
$container->get('flaglists'),
$container->get('renderer')
);
}
/**
* Constructs a new MaterioFlagController object.
*/
public function __construct(
FlagServiceInterface $flag,
FlagListsServiceInterface $flag_lists,
RendererInterface $renderer
) {
parent::__construct($flag, $flag_lists, $renderer);
}
public function flaglistentity(Request $request) {
$post_data = json_decode( $request->getContent(),TRUE);
$flagid = $post_data['flagid'];
$id = $post_data['id'];
$flagcollid = $post_data['flagcollid'];
// $flagcoll = $this->flagListsService->getFlaggingCollectionById($flagcollid);
// $flag = $flagcoll->getRelatedFlag();
$flag = $this->flagService->getFlagById($flagid);
// $node = \Drupal::service('entity.repository')->loadEntityByUuid('node', $uuid);
// $nid = $node->id();
// call the parent flag function
$this->flag($flag, $id, $flagcollid);
// // OR rewrite it entirely
// $entity = $this->flagService->getFlaggableById($flag, $nid);
// // flag
// $this->flagService->flag($flag, $entity);
// // Create the flag list item.
// // $actionLinkHelper = new ActionLinkHelper($this->flagListsService);
// // $actionLinkHelper->flagHelper($flag, $nid, $flagcoll);
// $flag_list_item = FlagListItem::create([
// 'entity_id' => $nid,
// 'type' => $flag->getFlaggableEntityTypeId(),
// 'baseflag' => $flagid,
// 'flag_list' => $flagcollid,
// 'name' => $flagcoll->getName() . ' ' . $nid,
// ]);
// $flag_list_item->save();
$data = [
'flag' => $flag->toArray(),
'flagid' => $flagid,
'entity_id' => $id,
// 'entity_uuid' => $uuid,
'flagcollid' => $flagcollid,
// 'post_data' => $post_data,
// 'flaggableEntityTypeId' => $flag->getFlaggableEntityTypeId(),
// 'entity_title' => $entity->get('title')->value,
// 'flag_list_item' => $flag_list_item->getName(),
];
return new JsonResponse($data);
}
public function unFlaglistentity(Request $request) {
$post_data = json_decode( $request->getContent(),TRUE);
$flagid = $post_data['flagid'];
$id = $post_data['id'];
$flagcollid = $post_data['flagcollid'];
// get flag
$flag = $this->flagService->getFlagById($flagid);
// get the nid from uuid
// $node = \Drupal::service('entity.repository')->loadEntityByUuid('node', $uuid);
// $nid = $node->id();
// call the parent flag function
$this->unflag($flag, $id, $flagcollid);
// response
$data = [
'flag' => $flag->toArray(),
'flagid' => $flagid,
'entity_id' => $id,
// 'entity_uuid' => $uuid,
'flagcollid' => $flagcollid,
];
return new JsonResponse($data);
}
}