You are here

protected function EntityStructureWrapper::setProperty in Entity API 7

Sets a property.

File

includes/entity.wrapper.inc, line 486
Provides wrappers allowing easy usage of the entity metadata.

Class

EntityStructureWrapper
Provides a general wrapper for any data structure. For this to work the metadata has to be passed during construction.

Code

protected function setProperty($name, $value) {
  $info = $this
    ->getPropertyInfo($name);
  if (!empty($info['setter callback'])) {
    $data = $this
      ->value();

    // In case the data structure is not set, support simple auto-creation
    // for arrays. Else an exception is thrown.
    if (!isset($data)) {
      if (!empty($this->info['auto creation']) && !$this instanceof EntityDrupalWrapper) {
        $data = $this->info['auto creation']($name, $this->info);
      }
      else {
        throw new EntityMetadataWrapperException('Unable to set the data property ' . check_plain($name) . ' as the parent data structure is not set.');
      }
    }

    // Invoke the setter callback for updating our data.
    $info['setter callback']($data, $name, $value, $this->langcode, $this->type, $info);

    // If the setter has not thrown any exceptions, proceed and apply the
    // update to the current and any parent wrappers as necessary.
    $data = $this->info['type'] == 'entity' ? $this : $data;
    $this
      ->set($data);

    // Clear the cache of properties dependent on this value.
    foreach ($info['clear'] as $name) {
      if (isset($this->cache[$name])) {
        $this->cache[$name]
          ->clear();
      }
    }
  }
  else {
    throw new EntityMetadataWrapperException('Entity property ' . check_plain($name) . " doesn't support writing.");
  }
}