getTranslationAccess example

      if (!$new_translation) {
        $weight = 100;
        foreach (['delete', 'submit'] as $key) {
          if (isset($form['actions'][$key]['weight'])) {
            $weight = $form['actions'][$key]['weight'];
            break;
          }
        }
        /** @var \Drupal\Core\Access\AccessResultInterface $delete_access */
        $delete_access = \Drupal::service('content_translation.delete_access')->checkAccess($entity);
        $access = $delete_access->isAllowed() && (
          $this->getTranslationAccess($entity, 'delete')->isAllowed() ||
          ($entity->access('delete') && $this->entityType->hasLinkTemplate('delete-form'))
        );
        $form['actions']['delete_translation'] = [
          '#type' => 'submit',
          '#value' => t('Delete translation'),
          '#weight' => $weight,
          '#submit' => [[$this, 'entityFormDeleteTranslation']],
          '#access' => $access,
        ];
      }

      
$row_title = Link::fromTextAndUrl($label$link['url'])->toString();
          }

          if (empty($link['url'])) {
            $row_title = $is_original ? $label : $this->t('n/a');
          }

          // If the user is allowed to edit the entity we point the edit link to           // the entity form, otherwise if we are not dealing with the original           // language we point the link to the translation form.           $update_access = $entity->access('update', NULL, TRUE);
          $translation_access = $handler->getTranslationAccess($entity, 'update');
          $cacheability = $cacheability
            ->merge(CacheableMetadata::createFromObject($update_access))
            ->merge(CacheableMetadata::createFromObject($translation_access));
          if ($update_access->isAllowed() && $entity_type->hasLinkTemplate('edit-form')) {
            $links['edit']['url'] = $entity->toUrl('edit-form');
            $links['edit']['language'] = $language;
          }
          elseif (!$is_original && $translation_access->isAllowed()) {
            $links['edit']['url'] = $edit_url;
          }

          
/** @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */
          $handler = $this->entityTypeManager->getHandler($entity->getEntityTypeId(), 'translation');
          $translations = $entity->getTranslationLanguages();
          $languages = $this->languageManager->getLanguages();
          $source_language = $this->languageManager->getLanguage($source) ?: $entity->language();
          $target_language = $this->languageManager->getLanguage($target) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
          $is_new_translation = ($source_language->getId() != $target_language->getId()
            && isset($languages[$source_language->getId()])
            && isset($languages[$target_language->getId()])
            && !isset($translations[$target_language->getId()]));
          return AccessResult::allowedIf($is_new_translation)->cachePerPermissions()->addCacheableDependency($entity)
            ->andIf($handler->getTranslationAccess($entity$operation));

        case 'delete':
          // @todo Remove this in https://www.drupal.org/node/2945956.           /** @var \Drupal\Core\Access\AccessResultInterface $delete_access */
          $delete_access = \Drupal::service('content_translation.delete_access')->checkAccess($entity);
          $access = $this->checkAccess($entity$language$operation);
          return $delete_access->andIf($access);

        case 'update':
          return $this->checkAccess($entity$language$operation);
      }
    }
Home | Imprint | This part of the site doesn't use cookies.