Problem/Motivation

When you try to import files and you have the option "Autocreate entity" turned on for it, the import fails with the following message:

Missing bundle for entity type file

For the file target, files are already get created automatically, so the option has no use for it. It does disrupt the import process instead, because the file target is implemented a bit differently from other entity reference targets.

Steps to reproduce

  1. Install modules Node, File, Feeds.
  2. Create a content type with a file field.
  3. Create a feed type that imports nodes for this content type.
  4. On the mapping page, map to the file field, click the wheel icon in the "Configure" column for this target, and enable the "Autocreate entity" option.
  5. Add a feed and import an item that has a url to a file.

After import, something like the following messages appear:

Feed has been created.
Missing bundle for entity type file
Feed: Failed importing 1 Article.

Proposed resolution

Make sure that the option "Autocreate entity" is not available for the file target. I propose to do that as follows:

  1. Add a method called hasAutocreateSupport() to EntityReference.php to check if autocreate is possible, for now let that method return TRUE.
  2. Override the method in File.php and return FALSE there.
  3. Adjust EntityReference.php so that the the autocreate option is only defined/checked when that method returns TRUE: update findEntities(), defaultConfiguration(), buildConfigurationForm() and getSummary(). Call hasAutocreateSupport() from there.

Remaining tasks

  • Implement the proposed solution.
  • If unit tests fail after implementing the solution, check if the tests either need to be updated or that a new bug is introduced.
  • Update tests if needed.

User interface changes

The option "Autocreate entity" is no longer visible in the user interface for the file target.

Introduced terminology

API changes

The EntityReference class gets a new method which child classes could override.

Data model changes

The option "autocreate" in existing saved feed types for the file target will be ignored.

Original report by irinaz

I created CSV Upload import feed for content type Article (http://dev-feeds8.pantheonsite.io/admin/structure/feeds/manage/article_i...)

Images for article are downloaded from url and need to be Autocreated. When I run import, new images are created as temporary files and I get the following warning, and import of those articles is skipped.

field_image.0.target_id: This entity (file: 17) cannot be referenced.
field_image.0: You do not have access to the referenced entity (file: 17).

In case when I reference images by filename and these files are already in the system, I do not have this problem and images are correctly attached to articles.

There was a module https://www.drupal.org/project/view_unpublished that allows to reference unpublished node, but I cannot find anything similar for temporary images.

Thanks in advance for looking into this issue.

Issue fork feeds-2973170

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

irinaz created an issue. See original summary.

megachriz’s picture

Title: CSV import creates temporary images that cannot be referenced » Importing new files fails when "Autocreate entity" option is turned on.
StatusFileSize
new16.7 KB

This issue seems to be caused by the "Autocreate entity" setting. This setting was made for term reference targets - and apparently has a negative effect on file reference targets when turned on.

The easiest way to fix this issue is by removing that setting for the file target.

There is however a chance that the issue also exists for other entity reference targets.

kobb’s picture

FYI: Changing "Reference by" to File ID works. Images are imported.

However, after importing ~500 nodes, the images do not show in the media library. If you remove the image manually and add it back, it includes it in the library.

Is there a trick that will include these images in the media library? It is a shame not to be able to reuse them.

megachriz’s picture

@kobb
I haven't worked with the Media module in D8 yet, so I'm not sure what the Media module does to get images in their library. It at least sounds like that the Media module only acts on images uploaded in the UI, and not when adding images programmatically. So this could be a bug in the Media module. It could also be that Feeds ignores a particular setting that it shouldn't ignore.

kobb’s picture

StatusFileSize
new10.45 KB

Ah, it looks like the media item needs a Media Name and the "save to library" option checked before it shows.

save to media library checkbox

megachriz’s picture

@kobb
Hm, that sounds like that a media image field (if that is what it is), needs mapping options. There is no specific support for the Media module in Feeds yet, but getting that is discussed/handled in #2928904: Add a mapping target to media field.

megachriz’s picture

Issue tags: +Feeds file target

There are more file target related issues. Tagging this with “Feeds file target”.

agilman’s picture

Has anybody had any issues with files not being saved to the entity itself?

(Drupal 8) I have a feed (CSV upload) that's attached to a media type. The media type has a file field that maps to a PDF document on a separate publicly available webserver. There are plain text and taxonomy fields, too.

The feed import process runs successfully and creates the media entity. The text and taxonomy fields are populated, but the document file is not attached.

Any ideas?

megachriz’s picture

@agilman
Unfortunately, I haven’t found the time yet to look into Media support in the Feeds module. None of my clients use the Media module yet (for their purpose the standard file/image fields work fine) and in my free time I’m already busy with other Feeds related tasks (Drupal 9 support, multilingual support and the Feeds Migrate project), so I’m probably not able to address this soon.

The issue for media support is #2928904: Add a mapping target to media field.

agilman’s picture

@MegaChriz - Appreciate the response! It turns out the culprit was the media_feeds module. I uninstalled it and issue solved.

inst’s picture

Hi,
could you describe how to import media images with image-alt-text and image-titel to the media library?

thanks!

loopy1492’s picture

#3 worked for me for some reason. I quite frankly do not understand why because the naming of these is so weird. Why not call it "source url" or something?

mlhyyl’s picture

Somehow #3 did the trick for me, too.

raveendrab’s picture

Importing files seems to be not working. Tried on a clean site by mapping text fields and one image field. Text is imported properly, images are missing.

raveendrab’s picture

Tried on drupal 9.2.7.

raveendrab’s picture

Working with drupal 8.x. Not sure whether the image field has been associated with media module.

hockey2112’s picture

#3 fixed my issue. The file field content (PDF docs from the old web server) were not importing, with the errors "You do not have access to the referenced entity" "target_id: This entity ... cannot be referenced". I changed the "Reference by:" setting to "File ID", and that worked.

megachriz’s picture

Issue summary: View changes
Issue tags: +Novice

I updated the issue summary so that this issue can be picked up by others.

ad0z’s picture

Assigned: Unassigned » ad0z
ad0z’s picture

megachriz’s picture

Status: Active » Needs work

@ad0z
Thanks for your contribution! I think that you almost got it right. In EntityReference::defaultConfiguration() the autocreate options are still defined (see the first lines of the method).

ad0z’s picture

Status: Needs work » Needs review

@megachriz

Ohh sorry, missed that. Fixed + updated tests to check if autocreate option is available in form.

ad0z’s picture

Assigned: ad0z » Unassigned

  • megachriz committed ea64d3b8 on 8.x-3.x authored by ad0z
    Issue #2973170 by ad0z, megachriz, irinaz: Removed "Autocreate entity"...
megachriz’s picture

Status: Needs review » Fixed

Thanks ad0z for your contribution! I made a few more changes and I merged the code.

Status: Fixed » Closed (fixed)

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