You are here

public function LingotekManagementFormBase::changeProfile in Lingotek Translation 4.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  2. 3.0.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  3. 3.1.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  4. 3.2.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  5. 3.3.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  6. 3.4.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  7. 3.5.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  8. 3.6.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  9. 3.7.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()
  10. 3.8.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::changeProfile()

Change Translation Profile.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.

File

src/Form/LingotekManagementFormBase.php, line 1552

Class

LingotekManagementFormBase
Form for bulk management of content.

Namespace

Drupal\lingotek\Form

Code

public function changeProfile(ContentEntityInterface $entity, $profile_id = NULL, $job_id = NULL, &$context = NULL) {
  $context['message'] = $this
    ->t('Changing Translation Profile for @type %label.', [
    '@type' => $entity
      ->getEntityType()
      ->getLabel(),
    '%label' => $entity
      ->label(),
  ]);
  $bundleInfos = $this->entityTypeBundleInfo
    ->getBundleInfo($entity
    ->getEntityTypeId());
  if (!$entity
    ->getEntityType()
    ->isTranslatable() || !$bundleInfos[$entity
    ->bundle()]['translatable']) {
    \Drupal::messenger()
      ->addWarning(t('Cannot change profile for @type %label. That @bundle_label is not enabled for translation.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
      '@bundle_label' => $entity
        ->getEntityType()
        ->getBundleLabel(),
    ]));
    return;
  }
  if (!$this->lingotekConfiguration
    ->isEnabled($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    $this
      ->messenger()
      ->addWarning(t('Cannot change profile for @type %label. That @bundle_label is not enabled for Lingotek translation.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
      '@bundle_label' => $entity
        ->getEntityType()
        ->getBundleLabel(),
    ]));
    return;
  }
  try {
    $this->lingotekConfiguration
      ->setProfile($entity, $profile_id, TRUE);
    if ($profile_id === Lingotek::PROFILE_DISABLED) {
      $this->translationService
        ->setSourceStatus($entity, Lingotek::STATUS_DISABLED);
      $this->translationService
        ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
    }
    elseif ($this->translationService
      ->getSourceStatus($entity) === Lingotek::STATUS_DISABLED) {
      if ($this->translationService
        ->getDocumentId($entity) !== NULL) {
        $this->translationService
          ->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
      }
      else {
        $this->translationService
          ->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
      }
      $this->translationService
        ->checkTargetStatuses($entity);
    }
  } catch (LingotekApiException $exception) {
    $this
      ->messenger()
      ->addError(t('The Tranlsation Profile change for @entity_type %title failed. Please try again.', [
      '@entity_type' => $entity
        ->getEntityTypeId(),
      '%title' => $entity
        ->label(),
    ]));
  }
}