You are here

protected function TranslationOperationsField::getTranslationOperations in Translation Views 8

Operation links manager.

Decide which links we should generate: based on user permissions, and entity state (has translation, is default, etc.).

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The source entity to get context for decision.

string $source_langcode: The langcode of the row.

Return value

array Operation links' render array.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Core\Entity\EntityMalformedException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to TranslationOperationsField::getTranslationOperations()
TranslationOperationsField::render in src/Plugin/views/field/TranslationOperationsField.php
Build operation links.

File

src/Plugin/views/field/TranslationOperationsField.php, line 144

Class

TranslationOperationsField
Renders translation operations links.

Namespace

Drupal\translation_views\Plugin\views\field

Code

protected function getTranslationOperations(ContentEntityInterface $entity, $source_langcode) {
  $target_langcode = $this
    ->getTargetLangcode() ? $this
    ->getTargetLangcode() : $source_langcode;

  // Load correct translation and revision.
  if ($entity
    ->hasTranslation($target_langcode)) {
    $entity = $entity
      ->getTranslation($target_langcode);
  }
  $entity_type_id = $entity
    ->getEntityTypeId();
  $use_latest_revisions = $entity
    ->getEntityType()
    ->isRevisionable() && ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $entity
    ->bundle());
  $storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  if ($use_latest_revisions) {
    $entity = $storage
      ->load($entity
      ->id());
    $latest_revision_id = $storage
      ->getLatestTranslationAffectedRevisionId($entity
      ->id(), $target_langcode);
    if ($latest_revision_id) {

      /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_revision */
      $latest_revision = $storage
        ->loadRevision($latest_revision_id);

      // Make sure we do not list removed translations, i.e. translations
      // that have been part of a default revision but no longer are.
      if (!$latest_revision
        ->wasDefaultRevision() || $entity
        ->hasTranslation($target_langcode)) {
        $entity = $latest_revision;
      }
    }
  }

  // Build translation operation links.

  /* @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */
  $handler = $this
    ->getEntityTypeManager()
    ->getHandler($entity
    ->getEntityTypeId(), 'translation');
  $target_language = $this->languageManager
    ->getLanguage($target_langcode);
  $options = [
    'language' => $target_language,
  ];
  $is_default_language = $entity
    ->getUntranslated()
    ->language()
    ->getId() === $target_langcode ? TRUE : FALSE;
  $links = [];
  if ($entity
    ->hasTranslation($target_langcode)) {

    // Build edit translation links.
    if ($entity
      ->access('update') && $entity
      ->getEntityType()
      ->hasLinkTemplate('edit-form')) {
      $links += [
        'edit' => [
          'title' => $this
            ->t('Edit'),
          'url' => $entity
            ->toUrl('edit-form'),
          'language' => $target_language,
        ],
      ];
    }
    elseif (!$is_default_language) {
      if ($this->translatorsModuleExists && $handler
        ->getTranslationAccess($entity, 'update', $target_langcode)
        ->isAllowed()) {
        $links += [
          'edit' => [
            'title' => $this
              ->t('Edit'),
            'url' => $entity
              ->toUrl('drupal:content-translation-edit', $options)
              ->setRouteParameter('language', $target_langcode),
          ],
        ];
      }
      else {
        if (!$this->translatorsModuleExists && $handler
          ->getTranslationAccess($entity, 'update')
          ->isAllowed()) {
          $links += [
            'edit' => [
              'title' => $this
                ->t('Edit'),
              'url' => $entity
                ->toUrl('drupal:content-translation-edit', $options)
                ->setRouteParameter('language', $target_langcode),
            ],
          ];
        }
      }
    }

    // Build delete translation links.
    if ($entity
      ->access('delete') && $entity
      ->getEntityType()
      ->hasLinkTemplate('delete-form')) {
      $links += [
        'delete' => [
          'title' => $this
            ->t('Delete'),
          'url' => $entity
            ->toUrl('delete-form'),
          'language' => $target_language,
        ],
      ];
    }
    elseif (!$is_default_language && \Drupal::service('content_translation.delete_access')
      ->checkAccess($entity)) {
      if ($this->translatorsModuleExists && $handler
        ->getTranslationAccess($entity, 'delete', $target_langcode)
        ->isAllowed()) {
        $links += [
          'delete' => [
            'title' => $this
              ->t('Delete'),
            'url' => $entity
              ->toUrl('drupal:content-translation-delete', $options)
              ->setRouteParameter('language', $target_langcode),
          ],
        ];
      }
      else {
        if (!$this->translatorsModuleExists && $handler
          ->getTranslationAccess($entity, 'delete')
          ->isAllowed()) {
          $links += [
            'delete' => [
              'title' => $this
                ->t('Delete'),
              'url' => $entity
                ->toUrl('drupal:content-translation-delete', $options)
                ->setRouteParameter('language', $target_langcode),
            ],
          ];
        }
      }
    }
  }
  elseif (!$entity
    ->hasTranslation($target_langcode) && $entity
    ->isTranslatable()) {
    $route_name = "entity.{$entity_type_id}.content_translation_add";
    $add_url = Url::fromRoute($route_name, [
      'source' => $source_langcode,
      'target' => $target_langcode,
      $entity_type_id => $entity
        ->id(),
    ]);
    if ($this->translatorsModuleExists && $handler
      ->getTranslationAccess($entity, 'create', $source_langcode, $target_langcode)
      ->isAllowed()) {
      $links += [
        'add' => [
          'url' => $add_url,
          'language' => $target_language,
          'title' => $this
            ->t('Add'),
        ],
      ];
    }
    elseif (!$this->translatorsModuleExists && $handler
      ->getTranslationAccess($entity, 'create')
      ->isAllowed()) {
      $links += [
        'add' => [
          'url' => $add_url,
          'language' => $target_language,
          'title' => $this
            ->t('Add'),
        ],
      ];
    }
  }
  return $links;
}