Currently, EntityWrapper::getValue() relies on entity_load() and returns NULL if the entity hasn't been saved yet. This should be considered a bug because we need this to work with unsaved entities as well e.g. when passing a freshly created node object before saving it.


public function getValue() {
  $source = $this->getIdSource();
  $id = $source ? $source->getValue() : $this->id;
  return $id ? entity_load($this->entityType, $id) : NULL;
}

Originally reported by EclipseGc (following snippet currently fails):


$node = entity_create('node', array('type' => 'page'));
$typed_data = typed_data()->create(
  array(
    'type' => 'entity',
    'constraints' => array(
      'EntityType' => 'node',
    ),
  ),
  $node
);

drupal_set_message('<pre>' . var_export($typed_data->getValue(), TRUE) . '</pre>');

Comments

fago’s picture

Let's make sure to consider this when doing #1868004: Improve the TypedData API usage of EntityNG.

fubhy’s picture

Issue summary: View changes

Updating issue summary.

fubhy’s picture

Title: EntityWrapper currently relies on entity_load() which causes ->getValue() to return NULL for unsaved entities. » EntityWrapper::getValue() currently doesn't work with unsaved entities.

Simplifying issue title.

eclipsegc’s picture

Does this seem saner?


  /**
   * Overrides \Drupal\Core\TypedData\TypedData::getValue().
   */
  public function getValue() {
    $source = $this->getIdSource();
    $id = $source ? $source->getValue() : $this->id;
    if ($id) {
      return entity_load($this->entityType, $id);
    }
    else {
      $info = entity_get_info($this->entityType);
      $values = array_fill_keys(array_values(array_filter($info['entity_keys'])), NULL);
      return entity_create($this->entityType, $values);
    }
  }

Eclipse

maciej.zgadzaj’s picture

Status: Active » Needs review
StatusFileSize
new1021 bytes

Rolled into patch. Ping testbot.

Status: Needs review » Needs work

The last submitted patch, drupal-getValue_on_unsaved_entities-1909666-4.patch, failed testing.

podarok’s picture

Status: Needs work » Needs review
StatusFileSize
new924 bytes

update
bot?

Status: Needs review » Needs work

The last submitted patch, 1909666-5.patch, failed testing.

podarok’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new923 bytes

woops

podarok’s picture

Status: Reviewed & tested by the community » Needs review

cross post

Status: Needs review » Needs work

The last submitted patch, 1909666-8.patch, failed testing.

podarok’s picture

Status: Needs work » Needs review
StatusFileSize
new2.46 KB

this should fix a few tests

Status: Needs review » Needs work

The last submitted patch, 1909666-11.patch, failed testing.

Anonymous’s picture

Issue summary: View changes

Updated issue summary.

berdir’s picture

Issue summary: View changes
Status: Needs work » Closed (duplicate)

I think this works fine now.

bkline’s picture

@berdir This issue was marked as a duplicate, but I can't find any identification of the other ticket of which this one was deemed to be a duplicate. Is it there and I just missed it?