You are here

class EntityLanguage in Acquia Content Hub 8.2

Language and default_language handling code.

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField\EntityLanguage implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EntityLanguage

1 string reference to 'EntityLanguage'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses EntityLanguage
language.field.cdf.unserializer in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField\EntityLanguage

File

src/EventSubscriber/UnserializeContentField/EntityLanguage.php, line 12

Namespace

Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField
View source
class EntityLanguage implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::UNSERIALIZE_CONTENT_ENTITY_FIELD] = [
      'onUnserializeContentField',
      100,
    ];
    return $events;
  }

  /**
   * Handles language fields and the default_langcode field values.
   *
   * This is actually a tricky bit of code that throws away language values
   * for the "langcode" entity type key when present because Drupal will do its
   * own determination of things like "default_langcode" if values are present
   * in that field. Rather than allow this, we throw it away and handle
   * defaults manually.
   *
   * @param \Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event
   *   The unserialize event.
   */
  public function onUnserializeContentField(UnserializeCdfEntityFieldEvent $event) {
    $entityType = $event
      ->getEntityType();
    if ($entityType
      ->hasKey('langcode')) {
      $fieldName = $event
        ->getFieldName();
      if ($event
        ->getFieldMetadata()['type'] === 'boolean' && $fieldName === 'default_langcode') {
        $field = $event
          ->getField();
        $values = [];
        foreach ($field['value'] as $langcode => $value) {
          $values[$langcode][$entityType
            ->getKey('langcode')] = $langcode;
        }
        $event
          ->setValue($values);
        $event
          ->stopPropagation();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityLanguage::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityLanguage::onUnserializeContentField public function Handles language fields and the default_langcode field values.