You are here

public function DataProviderEntity::getCacheFragments in RESTful 7.2

Gets the entity context.

Parameters

mixed $identifier: The ID.

Overrides DataProvider::getCacheFragments

File

src/Plugin/resource/DataProvider/DataProviderEntity.php, line 124
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntity.

Class

DataProviderEntity
Class DataProviderEntity.

Namespace

Drupal\restful\Plugin\resource\DataProvider

Code

public function getCacheFragments($identifier) {
  if (is_array($identifier)) {

    // Like in https://example.org/api/articles/1,2,3.
    $identifier = implode(ResourceInterface::IDS_SEPARATOR, $identifier);
  }
  $fragments = new ArrayCollection(array(
    'resource' => CacheDecoratedResource::serializeKeyValue($this->pluginId, $this
      ->canonicalPath($identifier)),
    'entity' => CacheDecoratedResource::serializeKeyValue($this->entityType, $this
      ->getEntityIdByFieldId($identifier)),
  ));
  $options = $this
    ->getOptions();
  switch ($options['renderCache']['granularity']) {
    case DRUPAL_CACHE_PER_USER:
      if ($uid = $this
        ->getAccount()->uid) {
        $fragments
          ->set('user_id', (int) $uid);
      }
      break;
    case DRUPAL_CACHE_PER_ROLE:
      $fragments
        ->set('user_role', implode(',', $this
        ->getAccount()->roles));
      break;
  }
  return $fragments;
}