You are here

function tmgmt_content_entity_access in Translation Management Tool 8

Implements hook_entity_access().

File

sources/content/tmgmt_content.module, line 370
Source plugin for the Translation Management system that handles entities.

Code

function tmgmt_content_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
  $result = AccessResult::neutral();
  $key = \Drupal::request()->query
    ->get('key');
  if ($entity instanceof ContentEntityInterface && $operation == 'view' && $key) {
    $entity = $entity
      ->getUntranslated();
    $source_langcode = $entity
      ->language()
      ->getId();
    $current_job_items = tmgmt_job_item_load_latest('content', $entity
      ->getEntityTypeId(), $entity
      ->id(), $source_langcode);

    /** @var \Drupal\tmgmt\JobItemInterface $job_item */
    if ($current_job_items) {
      foreach ($current_job_items as $job_item) {
        $valid_key = \Drupal::service('tmgmt_content.key_access')
          ->getKey($job_item);
        if ($key === $valid_key && \Drupal::config('tmgmt.settings')
          ->get('anonymous_access')) {
          $result = AccessResult::allowed();
        }
        $result
          ->addCacheableDependency($job_item);
      }
      $result
        ->addCacheableDependency(\Drupal::config('tmgmt.settings'));
    }
  }
  return $result
    ->addCacheContexts([
    'url.query_args:key',
  ]);
}