0

I have a site with a lot of audio files (using core's Media audio media type).

On the Manage form display admin page (/admin/structure/media/manage/audio/form-fields), I have set the Name field to Disabled.

This way, when I add a media audio entity in the UI (at /media/add/audio), then I don't have to add the title, and the entity name will automatically be set to the file name.

Now I want to automate media audio entity creation with the Feeds module. I configured a feed type + a feed for my media audio entities. I'm importing CSV files from a directory.

I used the following mappings:

  • File ID + File Description for the media field field_media_audio_file
  • Code for the text field field_import_id (for which I set the unique option to keep track of imports)

However, when I try to import the feed, I get this error:

Name (name): This value should not be null.

How can I disable this validation (checking for the Name field) by the Feeds module for media audio entities?

As a workaround, I can create a column in the CSV file and list all the file names, but this results in duplicate information and presents the risk of typos.

3 Answers 3

1

Why not use Feeds Tamper to assign a default Name?

0

There is certainly a more elegant way to do this, but for now I decided to patch the validator itself in /feeds/src/Feeds/Processor/EntityProcessorBase.php:

if ($label || $label === '0' || $label === 0) {
  $messages[] = $this->t('The @entity %label failed to validate with the following errors: @errors', $args);
  if (mb_strtolower($this->entityTypeLabel()) === 'media') {
    // Media will automatically have the title set to filename, so allow this.
    return;
  }
}
2
  • 2
    mb_strtolower($this->entityTypeLabel()) === 'media' - this is strange to me. Surely you can just get the entity type's machine name. E.g., $this->entityTypeId()
    – sonfd
    Commented Jun 28, 2021 at 11:23
  • @sonfd Yeah, it seemed weird to me, too. But that's how the Feeds module itself is doing it in the code immediately above, so I decided to stick with the same code as the module itself. Commented Jun 28, 2021 at 11:36
0

Feeds has presave and prevalidate events. So another way to approach this is to add an event subscriber (sample code from the maintainer).

In this case, validation can be overridden with PROCESS_ENTITY_PREVALIDATE.

Your Answer

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

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