How to copy data from one field to another in same content type using hook_update_N Trying to copy data from field A (plain text field) to field B (entity reference field with taxonomy) within the same content type. The taxonomy terms used in field B are the same terms used in old field (field A). 

Below is my first attempt in writing a hook for field copy (using hook_update_N) but its not working. 

function mymodule_update_8001() {
   $node_type = "test_content_type"; 
   $nidArray = db_query("SELECT nid FROM node WHERE type = '%s' ", $node_type);

  foreach ($nidArray as $nid) {
    $node = node_load($nid);
    $termString = $node-&gt;field_a;
    $termObject = taxonomy_term_load_multiple_by_name($termString);
    $node-&gt;field_b-&gt;target_id = $termObject-&gt;tid;
    $node-&gt;revision = TRUE;
    node_save($node);
  }
}


Any help on this would be greatly appreciated.
The code doesn't work because it uses node_save(), which is a function Drupal 8 doesn't implement. (If you click on Same name and namespace in other branches on the documentation page, you will notice there isn't a link for any Drupal 8.x version.)
In Drupal 8, where a $node is an instance of a class implementing EntityInterface, a node is saved with $node-&gt;save().
The code is also using functions that are still implemented by Drupal 8, but they are deprecated and they will be removed on Drupal 9 or future Drupal versions.

The documentation for node_load() suggests to instead use \Drupal\node\Entity\Node::load(), but other methods could be used, including the ones the entity_type.manager offers.

Instead of db_query(), the object returned from \Drupal\Core\Database\Database::getConnection() should be used; preferably, the database connection should be injected.

For entities, instead of directly querying its database table, the entity_type.manager service should be used to obtain an instance of the class that handles queries to that entity's database table

taxonomy_term_load_multiple_by_name() has not been marked as deprecated, and it's still present in Drupal 9, but the function it uses (entity_load_multiple_by_properties()) has been marked as deprecated. Preferably, the entity_type.manager service should be used also in this case.


The hook to implement isn't hook_update_N(). As the documentation for that hook says, loading, saving, or performing any other CRUD operation on an entity is never safe to do inside hook_update_N().
The correct hook to implement in this case is hook_post_update_NAME(), which receives the same arguments hook_update_N() gets. The expected return values are the same values expected from hook_update_N().
Since updating all the nodes could possibly cause PHP to time out, depending on the number of nodes the site has, the operation needs to be executed in batches, using the argument hook_post_update_NAME() gets.
The code I would use is the following one.
// Add the following lines at the begin of the mymodule.post_update.php file,
// where this hook is expected to be.
// (Replace mymodule with the machine name of the module you are using to
// implement the hook, in the hook name and in the name of the file containing
// it.) 
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;

function mymodule_post_update_update_taxonomy_references(&amp;$sandbox) {
  $content_type = 'test_content_type';
  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = \Drupal::entityTypeManager(); 
  /** @var \Drupal\Core\Entity\EntityStorageInterface $node_storage */
  $node_storage = $entity_type_manager-&gt;getStorage('node');
  /** @var \Drupal\Core\Entity\EntityStorageInterface $taxonomy_term_storage */
  $taxonomy_term_storage = $entity_type_manager-&gt;getStorage('taxonomy_term');
  if (!isset($sandbox['total'])) {
    $sandbox['current'] = 0;
    $sandbox['count'] = 0;
    $sandbox['total'] = $node_storage-&gt;getQuery()
      -&gt;accessCheck(FALSE)
      -&gt;condition('type', $content_type)
      -&gt;count()-&gt;execute();
  }
  if (!empty($sandbox['total'])) {
    $nids = $node_storage-&gt;getQuery()
      -&gt;accessCheck(FALSE)
      -&gt;pager(50)
      -&gt;condition('type', $content_type)
      -&gt;condition('nid', $sandbox['current'], '&gt;')
      -&gt;sort('nid', 'ASC')
      -&gt;execute();
    $nodes = $node_storage-&gt;loadMultiple($nids);
    /** @var \Drupal\node\NodeInterface $node */
    foreach ($nodes as $node) {
      $sandbox['current'] = $node-&gt;id();
      $sandbox['count']++;
      $taxonomy_terms = $taxonomy_term_storage-&gt;loadByProperties(['name' =&gt; $node-&gt;field_a]);
      if (!empty($taxonomy_terms) &amp;&amp; is_array($taxonomy_terms)) {
        /** @var \Drupal\taxonomy\TermInterface $taxonomy_term */
        $taxonomy_term = reset($taxonomy_terms);
        $node-&gt;field_b-&gt;entity = $taxonomy_term;
        $node-&gt;setNewRevision();
        $node-&gt;save();
      }
    }
  }
  $sandbox['#finished'] = empty($sandbox['total']) ? 1 : $sandbox['count'] / $sandbox['total'];
}