jDrupalResource.php in jDrupal 8.0.x
File
src/Plugin/rest/resource/jDrupalResource.php
View source
<?php
namespace Drupal\jDrupal\Plugin\rest\resource;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Psr\Log\LoggerInterface;
class jDrupalResource extends ResourceBase {
protected $currentUser;
protected $entityManager;
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('entity.manager'), $container
->get('current_user'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, EntityManagerInterface $entity_manager, AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->entityManager = $entity_manager;
$this->currentUser = $current_user;
}
public function get($entity = NULL) {
if ($entity) {
$permission = 'Administer content types';
if (!$this->currentUser
->hasPermission($permission)) {
throw new AccessDeniedHttpException();
}
$bundles_entities = \Drupal::entityManager()
->getStorage($entity . '_type')
->loadMultiple();
$bundles = array();
foreach ($bundles_entities as $entity) {
$bundles[$entity
->id()] = $entity
->label();
}
if (!empty($bundles)) {
return new ResourceResponse($bundles);
}
throw new NotFoundHttpException(t('Bundles for entity @entity were not found', array(
'@entity' => $entity,
)));
}
throw new HttpException(t('Entity wasn\'t provided'));
}
}