You are here

class ShsCacheableJsonResponse in Simple hierarchical select 8

Same name and namespace in other branches
  1. 2.0.x src/Cache/ShsCacheableJsonResponse.php \Drupal\shs\Cache\ShsCacheableJsonResponse

A cacheable JsonResponse that returns alterable data for SHS.

Hierarchy

Expanded class hierarchy of ShsCacheableJsonResponse

1 file declares its use of ShsCacheableJsonResponse
ShsController.php in src/Controller/ShsController.php

File

src/Cache/ShsCacheableJsonResponse.php, line 10

Namespace

Drupal\shs\Cache
View source
class ShsCacheableJsonResponse extends CacheableJsonResponse {

  /**
   * Response context used to alter the response data.
   *
   * @var array
   *   Array container the field identifier and the bundle.
   */
  protected $context;

  /**
   * {@inheritdoc}
   */
  public function __construct($context, $data = NULL, $status = 200, $headers = []) {
    parent::__construct($data, $status, $headers);
    $this->context = $context;
    $this->context['encodingOptions'] = $this->encodingOptions;
  }

  /**
   * {@inheritdoc}
   *
   * @param mixed $data
   *   Data to send as JsonResponse.
   * @param bool $alterable
   *   TRUE if the data should be alterable by other modules.
   */
  public function setData($data = [], $alterable = FALSE) {
    if ($alterable === TRUE) {
      $identifier = str_replace('-', '_', $this->context['identifier']);
      $hooks = [
        'shs_term_data',
        "shs__bundle_{$this->context['bundle']}__term_data",
        "shs__field_{$identifier}__term_data",
      ];
      \Drupal::moduleHandler()
        ->alter($hooks, $data, $this->context);
    }
    parent::setData($data);
  }

  /**
   * {@inheritdoc}
   */
  public function sendContent() {
    $identifier = str_replace('-', '_', $this->context['identifier']);
    $hooks = [
      'shs_term_data_response',
      "shs__bundle_{$this->context['bundle']}__term_data_response",
      "shs__field_{$identifier}__term_data_response",
    ];

    // Alter json encoded data.
    \Drupal::moduleHandler()
      ->alter($hooks, $this->content, $this->context);
    echo $this->content;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableResponseTrait::$cacheabilityMetadata protected property The cacheability metadata.
CacheableResponseTrait::addCacheableDependency public function
CacheableResponseTrait::getCacheableMetadata public function
ShsCacheableJsonResponse::$context protected property Response context used to alter the response data.
ShsCacheableJsonResponse::sendContent public function Sends content for the current web response.
ShsCacheableJsonResponse::setData public function
ShsCacheableJsonResponse::__construct public function Constructor.