Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By phenaproxima on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.5.x
Introduced in version:
8.5.0-alpha1
Issue links:
Description:
Currently, to get the value of the source field of a media entity, you must write code similar to this:
$source_field = $this->getSourceFieldDefinition($media->bundle->entity)->getName();
/** @var \Drupal\Core\Field\FieldItemInterface $source_field_item */
$source_field_item = $media->get($source_field)->first();
$main_property = $source_field_item->mainPropertyName();
$source_field_value = $source_field_item->get($main_property)->getString();
As of #2934966: Make it easier to get source field values from media items, a new method has been added to MediaSourceInterface called getSourceFieldValue(), which returns the value of a media entity's source field. MediaSourceBase has a generic implementation of this method which should work in almost all cases.
So now, to get the source field value, you can just do this instead:
$media_entity->getSource()->getSourceFieldValue($media_entity)
Impacts:
Module developers