-
-
Save chx/b065142ad40ab3ee110f9250ed240b9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function __construct(array $values, $entity_type) { | |
$class = self::getDecoratedClass($entity_type) | |
$this->subject = new $class($values, $entity_type); | |
} | |
protected static function getDecoratedClass(string $entity_type = NULL): string { | |
return \Drupal::entityTypeManager() | |
->getDefinition($entity_type ?? self::ENTITY_TYPE) | |
->get(__CLASS__); | |
} | |
protected static function translationHelper(ContentEntityInterface $original_translation) { | |
$translation = new self([], $original_translation->getEntityTypeId()); | |
foreach ($original_translation->toArray() as $field_name => $lists) { | |
$item_list = $translation->$field_name; | |
foreach ($lists as $delta => $values) { | |
$item_list->set($delta, $values); | |
} | |
} | |
return $translation; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getTranslation($langcode) { | |
return self::translationHelper($this->subject->getTranslation($langcode)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getUntranslated() { | |
return self::translationHelper($this->subject->getUntranslated()); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function addTranslation($langcode, array $values = []) { | |
return self::translationHelper($this->subject->addTranslation($langcode, $values)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function load($id) { | |
$class = static::getDecoratedClass(); | |
return $class::load($id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment