The fileEntityLoad function's code for creating files is: $file = file_save_data($output, 'public://' . $filename, FILE_EXISTS_RENAME);

This hardcoded path causes the files to be created in the root of the files directory regardless of config in the file field which places files into a subdirectory.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cameronbprince created an issue. See original summary.

cameron prince’s picture

cameron prince’s picture

Fixes missing $entity_type argument.

cameron prince’s picture

slucero’s picture

Version: 8.x-2.x-dev » 8.x-1.x-dev
Status: Active » Needs review
Issue tags: +Needs tests

Everything here looks good and seems to be working well. I'll mark this as needs review and hopefully we can get somebody else to confirm as well before committing.

Thanks @cameronbprince!

kedramon’s picture

Status: Needs review » Needs work

Hi guys,

just tested it, works good!
Only one notice this function throwParamError don't return anything.

cameron prince’s picture

As a test, I removed the images directory and ran the import. throwParamError() printed out the params in the error as expected.

@kedramon, I assume you are expecting a "return" in the throwParamError() function itself. This is not needed because that function, in turn, calls throw itself.

More info: https://stackoverflow.com/questions/11547656/why-do-some-people-return-a...

$ mv images/ images.old
$ drush @myalias yci profiles/custom/myprofile
file_get_contents(profiles/custom/myprofile/images/placeholder.jpg): failed to open stream: No such file or directory                [warning]
ContentLoader.php:547
Drupal\Core\TypedData\Exception\MissingDataException: Drupal\yaml_content\ContentLoader\ContentLoader: Unable to process file           [error]
content: [
  "entity_type" => image,
  'type' => 'module',
  'filename' => 'placeholder.jpg',
] in Drupal\yaml_content\ContentLoader\ContentLoader->throwParamError() (line 646 of
/modules/contrib/yaml_content/src/ContentLoader/ContentLoader.php).
cameron prince’s picture

Here is a re-roll and new code to check for the existence of a file entity with the same URI as the file being imported. This will prevent the files directory from filling with duplicates when your imported content references the same image multiple times.

jasonawant’s picture

A few questions about the check for existing files. For example, say we are using an image file that changes, but uses the same filename, what happens when checking the for the $file_uri? When we use the existing file, when actually want to use an updated image in the file?

// Check for an existing file entity for this file.
$query = \Drupal::entityQuery('file')
  ->condition('uri', $file_uri);
$existing_file = $query->execute();

If the desire is not create a duplicate, assuming the yaml_content is being processed again in effort to update the already created content, could we not use FILE_EXISTS_REPLACE with file_save_data()?

So, change

$file = file_save_data($output, $file_uri, FILE_EXISTS_RENAME);

with

$file = file_save_data($output, $file_uri, FILE_EXISTS_REPLACE);
cameron prince’s picture

@jasonawant, I originally believed that would result in a duplicate file object pointing to the same file. But now, after reviewing the file_save_data code, I see that existing files are loaded when that option is set, so that should be fine. I'll update the patch.

Thanks!

cameron prince’s picture

Another re-roll with FILE_EXISTS_REPLACE.

slucero’s picture

I've just committed this and it will be included in the next alpha release (8.x-1.x-alpha4).

Thanks for all the work everybody!

slucero’s picture

Adding to the release plan ticket to record which release it will be included in.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.