flagging unflagging cards

This commit is contained in:
2020-11-24 14:07:10 +01:00
parent ffc4a88094
commit a38653f7ce
14 changed files with 476 additions and 81 deletions

View File

@@ -21,3 +21,27 @@ materio_flag.delete_user_flagging_collection:
_title: 'Delete User Flagging Collection'
requirements:
_permission: 'edit own flag lists'
materio_flag.flag:
path: 'materio_flag/flag'
defaults:
_controller: '\Drupal\materio_flag\Controller\MaterioFlagActionsController::flaglistentity'
_title: 'Add entity to Flagging Collection'
requirements:
_permission: 'edit own flag lists'
materio_flag.unFlag:
path: 'materio_flag/unflag'
defaults:
_controller: '\Drupal\materio_flag\Controller\MaterioFlagActionsController::unFlaglistentity'
_title: 'Remove entity from Flagging Collection'
requirements:
_permission: 'edit own flag lists'
# materio_flag.unflag
# path: 'materio_flag/unflag'
# defaults:
# _controller: '\Drupal\materio_flag\Controller\MaterioFlagController::unFlag'
# _title: 'Remove entity from Flagging Collection'
# requirements:
# _permission: 'edit own flag lists'

View File

@@ -0,0 +1,118 @@
<?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'];
$uuid = $post_data['uuid'];
$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, $nid, $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' => $nid,
'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'];
$uuid = $post_data['uuid'];
$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, $nid, $flagcollid);
// response
$data = [
'flag' => $flag->toArray(),
'flagid' => $flagid,
'entity_id' => $nid,
'entity_uuid' => $uuid,
'flagcollid' => $flagcollid,
];
return new JsonResponse($data);
}
}

View File

@@ -6,8 +6,10 @@ use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\flag_lists\FlagListsService;
use Drupal\flag_lists\Entity\FlaggingCollection;
use Drupal\flag_lists\Controller\ActionLinkController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
@@ -16,10 +18,15 @@ use Symfony\Component\HttpFoundation\JsonResponse;
class MaterioFlagController extends ControllerBase {
/*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* @var \Drupal\flag_lists\FlagListsService
*/
protected $flaglists;
protected $flagListsService;
/**
* @var \Drupal\user\User
@@ -31,6 +38,7 @@ class MaterioFlagController extends ControllerBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('flaglists'),
$container->get('current_user')
);
@@ -39,9 +47,10 @@ class MaterioFlagController extends ControllerBase {
/**
* Constructs a new MaterioFlagController object.
*/
public function __construct(FlagListsService $flag_lists_service, AccountProxyInterface $account) {
$this->flaglists = $flag_lists_service;
$this->user = $account;
public function __construct(EntityTypeManagerInterface $entity_type_manager, FlagListsService $flag_lists_service, AccountProxyInterface $account) {
$this->entityTypeManager = $entity_type_manager;
$this->flagListsService = $flag_lists_service;
$this->user = $account;
}
/**
@@ -52,13 +61,35 @@ class MaterioFlagController extends ControllerBase {
*/
public function getUsersFlaggingCollections() {
$colls = $this->flaglists->getUsersFlaggingCollections();
$colls = $this->flagListsService->getUsersFlaggingCollections();
$data = [];
foreach ($colls as $id => $collection) {
$data[] = array(
"id" => $id,
"name" => $collection->getName()
$flag_id = $collection->getRelatedFlag()->id();
// get the items
$itemsids = $this->flagListsService->getFlagListItemIds($flag_id,$id);
$items = [];
foreach ($this->flagListsService->getFlagListItems($itemsids) as $id => $item) {
// $items[] = array(
// 'id' => $id,
// 'name' => $item->getName(),
// 'connected_entity_id' => $item->getConnectedEntityId(),
// );
$items[] = $item->getConnectedEntityId();
}
$items_uuids = [];
foreach ($items as $nid) {
$node = $this->entityTypeManager->getStorage('node')->load($nid);
$items_uuids[] = $node->uuid();
}
$data[$collection->id()] = array(
"id" => $collection->id(),
"name" => $collection->getName(),
"flag_id" => $flag_id,
"items" => $items,
"items_uuids" => $items_uuids,
);
}
@@ -91,9 +122,9 @@ class MaterioFlagController extends ControllerBase {
public function deleteUserFlaggingCollection(Request $request) {
// dpm($request);
$post_data = json_decode( $request->getContent(),TRUE);
$flagid = $post_data['flagid'];
$flagcollid = $post_data['flagcollid'];
$flagcoll = $this->flaglists->getFlaggingCollectionById($flagid);
$flagcoll = $this->flagListsService->getFlaggingCollectionById($flagcollid);
// dump($flagcoll);
$flagcoll->delete();
// TODO: warning, sometimes relatedFlag deos not exists
@@ -101,9 +132,36 @@ class MaterioFlagController extends ControllerBase {
$data = [
// 'result' => $flag,
'id' => $flagid
'id' => $flagcollid
];
return new JsonResponse($data);
}
// public function flag(Request $request) {
// // dpm($request);
// $post_data = json_decode( $request->getContent(),TRUE);
// $flagid = $post_data['flagid'];
// $nid = $post_data['nid'];
// $flagcollid = $post_data['flagcollid'];
//
// $actionLinkController = new ActionLinkController();
// // FlagInterface $flag, $entity_id, $flag_list
// $status = $actionLinkController->flag($flagid, $nid, $flagcollid);
//
// // $flagcoll = $this->flagListsService->getFlaggingCollectionById($flagid);
// // // dump($flagcoll);
// // $flagcoll->delete();
// // // TODO: warning, sometimes relatedFlag deos not exists
// // // $flag = $flagcoll->getRelatedFlag();
//
// $data = [
// 'status' => $flag,
// 'flagid' => $flagid,
// 'nid' => $nid,
// 'flagcollid' => $flagcollid
// ];
// return new JsonResponse($data);
// }
}

View File

@@ -101,6 +101,7 @@ class Base extends ControllerBase {
// $items = [];
$uuids = [];
$nids = [];
foreach ($this->results as $result) {
// $nid = $result->getField('nid')->getValues()[0];
// $uuid = $result->getField('uuid')->getValues()[0];
@@ -111,9 +112,11 @@ class Base extends ControllerBase {
// 'title' => $title,
// ];
$uuids[] = $result->getField('uuid')->getValues()[0];
$nids[] = $result->getField('nid')->getValues()[0];
}
// $resp['items'] = $items;
$resp['uuids'] = $uuids;
$resp['nids'] = $nids;
}
return new JsonResponse($resp);