When I upgraded Video Embed Field to 8.x0-2.0-alpha1 and ran the database update, I found that the upgrade had added a new field ("field_media_video_embed_field _1") to the default one that had previously been created when video_embed_media was installed ("field_media_video_embed_field"). When I investigated, I found that the following section of media_entity_update_8201() was failing to find my video type's source field, and thus creating a new one:

    /** @var \Drupal\media\MediaTypeInterface $media_type */
    $media_type = \Drupal::entityTypeManager()->getStorage('media_type')
      ->load($config->get('id'));
    $media_source = $media_type->getSource();
    $source_field = $media_source->getSourceFieldDefinition($media_type); // Failing here! --BD.
    if (!$source_field) {
      $source_field = $media_source->createSourceField($media_type);
      $source_field->getFieldStorageDefinition()->save();
      $source_field->save();

      $media_type
        ->set('source_configuration', [
          'source_field' => $source_field->getName(),
        ]);
    }

Changing the behavior of VideoEmbedField::getSourceFieldDefinition() to account for the fact that the old version of Video Embed Media wasn't setting a source_field configuration value addresses this. I recognize that I'm hard-coding a field name in here, but I thought this line in the patched code made that a reasonably safe choice:

    $field = !empty($this->configuration['source_field']) ? $this->configuration['source_field'] : 'field_media_video_embed_field';

Hope this helps!

Comments

bdimaggio created an issue. See original summary.

bdimaggio’s picture

phenaproxima’s picture

Status: Active » Needs review
Issue tags: +Media Initiative, +Needs manual testing
marcoscano’s picture

Version: 8.x-2.0-alpha1 » 8.x-2.x-dev
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs manual testing

Manually tested this, and I can confirm that:
- The problem can be reproduced (i.e. an unexpected field "_1" is created on VEF media types, though not enabled in form/view displays)
- The patch in #2 solves the issue, and the upgrade works as expected with the patch applied.

I personally don't see an issue with defining the field machine name in code, it was already there in the 1.x branch, so we are not inventing anything new :)

For me this is OK to go.

sam152’s picture

  • Sam152 committed 6084137 on 8.x-2.x authored by bdimaggio
    Issue #2927149 by bdimaggio, phenaproxima, marcoscano: Updating from...
sam152’s picture

Status: Reviewed & tested by the community » Fixed

Some testing would have been nice here, but don't want to hold up the fix. If anyone feels like following it up with a test, that would be cool.

sam152’s picture

Given this is a pretty nasty bugfix in the upgrade, tagging alpha2.

m4olivei’s picture

Just something to be aware of here, if you are extending the VideoEmbedField class in your own module to create a new MediaSource plugin, you need to be careful to also declare a source_field machine name. Otherwise what happens is when the media type is saved, the source field storage is created here:

\Drupal\media\MediaSourceBase::createSourceFieldStorage

Which calls \Drupal\media\MediaSourceBase::getSourceFieldName, which names the source field by the plugin id, so you get a field created named field_media_' . $this->getPluginId(), in our example field_media_yt_push. However, then, the plugin refers to $this->configuration['source_field'] in a bunch of places which will resolve to field_media_video_embed_field, which blows up, b/c thats not the field that was auto-created.

Setting the default configuration to either empty, or the machine name according to your plugin id, in our case field_media_yt_push, fixes the issue.

Status: Fixed » Closed (fixed)

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