4

How do I in code, create a new field value, add it to a content entity, and then have it saved to the DB?

Field API is dead. Long live Entity Field API! clearly shows how to read values, but doesn't mention inserts/updates.

0

2 Answers 2

4

I'm not sure what you mean with "field value", do you mean field items with multiple values?

It's the same as reading them, nothing special:

// Those are identical, not specifying [0] results in that using by default.
$entity->some_field->value = 'Whatever';
$entity->some_field[0]->value = 'Whatever';

// To add a value for the second item:
$entity->some_field[1]->value = 'Something';

// And persist to DB:
$entity->save();

Edit, the following script works for me and gives me the following output:

<?php
use Drupal\node\Entity\Node;

$node = Node::create(array('type' => 'article'));
$node->field_tags->target_id = 5;
var_dump($node->field_tags->getValue());

$node->field_tags[] = 6;
$node->field_tags[] = array('target_id' => 7);
var_dump($node->field_tags->getValue());

unset($node->field_tags[1]);
var_dump($node->field_tags->getValue());

Output:

array(1) {
  [0] =>
  array(1) {
    'target_id' =>
    int(5)
  }
}
array(3) {
  [0] =>
  array(1) {
    'target_id' =>
    int(5)
  }
  [1] =>
  array(1) {
    'target_id' =>
    int(6)
  }
  [2] =>
  array(1) {
    'target_id' =>
    int(7)
  }
}
array(2) {
  [0] =>
  array(1) {
    'target_id' =>
    int(5)
  }
  [2] =>
  array(1) {
    'target_id' =>
    int(7)
  }
}

The delta is off then in the last example, but this will probably be fixed in https://www.drupal.org/node/2164601, which might also affect how the code above works..

7
  • I assumed that I needed to created an appropriate entity class instance of some kind? Some sort of extension of TypedData? FWIW, In my particular example I'm trying to add an entity reference.
    – Letharion
    Commented Nov 4, 2014 at 20:48
  • If you have an entity that has a field then no, you do not have to do anything special. The only different for an entity reference is that you have something like ->target_id = 5; instead of ->value.
    – Berdir
    Commented Nov 4, 2014 at 21:03
  • That seems too easy! ;) I'll try tomorrow, thank you very much. :)
    – Letharion
    Commented Nov 4, 2014 at 21:04
  • It is easy. There is a lot of magic behind the scenes to make it look that easy, but that is not relevant for you.
    – Berdir
    Commented Nov 4, 2014 at 21:11
  • I took the liberty of adding a call to $entity->save(), which I needed to persist the change. I assume this is the correct way to do it. One minor question, this solves updating existing info, but what about "add another"? Should one do $entity->$field[count($field)]->value, or is there a shortcut for this?
    – Letharion
    Commented Nov 5, 2014 at 18:43
1

Here's what I've ended up doing. In my examples, I'm adding and removing an entity reference.

How to add a new field value:

// Insert a new field value:
$field = 'field_name';
$fieldItems = $entity->$field->getValue();

$entity->$field->setValue(
  array_merge(
    $entity->$field->getValue(),
    array($target_entity)
  )
);
$entity->save();

I found no way of removing field items directly, so instead I overwrite all of them.

// Removing a field value. In this example, an entity reference.
foreach ($fieldItems as $delta => $fieldItem) {
  if ($fieldItem['target_id'] === $target_entity_id) {
    unset($fieldItems[$delta]);
    $entity->$field->setValue($fieldItems);
    $entity->save();
    break;
  }
}
4
  • unset($entity->field_name[$delta)) works fine, you don't need to set the values again.
    – Berdir
    Commented Nov 7, 2014 at 19:05
  • @Berdir I actually tried that, I couldn't get it to work. IIRC the $delta's weren't what I expected, which was the source of the problem. I'll try again and report back. :)
    – Letharion
    Commented Nov 7, 2014 at 19:56
  • I added a working script that I tested in my answer.
    – Berdir
    Commented Nov 8, 2014 at 10:18
  • I think we're supposed to set these to NULL as per Set entity values to NULL instead of using unset() method: unset() is misleading.
    – colan
    Commented Feb 28, 2017 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.