How can I attach an image from an existing image field to another multi-value image field?

Drupal Answers Asked by esod on October 26, 2021

I have the nodes that have the source image field field_old_image_field and the destination image field, field_new_image_field. field_old_image_fieldhas only one image. field_new_image_fieldcan have multiple images and probably does. I need to copy the image from field_old_image_field to field_new_image_field without disturbing the images that are already in field_new_image_field.

Here’s my code so far:

foreach ($nodes as $node) {  
    if ($node->hasField('field_old_image_field') && !empty($node->field_old_image_field->target_id)) {
      $image = $node->get('field_old_image_field');
      $image_uri = $image->entity->getFileUri();
      $image_alt = $image[0]->get('alt')->getString();
      $image_title = $image[0]->get('title')->getString();

        'target_id' => $image,
        'alt' => $image_alt,
        'title' => $image_title,


But when I run this code in an update hook, I get

In EntityReference.php line 106:

Value is not a valid entity.

I was getting it to work earlier, but I wasn’t getting the alt text or the title text copied over. What’s wrong with this code and is there an easier way to do this?

One Answer

The first line doesn't get you an image, it's a field item list:

$items = $node->get('field_old_image_field');

Then you can get the first item

$item = $items[0];

and append the item properties to the new field:

  'target_id' => $item->target_id,
  'alt' => $item->alt,
  'title' => $item->title,

Answered by 4k4 on October 26, 2021

Add your own answers!

Related Questions

Social Media Embed Problems With the URL Embed module

0  Asked on November 30, 2021 by mckinzie25


Contextual links for node blocks

3  Asked on November 28, 2021


Set node to flagged by default

1  Asked on November 28, 2021 by andrew-morris


How do I replace drush_set_error()?

1  Asked on November 28, 2021


Get file ID and path to use in View template

1  Asked on November 25, 2021 by srodrig


How to use drupal_add_library with a module?

2  Asked on November 25, 2021


Ajax Form Not Working in some Some Regions

2  Asked on November 23, 2021 by sandesh-yadav


Facebook Album fetcher

1  Asked on November 23, 2021 by rajat-goyal


Condition on current moderation state

2  Asked on November 23, 2021 by paul-cappucci


how to apply a css code for a specific screen size

1  Asked on November 23, 2021 by coding-mv


hook_entity_predelete never called

0  Asked on November 23, 2021


User reference and Views 3 and Panels 3

1  Asked on November 21, 2021


MetaTag with JSON API won’t serialize

2  Asked on November 21, 2021 by johnventions


How to disable Rich Text Editor in image field caption ?

1  Asked on November 19, 2021 by lee-woodman


search block form not accepting special characters in drupal 7

1  Asked on November 19, 2021 by blueztechz


Unknown engine type outputformat

1  Asked on November 19, 2021 by gilles9999


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP