Skip to content

Instantly share code, notes, and snippets.

@chx
Last active November 2, 2023 01:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chx/b065142ad40ab3ee110f9250ed240b9d to your computer and use it in GitHub Desktop.
Save chx/b065142ad40ab3ee110f9250ed240b9d to your computer and use it in GitHub Desktop.
<?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