You are here

class LingotekConfigSubscriber in Lingotek Translation 8

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

Updates config Lingotek translation status when saved.

Hierarchy

  • class \Drupal\lingotek\EventSubscriber\LingotekConfigSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LingotekConfigSubscriber

1 string reference to 'LingotekConfigSubscriber'
lingotek.services.yml in ./lingotek.services.yml
lingotek.services.yml
1 service uses LingotekConfigSubscriber
lingotek.config_subscriber in ./lingotek.services.yml
Drupal\lingotek\EventSubscriber\LingotekConfigSubscriber

File

src/EventSubscriber/LingotekConfigSubscriber.php, line 21
Contains \Drupal\lingotek\EventSubscriber\LingotekConfigSubscriber.

Namespace

Drupal\lingotek\EventSubscriber
View source
class LingotekConfigSubscriber implements EventSubscriberInterface {

  /**
   * The Lingotek content translation service.
   *
   * @var \Drupal\lingotek\LingotekConfigTranslationServiceInterface $translation_service
   */
  protected $translationService;

  /**
   * The mapper manager.
   *
   * @var \Drupal\config_translation\ConfigMapperManagerInterface
   */
  protected $mapperManager;

  /**
   * A array of configuration mapper instances.
   *
   * @var \Drupal\config_translation\ConfigMapperInterface[]
   */
  protected $mappers;

  /**
   * Constructs a LingotekConfigSubscriber.
   *
   * @param \Drupal\lingotek\LingotekConfigTranslationServiceInterface $translation_service
   *   The Lingotek config translation service.
   * @param \Drupal\config_translation\ConfigMapperManagerInterface $mapper_manager
   *   The configuration mapper manager.
   */
  public function __construct(LingotekConfigTranslationServiceInterface $translation_service, ConfigMapperManagerInterface $mapper_manager) {
    $this->translationService = $translation_service;
    $this->mapperManager = $mapper_manager;
    $this->mappers = $mapper_manager
      ->getMappers();
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      ConfigEvents::SAVE => 'onConfigSave',
    ];
  }

  /**
   * Updates the configuration translation status when a configuration is saved.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The configuration event.
   */
  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 ($this->translationService
            ->getConfigDocumentId($mapper)) {
            $this->translationService
              ->setConfigSourceStatus($mapper, Lingotek::STATUS_EDITED);
            $this->translationService
              ->markConfigTranslationsAsDirty($mapper);
          }
        }
      }

      // 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')) {

          /** @var LingotekConfigurationServiceInterface $lingotek_config */
          $lingotek_config = \Drupal::service('lingotek.configuration');
          if ($lingotek_config
            ->isEnabled($entity_type_id, $bundle)) {
            $lingotek_config
              ->setEnabled($entity_type_id, $bundle, FALSE);
            $fields = $lingotek_config
              ->getFieldsLingotekEnabled($entity_type_id, $bundle);
            foreach ($fields as $field_name) {
              $lingotek_config
                ->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 LingotekConfigurationServiceInterface $lingotek_config */
          $lingotek_config = \Drupal::service('lingotek.configuration');
          $field_definition = \Drupal::entityManager()
            ->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 ($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();
      \Drupal::entityManager()
        ->clearCachedDefinitions();
      \Drupal::service('router.builder')
        ->rebuild();
      if (\Drupal::service('entity.definition_update_manager')
        ->needsUpdates()) {
        $entity_types = \Drupal::service('lingotek.configuration')
          ->getEnabledEntityTypes();
        foreach ($entity_types as $entity_type_id => $entity_type) {
          $storage_definitions = \Drupal::entityManager()
            ->getFieldStorageDefinitions($entity_type_id);
          $installed_storage_definitions = \Drupal::entityManager()
            ->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') {
              \Drupal::entityManager()
                ->onFieldStorageDefinitionCreate($storage_definition);
            }
          }
        }
      }
    }
  }
  protected function getMapperFromConfigName($name) {

    // ToDo: This is inefficient.
    foreach ($this->mappers as $mapper) {
      $names = $mapper
        ->getConfigNames();
      foreach ($names as $the_name) {
        if ($the_name === $name) {
          return $mapper;
        }
      }
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekConfigSubscriber::$mapperManager protected property The mapper manager.
LingotekConfigSubscriber::$mappers protected property A array of configuration mapper instances.
LingotekConfigSubscriber::$translationService protected property The Lingotek content translation service.
LingotekConfigSubscriber::getMapperFromConfigName protected function
LingotekConfigSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
LingotekConfigSubscriber::onConfigSave public function Updates the configuration translation status when a configuration is saved.
LingotekConfigSubscriber::__construct public function Constructs a LingotekConfigSubscriber.