0

This question is a follow-on from this one about creating managed files. If you look at the question in the link, you'll see that I am writing a drush command to detect all the inline-images in a just-migrated Drupal 8 system, and add them into Drupal as managed images attached to the relevant nodes. To cut a long story short, this bit of code takes the existing file name and creates an entry in Drupal:

$file = entity_create('file');
$file->setFilename($file_name);
$file->setFileUri('public://images/'.$file_name);
$file->save();

This works fine, except that it very irritatingly displays this error message with every file: Object(Drupal\Core\Entity\Plugin\DataType\EntityAdapter).uri: The file <em class="placeholder">public://images/rint135_graph4_0.articleimage.PNG</em> already exists. Enter a unique file URI.

Of course the file exists! That is precisely the point! Despite the error message everything seems to work ok, but I would really like to get rid of those error messages. I guess that they are being set by drupal_set_message(), since they include HTML which of course is pointless in drush - but I can't find any way to switch them off.

1 Answer 1

0

Two options:

  1. Hide the messages with ob_start() and ob_end().

  2. Avoid the messages by moving the target file out of the way, and moving it back again after saving the entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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