You are here

protected function PdbBlock::getContextEntityValue in Decoupled Blocks 8

Get context value for Entity context.

Parameters

string $key: The context key.

\Drupal\Core\Entity\Plugin\DataType\EntityAdapter $data: The context data.

array $context_values: Array with contexts values.

2 calls to PdbBlock::getContextEntityValue()
PdbBlock::addEntityJsContext in src/Plugin/Block/PdbBlock.php
Add serialized entity to the JS Contexts.
PdbBlock::getContextsValues in src/Plugin/Block/PdbBlock.php
Get the value of contexts.

File

src/Plugin/Block/PdbBlock.php, line 204

Class

PdbBlock
Class PdbBlock.

Namespace

Drupal\pdb\Plugin\Block

Code

protected function getContextEntityValue($key, EntityAdapter $data, array &$context_values) {
  $entity_context = $data
    ->getValue();
  if (!isset($context_values["{$key}:" . $entity_context
    ->getEntityTypeId()])) {

    // Get a copy of the context entity so we do not alter the original one.
    $entity = clone $entity_context;
    $entity_access = $entity
      ->access('view', NULL, TRUE);
    if (!$entity_access
      ->isAllowed()) {
      return;
    }
    foreach ($entity as $field_name => $field) {

      // @var \Drupal\Core\Field\FieldItemListInterface $field
      $field_access = $field
        ->access('view', NULL, TRUE);

      // @todo Used addCacheableDependency($field_access);
      if (!$field_access
        ->isAllowed()) {
        $entity
          ->set($field_name, NULL);
      }
    }
    $context_values["{$key}:" . $entity
      ->getEntityTypeId()] = $entity;
  }
}