You are here

public function ConfigEntityHandler::onParseCdf in Acquia Content Hub 8.2

Parses the CDF representation of Configuration Entities.

Throws

\Exception

File

src/EventSubscriber/Cdf/ConfigEntityHandler.php, line 117

Class

ConfigEntityHandler
The Configuration entity CDF creator.

Namespace

Drupal\acquia_contenthub\EventSubscriber\Cdf

Code

public function onParseCdf(ParseCdfEntityEvent $event) {
  $cdf = $event
    ->getCdf();
  if ($cdf
    ->getType() !== 'drupal8_config_entity') {

    // Bail early if this isn't a config entity.
    return;
  }
  if (!$event
    ->isMutable()) {
    return;
  }
  $default_langcode = $cdf
    ->getMetadata()['default_language'];
  $data = Yaml::decode(base64_decode($cdf
    ->getMetadata()['data']));
  $default_values = $data[$default_langcode];
  $entity_type_id = $cdf
    ->getAttribute('entity_type')
    ->getValue()['und'];
  if ($entity_type_id === 'configurable_language') {
    $default_values['id'] = $this
      ->removeChannelId($default_values['id']);
  }

  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
  $entity = $this
    ->getEntityWithValues($entity_type_id, $default_values);
  if ($event
    ->hasEntity()) {
    $entity
      ->enforceIsNew(FALSE);
  }
  $event
    ->setEntity($entity);
  if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {

    /** @var \Drupal\Core\Config\Entity\ConfigEntityType $entity_type */
    $entity_type = $entity
      ->getEntityType();
    $config_name = $entity_type
      ->getConfigPrefix() . '.' . $entity
      ->get($entity_type
      ->getKey('id'));
    foreach ($data as $langcode => $language_override) {
      if ($langcode === $default_langcode) {
        continue;
      }
      if (empty($language_override)) {
        continue;
      }

      // Add language override for a language via Language Manager.
      $this->languageManager
        ->getLanguageConfigOverride($langcode, $config_name)
        ->setData($language_override)
        ->save();
    }
  }
}