You are here

public function LingotekConfigSubscriber::onConfigSave in Lingotek Translation 8.2

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

Updates the configuration translation status when a configuration is saved.

Parameters

\Drupal\Core\Config\ConfigCrudEvent $event: The configuration event.

File

src/EventSubscriber/LingotekConfigSubscriber.php, line 117

Class

LingotekConfigSubscriber
Updates config Lingotek translation status when saved.

Namespace

Drupal\lingotek\EventSubscriber

Code

public function onConfigSave(ConfigCrudEvent $event) {
  if (!drupal_installation_attempted()) {
    $config = $event
      ->getConfig();
    if (!$config instanceof ConfigEntityInterface) {
      $name = $config
        ->getName();
      $mapper = $this
        ->getMapperFromConfigName($name);
      if ($mapper !== NULL) {
        if ($mapper instanceof ConfigEntityMapper) {
          $entity = $mapper
            ->getEntity();
          if ($this->translationService
            ->getDocumentId($entity)) {
            $this->translationService
              ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
            $this->translationService
              ->markTranslationsAsDirty($entity);
          }
        }
        else {
          if ($this->translationService
            ->getConfigDocumentId($mapper)) {
            $this->translationService
              ->setConfigSourceStatus($mapper, Lingotek::STATUS_EDITED);
            $this->translationService
              ->markConfigTranslationsAsDirty($mapper);
          }
        }

        /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_config */
        $lingotek_config = $this->lingotekConfiguration;
        $profile = $lingotek_config
          ->getConfigProfile($mapper
          ->getPluginId());
        if ($profile !== NULL && $profile
          ->id() === Lingotek::PROFILE_DISABLED) {
          $this->translationService
            ->setConfigSourceStatus($mapper, Lingotek::STATUS_DISABLED);
          $this->translationService
            ->setConfigTargetStatuses($mapper, Lingotek::STATUS_DISABLED);
        }
      }
    }

    // If there are changes on content translation settings, we need to react to
    // them in case the entity was enabled for Lingotek translation.
    if (0 === strpos($config
      ->getName(), 'language.content_settings.') && $event
      ->isChanged('third_party_settings.content_translation.enabled')) {
      $id = $config
        ->get('id');
      list($entity_type_id, $bundle) = explode('.', $id);
      if (!$config
        ->get('third_party_settings.content_translation.enabled')) {
        if ($this->lingotekConfiguration
          ->isEnabled($entity_type_id, $bundle)) {
          $this->lingotekConfiguration
            ->setEnabled($entity_type_id, $bundle, FALSE);
          $fields = $this->lingotekConfiguration
            ->getFieldsLingotekEnabled($entity_type_id, $bundle);
          foreach ($fields as $field_name) {
            $this->lingotekConfiguration
              ->setFieldLingotekEnabled($entity_type_id, $bundle, $field_name, FALSE);
          }
        }
      }
    }
    if (0 === strpos($config
      ->getName(), 'field.field.') && $event
      ->isChanged('translatable')) {
      $id = $config
        ->get('id');
      list($entity_type_id, $bundle, $field_name) = explode('.', $id);
      if (!$config
        ->get('translatable')) {

        /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_config */
        $lingotek_config = $this->lingotekConfiguration;
        $field_definition = $this->entityFieldManager
          ->getFieldDefinitions($entity_type_id, $bundle);

        // We need to make an exception for hosted entities. The field
        // reference may not be translatable, but we want to translate the
        // hosted entity. See https://www.drupal.org/node/2735121.
        if (isset($field_definition[$field_name]) && $field_definition[$field_name]
          ->getType() !== 'entity_reference_revisions' && $lingotek_config
          ->isFieldLingotekEnabled($entity_type_id, $bundle, $field_name)) {
          $lingotek_config
            ->setFieldLingotekEnabled($entity_type_id, $bundle, $field_name, FALSE);
        }
      }
    }
  }
  if ($event
    ->getConfig()
    ->getName() === 'lingotek.settings' && $event
    ->isChanged('translate.entity')) {
    drupal_static_reset();
    $this->entityFieldManager
      ->clearCachedFieldDefinitions();
    $this->entityTypeManager
      ->clearCachedDefinitions();
    \Drupal::service('router.builder')
      ->setRebuildNeeded();
    if (\Drupal::service('entity.definition_update_manager')
      ->needsUpdates()) {
      $entity_types = $this->lingotekConfiguration
        ->getEnabledEntityTypes();
      foreach ($entity_types as $entity_type_id => $entity_type) {
        $storage_definitions = $this->entityFieldManager
          ->getFieldStorageDefinitions($entity_type_id);
        $installed_storage_definitions = \Drupal::service('entity.last_installed_schema.repository')
          ->getLastInstalledFieldStorageDefinitions($entity_type_id);
        foreach (array_diff_key($storage_definitions, $installed_storage_definitions) as $storage_definition) {

          /** @var $storage_definition \Drupal\Core\Field\FieldStorageDefinitionInterface */
          if ($storage_definition
            ->getProvider() == 'lingotek') {
            $this->entityTypeManager
              ->onFieldStorageDefinitionCreate($storage_definition);
          }
        }
      }
    }
  }
}