Skip to content

Instantly share code, notes, and snippets.

@killua99
Created May 25, 2016 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killua99/0e58e65ddc9feff1440667a78a806b24 to your computer and use it in GitHub Desktop.
Save killua99/0e58e65ddc9feff1440667a78a806b24 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\storleden_search\Plugin\rest\resource;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Psr\Log\LoggerInterface;
use Drupal\storleden_search\CalendarQueryBuilder;
/**
* Provides a resource to get view modes by entity and bundle.
*
* @RestResource(
* id = "storleden_search_calendar_rest",
* label = @Translation("Storleden Search - Calendar REST"),
* uri_paths = {
* "canonical" = "/evenemang"
* }
* )
*/
class CalendarRestResource extends ResourceBase {
/**
* A current user instance.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* @var \Drupal\storleden_search\CalendarQueryBuilder;
*/
protected $calendarQuery;
/**
* @var \Drupal\Core\Entity\EntityManagerInterface;
*/
protected $entityTypeManager;
/**
* Constructs a Drupal\rest\Plugin\ResourceBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param array $serializer_formats
* The available serialization formats.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* A current user instance.
* @param \Drupal\storleden_search\CalendarQueryBuilder $calendar_query
* Storleden Search Query builder.
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* EntityTypeManager.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
AccountProxyInterface $current_user,
CalendarQueryBuilder $calendar_query,
EntityTypeManager $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->currentUser = $current_user;
$this->calendarQuery = $calendar_query;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@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('storleden_search'),
$container->get('current_user'),
$container->get('storleden_search.calendar_query'),
$container->get('entity_type.manager')
);
}
/**
* Responds to GET requests.
*
* Returns a list of bundles for specified entity.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function get() {
// You must to implement the logic of your REST Resource here.
// Use current user after pass authentication to validate access.
if (!$this->currentUser->hasPermission('access content')) {
throw new AccessDeniedHttpException();
}
$items = $this->calendarQuery->getItems();
$results = array();
foreach ($items as $item) {
list(, $path, $langcode) = explode(':', $item->getId());
list($entity_type, $id) = explode('/', $path);
// Get the entity.
$entity = $this->entityTypeManager()->getStorage($entity_type)->load($id);
// Render as view modes.
// $key = 'entity:' . $entity_type . '_' . $entity->bundle();
// $view_mode_configuration = array('teaser');
// $view_mode = isset($view_mode_configuration[$key]) ? $view_mode_configuration[$key] : 'default';
$view_mode = 'teaser_3_column';
$results[] = $this->entityTypeManager()->getViewBuilder($entity_type)->view($entity, $view_mode);
}
return new ResourceResponse($results, 200);
}
/**
* {@inheritdoc}
*/
protected function getBaseRoute($canonical_path, $method) {
$route = parent::getBaseRoute($canonical_path, $method);
$parameters = $route->getOption('parameters') ?: [];
$route->setOption('parameters', $parameters);
return $route;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment