entityType = $entity_type; $this->setUp(); // Set initial values. foreach ($values as $key => $value) { $this->$key = $value; } } /** * Sets up the object instance on construction or unserialization. */ protected function setUp() { $this->entityInfo = entity_get_info($this->entityType); $this->idKey = $this->entityInfo['entity keys']['id']; $this->bundleKey = isset($this->entityInfo['entity keys']['bundle']) ? $this->entityInfo['entity keys']['bundle'] : NULL; } /** * Implements EntityInterface::id(). */ public function id() { return isset($this->{$this->idKey}) ? $this->{$this->idKey} : NULL; } /** * Implements EntityInterface::isNew(). */ public function isNew() { // We support creating entities with pre-defined IDs to ease migrations. // For that the "is_new" property may be set to TRUE. return !empty($this->is_new) || empty($this->{$this->idKey}); } /** * Implements EntityInterface::entityType(). */ public function entityType() { return $this->entityType; } /** * Implements EntityInterface::bundle(). */ public function bundle() { return isset($this->bundleKey) ? $this->{$this->bundleKey} : $this->entityType; } /** * Implements EntityInterface::label(). * * @see entity_label() */ public function label() { $label = FALSE; if (isset($this->entityInfo['label callback']) && function_exists($this->entityInfo['label callback'])) { $label = $this->entityInfo['label callback']($this->entityType, $this); } elseif (!empty($this->entityInfo['entity keys']['label']) && isset($this->{$this->entityInfo['entity keys']['label']})) { $label = $this->{$this->entityInfo['entity keys']['label']}; } return $label; } /** * Implements EntityInterface::uri(). * * @see entity_uri() */ public function uri() { $bundle = $this->bundle(); // A bundle-specific callback takes precedence over the generic one for the // entity type. if (isset($this->entityInfo['bundles'][$bundle]['uri callback'])) { $uri_callback = $this->entityInfo['bundles'][$bundle]['uri callback']; } elseif (isset($this->entityInfo['uri callback'])) { $uri_callback = $this->entityInfo['uri callback']; } else { return NULL; } // Invoke the callback to get the URI. If there is no callback, return NULL. if (isset($uri_callback) && function_exists($uri_callback)) { $uri = $uri_callback($this); // Pass the entity data to url() so that alter functions do not need to // look up this entity again. $uri['options']['entity_type'] = $this->entityType; $uri['options']['entity'] = $this; return $uri; } } /** * Implements EntityInterface::save(). */ public function save() { return entity_get_controller($this->entityType)->save($this); } /** * Implements EntityInterface::delete(). */ public function delete() { if (!$this->isNew()) { entity_get_controller($this->entityType)->delete(array($this->id())); } } /** * Implements EntityInterface::createDuplicate(). */ public function createDuplicate() { $duplicate = clone $this; $duplicate->{$this->idKey} = NULL; return $duplicate; } /** * Implements EntityInterface::entityInfo(). */ public function entityInfo() { return $this->entityInfo; } /** * Serializes only what is necessary. * * See @link http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.sleep PHP Magic Methods @endlink. */ public function __sleep() { $vars = get_object_vars($this); unset($vars['entityInfo'], $vars['idKey'], $vars['bundleKey']); // Also key the returned array with the variable names so the method may // be easily overridden and customized. return drupal_map_assoc(array_keys($vars)); } /** * Invokes setUp() on unserialization. * * See @link http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.sleep PHP Magic Methods @endlink */ public function __wakeup() { $this->setUp(); } }