diff --git a/modules/rest_flag/rest_flag-2946872-9.patch b/modules/rest_flag/rest_flag-2946872-9.patch new file mode 100644 index 0000000..e69de29 diff --git a/modules/rest_flag/rest_flag.info.yml b/modules/rest_flag/rest_flag.info.yml new file mode 100755 index 0000000..a7d7ffc --- /dev/null +++ b/modules/rest_flag/rest_flag.info.yml @@ -0,0 +1,7 @@ +name: 'REST Flag' +type: module +description: 'Flag REST features' +core: 8.x +package: 'Flag' +dependencies: + - flag diff --git a/modules/rest_flag/rest_flag.module b/modules/rest_flag/rest_flag.module new file mode 100755 index 0000000..665f137 --- /dev/null +++ b/modules/rest_flag/rest_flag.module @@ -0,0 +1,24 @@ +' . t('About') . ''; + $output .= '

' . t('Flag REST features') . '

'; + return $output; + + default: + } +} diff --git a/modules/rest_flag/src/Plugin/rest/resource/FlagResource.php b/modules/rest_flag/src/Plugin/rest/resource/FlagResource.php new file mode 100755 index 0000000..eb5842a --- /dev/null +++ b/modules/rest_flag/src/Plugin/rest/resource/FlagResource.php @@ -0,0 +1,51 @@ +getStatusCode() != 200) { + return $result; + } + $flagging = $this->performFlagging($data['flag_id'], $data['entity_type'], $data['entity_id'], 'flag'); + $this->cache->addCacheableDependency($flagging); + $response = new ResourceResponse($flagging); + } catch (HttpException $e) { + $response = new ResourceResponse(['message' => $e->getMessage()], $e->getStatusCode()); + } catch (\Exception $e) { + $code = !empty($e->getCode()) ? $e->getCode() : 500; + $response = new ResourceResponse(['message' => $e->getMessage()], $code); + } + $response->addCacheableDependency($this->cache); + return $response; + } + +} diff --git a/modules/rest_flag/src/Plugin/rest/resource/FlaggingResourceBase.php b/modules/rest_flag/src/Plugin/rest/resource/FlaggingResourceBase.php new file mode 100755 index 0000000..daf6e3f --- /dev/null +++ b/modules/rest_flag/src/Plugin/rest/resource/FlaggingResourceBase.php @@ -0,0 +1,149 @@ +currentUser = $current_user; + $this->flagService = $flag_service; + $this->entityTypeManager = $entity_manager; + $build['#cache']['contexts'] = ['user', 'url.query_args']; + $this->cache = CacheableMetadata::createFromRenderArray($build); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->getParameter('serializer.formats'), + $container->get('logger.factory')->get('rest'), + $container->get('current_user'), + $container->get('flag'), + $container->get('entity_type.manager') + ); + } + + /** + * Responds to POST requests. + * + */ + public function post(array $data) { + try { + $required_keys = ['flag_id', 'entity_type', 'entity_id']; + foreach ($required_keys as $required_key) { + if (empty($data[$required_key])) { + throw new CacheableNotAcceptableHttpException($this->cache, t('Missing body parameter ":key"', [ + ':key' => $required_key, + ])); + } + } + $response = new ResourceResponse(); + } catch (HttpException $e) { + $response = new ResourceResponse(['message' => $e->getMessage()], $e->getStatusCode()); + } catch (\Exception $e) { + $code = !empty($e->getCode()) ? $e->getCode() : 500; + $response = new ResourceResponse(['message' => $e->getMessage()], $code); + } + $response->addCacheableDependency($this->cache); + return $response; + } + + /** + * @param $flag_id + * @param $entity_type + * @param $entity_id + * @param string $action + * + * @return \Drupal\Core\Entity\EntityInterface|\Drupal\flag\FlagInterface|null + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + protected function performFlagging($flag_id, $entity_type, $entity_id, $action = 'flag') { + if ($action != 'flag' && $action != 'unflag') { + throw new \LogicException(t('Invalid flagging action')); + } + $flag = $this->flagService->getFlagById($flag_id); + $flaggable = $this->flagService->getFlaggableById($flag, $flag_id); + $action = $flag->actionAccess('unflag', $this->currentUser, $flaggable); + + if ($action->isForbidden()) { + throw new CacheableAccessDeniedHttpException($this->cache); + } + $entity = $this->entityTypeManager->getStorage($entity_type) + ->load($entity_id); + if (empty($entity)) { + throw new CacheableNotAcceptableHttpException($this->cache, t('Entity :id :entity_type not found', [ + ':id' => $entity_id, + ':tntity_type' => $entity_type, + ])); + + } + return $this->flagService->flag($flag, $entity, $this->currentUser); + } + + protected function returnResponse() { + + + } +} diff --git a/modules/rest_flag/src/Plugin/rest/resource/UnflagResource.php b/modules/rest_flag/src/Plugin/rest/resource/UnflagResource.php new file mode 100755 index 0000000..4a01a1d --- /dev/null +++ b/modules/rest_flag/src/Plugin/rest/resource/UnflagResource.php @@ -0,0 +1,53 @@ +getStatusCode() != 200) { + return $result; + } + $flagging = $this->performFlagging($data['flag_id'], $data['entity_type'], $data['entity_id'], 'unflag'); + $this->cache->addCacheableDependency($flagging); + $response = new ResourceResponse($flagging); + } catch (HttpException $e) { + $response = new ResourceResponse(['message' => $e->getMessage()], $e->getStatusCode()); + } catch (\Exception $e) { + $code = !empty($e->getCode()) ? $e->getCode() : 500; + $response = new ResourceResponse(['message' => $e->getMessage()], $code); + } + $response->addCacheableDependency($this->cache); + return $response; + + + } + +} diff --git a/modules/rest_flag/src/Plugin/rest/resource/UserFlaggingResource.php b/modules/rest_flag/src/Plugin/rest/resource/UserFlaggingResource.php new file mode 100755 index 0000000..518f8e0 --- /dev/null +++ b/modules/rest_flag/src/Plugin/rest/resource/UserFlaggingResource.php @@ -0,0 +1,169 @@ +currentUser = $current_user; + $this->flagService = $flag_service; + $this->entityTypeManager = $entity_manager; + $this->request = $request->getCurrentRequest(); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->getParameter('serializer.formats'), + $container->get('logger.factory')->get('rest'), + $container->get('current_user'), + $container->get('flag'), + $container->get('entity_type.manager'), + $container->get('request_stack') + ); + } + + /** + * Get list of all flagging user. + * + * @return \Drupal\rest\ResourceResponse + */ + public function get() { + try { + + $build['#cache']['contexts'] = ['user', 'url.query_args']; + $cache = CacheableMetadata::createFromRenderArray($build); + + $content_load = $this->request->query->get('content_load'); + $flag_id = $this->request->query->get('flag_id'); + if (!empty($flag_id)) { + $flags = [$this->flagService->getFlagById($flag_id)]; + } + + else { + $entity_type = $this->request->query->get('entity_type'); + $bundle = $this->request->query->get('bundle'); + $flags = $this->flagService->getAllFlags($entity_type, $bundle); + } + if (empty($flags)) { + $response = new ResourceResponse(); + $response->addCacheableDependency($cache); + return $response; + } + if ($content_load != TRUE) { + $flaggings = $this->flagService->getUserFlagging($flags, TRUE, $this->currentUser); + foreach ($flaggings as $flagging) { + $cache->addCacheableDependency($flagging); + } + $return = $flaggings; + } + else { + + $entity_to_load = []; + $results = $this->flagService->getUserFlagging($flags, FALSE, $this->currentUser); + + foreach ($results as $result) { + if (empty($result->entity_id) || empty($result->entity_type)) { + continue; + } + $entity_to_load[$result->entity_type][] = $result->entity_id; + } + $entities = []; + foreach ($entity_to_load as $entity_type => $ids) { + $entities[$entity_type] = $this->entityTypeManager->getStorage($entity_type) + ->loadMultiple($ids); + } + $return = []; + foreach ($results as $result) { + + if (empty($result->entity_id) || empty($result->entity_type) || empty($entities[$result->entity_type][$result->entity_id])) { + continue; + } + + $result = (array) $result; + $result['entity'] = $entities[$result['entity_type']][$result['entity_id']]; + $cache->addCacheableDependency($entities[$result['entity_type']][$result['entity_id']]); + $cache->addCacheTags(['flagging:' . $result['entity_id']]); + $return[] = $result; + } + } + + $response = new ResourceResponse($return); + } catch (HttpException $e) { + $response = new ResourceResponse(['message' => $e->getMessage()], $e->getStatusCode()); + } catch (\Exception $e) { + $code = !empty($e->getCode()) ? $e->getCode() : 500; + $response = new ResourceResponse(['message' => $e->getMessage()], $code); + } + $response->addCacheableDependency($cache); + return $response; + } + +}