1

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();
$img->setValue(array(
   "target_id" => 123,
    "alt" => "Alt text"
   "title" => "Title text" 
));

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

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

1 Answer 1

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);

0

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.