You are here

class ContentTranslationManager in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/content_translation/src/ContentTranslationManager.php \Drupal\content_translation\ContentTranslationManager

Provides common functionality for content translation.

Hierarchy

Expanded class hierarchy of ContentTranslationManager

1 string reference to 'ContentTranslationManager'
content_translation.services.yml in core/modules/content_translation/content_translation.services.yml
core/modules/content_translation/content_translation.services.yml
1 service uses ContentTranslationManager
content_translation.manager in core/modules/content_translation/content_translation.services.yml
Drupal\content_translation\ContentTranslationManager

File

core/modules/content_translation/src/ContentTranslationManager.php, line 16
Contains \Drupal\content_translation\ContentTranslationManager.

Namespace

Drupal\content_translation
View source
class ContentTranslationManager implements ContentTranslationManagerInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The updates manager.
   *
   * @var \Drupal\content_translation\ContentTranslationUpdatesManager
   */
  protected $updatesManager;

  /**
   * Constructs a ContentTranslationManageAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $manager
   *   The entity type manager.
   * @param \Drupal\content_translation\ContentTranslationUpdatesManager $updates_manager
   *   The updates manager.
   */
  public function __construct(EntityManagerInterface $manager, ContentTranslationUpdatesManager $updates_manager) {
    $this->entityManager = $manager;
    $this->updatesManager = $updates_manager;
  }

  /**
   * {@inheritdoc}
   */
  function getTranslationHandler($entity_type_id) {
    return $this->entityManager
      ->getHandler($entity_type_id, 'translation');
  }

  /**
   * {@inheritdoc}
   */
  public function getTranslationMetadata(EntityInterface $translation) {

    // We need a new instance of the metadata handler wrapping each translation.
    $entity_type = $translation
      ->getEntityType();
    $class = $entity_type
      ->get('content_translation_metadata');
    return new $class($translation, $this
      ->getTranslationHandler($entity_type
      ->id()));
  }

  /**
   * {@inheritdoc}
   */
  public function isSupported($entity_type_id) {
    $entity_type = $this->entityManager
      ->getDefinition($entity_type_id);
    return $entity_type
      ->isTranslatable() && ($entity_type
      ->hasLinkTemplate('drupal:content-translation-overview') || $entity_type
      ->get('content_translation_ui_skip'));
  }

  /**
   * {@inheritdoc}
   */
  public function getSupportedEntityTypes() {
    $supported_types = array();
    foreach ($this->entityManager
      ->getDefinitions() as $entity_type_id => $entity_type) {
      if ($this
        ->isSupported($entity_type_id)) {
        $supported_types[$entity_type_id] = $entity_type;
      }
    }
    return $supported_types;
  }

  /**
   * {@inheritdoc}
   */
  public function setEnabled($entity_type_id, $bundle, $value) {
    $config = $this
      ->loadContentLanguageSettings($entity_type_id, $bundle);
    $config
      ->setThirdPartySetting('content_translation', 'enabled', $value)
      ->save();
    $entity_type = $this->entityManager
      ->getDefinition($entity_type_id);
    $this->updatesManager
      ->updateDefinitions(array(
      $entity_type_id => $entity_type,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled($entity_type_id, $bundle = NULL) {
    $enabled = FALSE;
    if ($this
      ->isSupported($entity_type_id)) {
      $bundles = !empty($bundle) ? array(
        $bundle,
      ) : array_keys($this->entityManager
        ->getBundleInfo($entity_type_id));
      foreach ($bundles as $bundle) {
        $config = $this
          ->loadContentLanguageSettings($entity_type_id, $bundle);
        if ($config
          ->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
          $enabled = TRUE;
          break;
        }
      }
    }
    return $enabled;
  }

  /**
   * Loads a content language config entity based on the entity type and bundle.
   *
   * @param string $entity_type_id
   *   ID of the entity type.
   * @param string $bundle
   *   Bundle name.
   *
   * @return \Drupal\language\Entity\ContentLanguageSettings
   *   The content language config entity if one exists. Otherwise, returns
   *   default values.
   */
  protected function loadContentLanguageSettings($entity_type_id, $bundle) {
    if ($entity_type_id == NULL || $bundle == NULL) {
      return NULL;
    }
    $config = $this->entityManager
      ->getStorage('language_content_settings')
      ->load($entity_type_id . '.' . $bundle);
    if ($config == NULL) {
      $config = $this->entityManager
        ->getStorage('language_content_settings')
        ->create([
        'target_entity_type_id' => $entity_type_id,
        'target_bundle' => $bundle,
      ]);
    }
    return $config;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentTranslationManager::$entityManager protected property The entity type manager.
ContentTranslationManager::$updatesManager protected property The updates manager.
ContentTranslationManager::getSupportedEntityTypes public function Gets the entity types that support content translation. Overrides ContentTranslationManagerInterface::getSupportedEntityTypes
ContentTranslationManager::getTranslationHandler function Returns an instance of the Content translation handler. Overrides ContentTranslationManagerInterface::getTranslationHandler
ContentTranslationManager::getTranslationMetadata public function Returns an instance of the Content translation metadata. Overrides ContentTranslationManagerInterface::getTranslationMetadata
ContentTranslationManager::isEnabled public function Determines whether the given entity type is translatable. Overrides ContentTranslationManagerInterface::isEnabled
ContentTranslationManager::isSupported public function Checks whether an entity type supports translation. Overrides ContentTranslationManagerInterface::isSupported
ContentTranslationManager::loadContentLanguageSettings protected function Loads a content language config entity based on the entity type and bundle.
ContentTranslationManager::setEnabled public function Sets the value for translatability of the given entity type bundle. Overrides ContentTranslationManagerInterface::setEnabled
ContentTranslationManager::__construct public function Constructs a ContentTranslationManageAccessCheck object.