123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?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);
- }
- }
|