jDrupalConnect.php in jDrupal 8.0.x
File
src/Plugin/rest/resource/jDrupalConnect.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 jDrupalConnect 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() {
$account = \Drupal::currentUser();
$results = array(
'uid' => $account
->id(),
'name' => $account
->getAccountName(),
'roles' => $account
->getRoles(),
);
\Drupal::moduleHandler()
->alter('jdrupal_connect', $results);
$response = new ResourceResponse($results);
$response
->addCacheableDependency($account);
return $response;
throw new HttpException(t('jDrupal Connect GET Failed!'));
}
}