You are here

class FormatterSingleJson in RESTful 7.2

Class FormatterSingleJson @package Drupal\restful\Plugin\formatter

Plugin annotation


@Formatter(
  id = "single_json",
  label = "Single JSON",
  description = "Output a single item using the JSON format."
)

Hierarchy

Expanded class hierarchy of FormatterSingleJson

File

src/Plugin/formatter/FormatterSingleJson.php, line 22
Contains \Drupal\restful\Plugin\formatter\FormatterSingleJson.

Namespace

Drupal\restful\Plugin\formatter
View source
class FormatterSingleJson extends FormatterJson {

  /**
   * Content Type
   *
   * @var string
   */
  protected $contentType = 'application/drupal.single+json; charset=utf-8';

  /**
   * {@inheritdoc}
   */
  public function prepare(array $data) {

    // If we're returning an error then set the content type to
    // 'application/problem+json; charset=utf-8'.
    if (!empty($data['status']) && floor($data['status'] / 100) != 2) {
      $this->contentType = 'application/problem+json; charset=utf-8';
      return $data;
    }
    $extracted = $this
      ->extractFieldValues($data);
    $output = $this
      ->limitFields($extracted);

    // Force returning a single item.
    $output = ResourceFieldBase::isArrayNumeric($output) ? reset($output) : $output;
    return $output ?: array();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurablePluginTrait::$instanceConfiguration protected property Plugin instance configuration.
ConfigurablePluginTrait::calculateDependencies public function
ConfigurablePluginTrait::defaultConfiguration public function 1
ConfigurablePluginTrait::getConfiguration public function
ConfigurablePluginTrait::setConfiguration public function
Formatter::$resource protected property The resource handler containing more info about the request.
Formatter::cacheFragments protected static function Gets a cache fragments based on the data to be rendered.
Formatter::calculateItemsPerPage protected function Helper function that calculates the number of items per page.
Formatter::createCacheController protected function Gets a cache controller based on the data to be rendered.
Formatter::format public function Formats the un-structured data into the output format. Overrides FormatterInterface::format
Formatter::getCachedData protected function Gets the cached computed value for the fields to be rendered.
Formatter::getCacheHash protected function Gets the cached computed value for the fields to be rendered.
Formatter::getResource public function Gets the underlying resource. Overrides FormatterInterface::getResource
Formatter::isCacheEnabled protected function Checks if the passed in data to be rendered can be cached.
Formatter::isIterable protected static function Helper function to know if a variable is iterable or not.
Formatter::limitFields protected function Returns only the allowed fields by filtering out the other ones.
Formatter::setCachedData protected function Gets the cached computed value for the fields to be rendered.
Formatter::setResource public function Sets the underlying resource. Overrides FormatterInterface::setResource
Formatter::unprefixInputOptions protected static function Given a prefix, return the allowed fields that apply removing the prefix.
Formatter::__construct public function
FormatterJson::addHateoas protected function Add HATEOAS links to list of item.
FormatterJson::extractFieldValues protected function Extracts the actual values from the resource fields.
FormatterJson::getContentTypeHeader public function Returns the content type for the selected output format. Overrides Formatter::getContentTypeHeader
FormatterJson::parseBody public function Parses the body string into the common format. Overrides Formatter::parseBody
FormatterJson::render public function Renders an array in the selected format. Overrides FormatterInterface::render
FormatterSingleJson::$contentType protected property Content Type Overrides FormatterJson::$contentType
FormatterSingleJson::prepare public function Massages the raw data to create a structured array to pass to the renderer. Overrides FormatterJson::prepare