You are here

public function ShortcodeBase::getMediaField in Shortcode 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/ShortcodeBase.php \Drupal\shortcode\Plugin\ShortcodeBase::getMediaField()

Get a media entity field.

Loop through Drupal media file fields, and return a field object if found.

Parameters

\Drupal\media\Entity\Media $entity: Drupal media entity.

Return value

mixed|object|bool If available, the field object.

2 calls to ShortcodeBase::getMediaField()
ShortcodeBase::getImageProperties in src/Plugin/ShortcodeBase.php
Returns image properties for a given image media entity id.
ShortcodeBase::getMediaFileUrl in src/Plugin/ShortcodeBase.php
Get the file url for a media object.

File

src/Plugin/ShortcodeBase.php, line 303

Class

ShortcodeBase
Provides a base class for Shortcode plugins.

Namespace

Drupal\shortcode\Plugin

Code

public function getMediaField(Media $entity) {
  $media_file_fields = [
    'field_media_file',
    'field_media_image',
    'field_media_video_file',
    'field_media_audio_file',
  ];
  foreach ($media_file_fields as $field_name) {
    if ($entity
      ->hasField($field_name)) {
      return $entity
        ->get($field_name);
    }
  }
  return FALSE;
}