I'm writing a custom module to pull in some images from an old D6 installation (the cck plugin is failing to connect images to the correct node). So I need to be able to create new ImageItem and add them to the field.

Can I simply create a new object like:

$field = $node->field_gallery;
$img = new ImageItem();
   "target_id" => 123,
    "alt" => "Alt text"
   "title" => "Title text" 

$field->set($delta, $img);

Or do I need to do something with entityManager to get it linked properly?

1 Answer 1


You don't need any of that. You just set the values, everything else will be taken care of for you.

$node->set('field_gallery', [
  'target_id' => 123,
  'alt' => 'Alt text'
  'title' => 'Title text' 

If you want to have multiple items, use `$node->field_gallery->appendItem($values);


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.