MaterioFlagActionsController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // https://developpeur-drupal.com/article/injection-dependances-lors-heritage-classe-qui-implemente-controllerbase
  3. namespace Drupal\materio_flag\Controller;
  4. use Drupal\Core\Controller\ControllerBase;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Drupal\Core\Session\AccountProxy;
  7. use Drupal\Core\Session\AccountProxyInterface;
  8. use Drupal\Core\Render\RendererInterface;
  9. use Drupal\flag\FlagInterface;
  10. use Drupal\flag\FlagServiceInterface;
  11. use Drupal\flag_lists\FlagListsService;
  12. use Drupal\flag_lists\FlagListsServiceInterface;
  13. use Drupal\flag_lists\Entity\FlaggingCollection;
  14. use Drupal\flag_lists\Controller\ActionLinkController;
  15. use Drupal\flag_lists\Controller\ActionLinkHelper;
  16. use Drupal\flag_lists\Entity\FlagListItem;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. /**
  20. * Class AjaxHomeController.
  21. */
  22. class MaterioFlagActionsController extends ActionLinkController {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function create(ContainerInterface $container) {
  27. return new static(
  28. $container->get('flag'),
  29. $container->get('flaglists'),
  30. $container->get('renderer')
  31. );
  32. }
  33. /**
  34. * Constructs a new MaterioFlagController object.
  35. */
  36. public function __construct(
  37. FlagServiceInterface $flag,
  38. FlagListsServiceInterface $flag_lists,
  39. RendererInterface $renderer
  40. ) {
  41. parent::__construct($flag, $flag_lists, $renderer);
  42. }
  43. public function flaglistentity(Request $request) {
  44. $post_data = json_decode( $request->getContent(),TRUE);
  45. $flagid = $post_data['flagid'];
  46. $uuid = $post_data['uuid'];
  47. $flagcollid = $post_data['flagcollid'];
  48. // $flagcoll = $this->flagListsService->getFlaggingCollectionById($flagcollid);
  49. // $flag = $flagcoll->getRelatedFlag();
  50. $flag = $this->flagService->getFlagById($flagid);
  51. $node = \Drupal::service('entity.repository')->loadEntityByUuid('node', $uuid);
  52. $nid = $node->id();
  53. // call the parent flag function
  54. $this->flag($flag, $nid, $flagcollid);
  55. // // OR rewrite it entirely
  56. // $entity = $this->flagService->getFlaggableById($flag, $nid);
  57. // // flag
  58. // $this->flagService->flag($flag, $entity);
  59. // // Create the flag list item.
  60. // // $actionLinkHelper = new ActionLinkHelper($this->flagListsService);
  61. // // $actionLinkHelper->flagHelper($flag, $nid, $flagcoll);
  62. // $flag_list_item = FlagListItem::create([
  63. // 'entity_id' => $nid,
  64. // 'type' => $flag->getFlaggableEntityTypeId(),
  65. // 'baseflag' => $flagid,
  66. // 'flag_list' => $flagcollid,
  67. // 'name' => $flagcoll->getName() . ' ' . $nid,
  68. // ]);
  69. // $flag_list_item->save();
  70. $data = [
  71. 'flag' => $flag->toArray(),
  72. 'flagid' => $flagid,
  73. 'entity_id' => $nid,
  74. 'entity_uuid' => $uuid,
  75. 'flagcollid' => $flagcollid,
  76. // 'post_data' => $post_data,
  77. // 'flaggableEntityTypeId' => $flag->getFlaggableEntityTypeId(),
  78. // 'entity_title' => $entity->get('title')->value,
  79. // 'flag_list_item' => $flag_list_item->getName(),
  80. ];
  81. return new JsonResponse($data);
  82. }
  83. public function unFlaglistentity(Request $request) {
  84. $post_data = json_decode( $request->getContent(),TRUE);
  85. $flagid = $post_data['flagid'];
  86. $uuid = $post_data['uuid'];
  87. $flagcollid = $post_data['flagcollid'];
  88. // get flag
  89. $flag = $this->flagService->getFlagById($flagid);
  90. // get the nid from uuid
  91. $node = \Drupal::service('entity.repository')->loadEntityByUuid('node', $uuid);
  92. $nid = $node->id();
  93. // call the parent flag function
  94. $this->unflag($flag, $nid, $flagcollid);
  95. // response
  96. $data = [
  97. 'flag' => $flag->toArray(),
  98. 'flagid' => $flagid,
  99. 'entity_id' => $nid,
  100. 'entity_uuid' => $uuid,
  101. 'flagcollid' => $flagcollid,
  102. ];
  103. return new JsonResponse($data);
  104. }
  105. }