1

I need to clear/remove image (or file) field from specific node.
I have before:
before

I want to get after code execution for the field:
after

What code should I use to properly clear image (file) field data?

  • why it's necessary to remove the image programmatically? You can remove it by clicking "Remove" button. Can you provide more information because it's possible to do it in multiple ways? – Ales Sep 12 at 21:36
  • My specific case is repairing site with many thousands nodes with broken images within several groups of nodes. I can filter these nodes but it's too tedious to click Remove for each such node. – Dimetry Sep 13 at 7:52
  • I think in this case would better to use hook_update and then go through all the nodes, check if the node field image source exists (file_exists (URL)), if doesn't exist you will have to remove the file entity itself + data of the field which is used on the node. – Ales Sep 13 at 8:20
3
+50

I assume that your field accepts only one file at a time. This is the code you need to use:

// Replace this with the ID of the node you want to update.
$node_id = 123;

/** @var \Drupal\node\NodeInterface $node */
$node = \Drupal\node\Entity\Node::load($node_id);

// Step 1: remove the file. This will actually delete the file from the
// filesystem, so in case you want to keep it just remove the next two
// lines.
/** @var \Drupal\file\FileInterface $file */
$file = $node->get('field_NAME_OF_YOUR_FIELD')->entity;
$file->delete();

// Step 2: remove the field data.
// The 0 in this case is the index of the item you want to remove. It simply
// means the first value in that field needs to be removed.
$node->get('field_NAME_OF_YOUR_FIELD')->removeItem(0);
// We have to save the changes to the node.
$node->save();
| improve this answer | |
1

Just to offer an alternate solution

$nid = 123;

// Load the node (EDIT:) while checking if it exists
if(!$node = \Drupal::service('entity_type.manager')
  ->getStorage('node')
  ->load($nid)) {
  // No results for the $nid, bail out early
  return;
}

// EDIT: To make sure there's a value on the field
if(!$node->hasField('field_name') || $node->get('field_name')->isEmpty(){
  return;
}

// Grab the file entity and make it temporary.
// Deletion will be handled by cron.
$file = $node->get('field_name')
  ->entity
  ->set('status', 0)
  ->save();

// Unset the value on the file ref field and save
$node->set('field_name', '')
  ->save()
| improve this answer | |
0

A solution for multiple files (also good for avoiding errors when the field is empty):

$nid = 123;

$node = \Drupal\node\Entity\Node::load($nid);

// Grab the file entities and make them temporary.
// Deletion should be handled by cron.
$items = $node->get('field_NAME_OF_YOUR_FIELD');
foreach ($items as $item) {
  $file = $item->entity;
  $file->set('status', 0);
  $file->save();
}

// Unset the values on the file ref field and save
$node->set('field_NAME_OF_YOUR_FIELD', []);
$node->save();

This code is based on @Mrweiner great answer.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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