apetkevicius
3/19/2018 - 8:37 AM

Custom data response plugin

Custom data response plugin.

<?php

namespace Drupal\[custom_module]\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Drupal\user\UserStorageInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a custom data resource
 *
 * @RestResource(
 *   id = "custom_data_resource",
 *   label = @Translation("Custom data Resource"),
 *   uri_paths = {
 *     "canonical" = "/custom_rest_api/custom_data_resource"
 *   }
 * )
 */
class CustomDataResource extends ResourceBase {

  /**
   * The available serialization formats.
   *
   * @var array
   */
  protected $serializerFormats = [];

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The user storage.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * 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\user\UserStorageInterface $user_storage
   *   The user storage.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, UserStorageInterface $user_storage) {
    $this->configuration = $configuration;
    $this->pluginId = $plugin_id;
    $this->pluginDefinition = $plugin_definition;
    $this->serializerFormats = $serializer_formats;
    $this->logger = $logger;
    $this->userStorage = $user_storage;
  }

  /**
   * {@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('entity.manager')->getStorage('user')
    );
  }

  /**
   * Responds to entity GET requests.
   * @return \Drupal\rest\ResourceResponse
   */
  public function get() {
    // Current user.
    $account = \Drupal::currentUser();

    // Get user account object.
    if ($this->userStorage->load($account->id()) !== NULL) {
      $account = $this->userStorage->load($account->id());
    }

    $data = [];

    // ... collect all the data.

    $response = [
      'error' => 1,
      'message' => 'We have no data for this user.'
    ];

    if (isset($data)) {
      $response = [
        'error' => 0,
        'data' => $data
      ];
    }

    // Disable cache.
    $build = array(
      '#cache' => array(
        'max-age' => 0,
      ),
    );

    return (new ResourceResponse($response))->addCacheableDependency($build);
  }
}