Score:0

How do I automatically add a title to imported entities?

cn flag

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.

Score:1
in flag

Why not use Feeds Tamper to assign a default Name?

Score:0
cn flag

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;
  }
}
sonfd avatar
in flag
`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()`
cn flag
@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.
Score:0
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.