MaterioFlagController.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Drupal\materio_flag\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Drupal\Core\Session\AccountProxy;
  6. use Drupal\Core\Session\AccountProxyInterface;
  7. use Drupal\flag_lists\FlagListsService;
  8. use Drupal\flag_lists\Entity\FlaggingCollection;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. /**
  12. * Class AjaxHomeController.
  13. */
  14. class MaterioFlagController extends ControllerBase {
  15. /**
  16. * @var \Drupal\flag_lists\FlagListsService
  17. */
  18. protected $flaglists;
  19. /**
  20. * @var \Drupal\user\User
  21. */
  22. protected $user;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function create(ContainerInterface $container) {
  27. return new static(
  28. $container->get('flaglists'),
  29. $container->get('current_user')
  30. );
  31. }
  32. /**
  33. * Constructs a new MaterioFlagController object.
  34. */
  35. public function __construct(FlagListsService $flag_lists_service, AccountProxyInterface $account) {
  36. $this->flaglists = $flag_lists_service;
  37. $this->user = $account;
  38. }
  39. /**
  40. * Hello.
  41. *
  42. * @return string
  43. * Return Hello string.
  44. */
  45. public function getUsersFlaggingCollections() {
  46. $colls = $this->flaglists->getUsersFlaggingCollections();
  47. $data = [];
  48. foreach ($colls as $id => $collection) {
  49. $data[] = array(
  50. "id" => $id,
  51. "name" => $collection->getName()
  52. );
  53. }
  54. return new JsonResponse($data);
  55. }
  56. public function createUserFlaggingCollection(Request $request) {
  57. // dpm($request);
  58. $post_data = json_decode( $request->getContent(),TRUE);
  59. $name = $post_data['name'];
  60. $newFlagColl = FlaggingCollection::Create([
  61. 'type' => 'flagging_collection_type',
  62. 'name' => $name,
  63. 'user_id' => $this->user->id(),
  64. 'templateflag' => 'dossier'
  65. ]);
  66. $newFlagColl->save();
  67. $data = [
  68. 'status' => $newFlagColl->get('status')->value,
  69. 'id' => $newFlagColl->id(),
  70. 'name' => $newFlagColl->getName(),
  71. 'newflagcoll_toarray' => $newFlagColl->toArray()
  72. ];
  73. return new JsonResponse($data);
  74. }
  75. }