Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions entity_decorator.module
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class EntityDecoratorUnsupportedArgument extends EntityDecoratorException {}

class EntityDecoratorFieldNotFound extends EntityDecoratorException {}

class EntityDecoratorInvalidEntity extends EntityDecoratorException {}

abstract class EntityDecorator {
static $entityType;
static $bundle;
Expand Down Expand Up @@ -57,6 +59,14 @@ abstract class EntityDecorator {
*/
static public function buildFromEntity($entity) {
$class = get_called_class();

list(, , $bundle) = entity_extract_ids($class::$entityType, $entity);

if ($bundle != $class::$bundle) {
// Entity bundle doesn't match class-defined bundle.
throw new EntityDecoratorInvalidEntity($class . " can only be instantiated from an entity of type '" . $class::$entityType . "' and bundle '" . $class::$bundle . "'.");
}

$object = new $class($entity);
return $object;
}
Expand Down