You are here

public function MediaReference::process in Group Media 8.2

Search for the attached media entities.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Entity to search in.

Return value

\Drupal\media\MediaInterface[] Found media items.

Overrides MediaFinderInterface::process

File

src/Plugin/MediaFinder/MediaReference.php, line 23

Class

MediaReference
Plugin for searching media in entity reference fields.

Namespace

Drupal\groupmedia\Plugin\MediaFinder

Code

public function process(EntityInterface $entity) {
  $items = [];
  if ($entity instanceof ContentEntityInterface) {

    // Loop through all fields on the entity.
    foreach ($entity
      ->getFieldDefinitions() as $key => $field) {

      // Check if the field is an entity reference, referencing media entities,
      // and retriever the media entity.
      if (in_array($field
        ->getType(), $this
        ->getApplicableFieldTypes()) && $field
        ->getSetting('target_type') == 'media' && !$entity
        ->get($key)
        ->isEmpty()) {
        foreach ($entity
          ->get($key)
          ->getIterator() as $item) {
          if ($item->entity) {
            $items[] = $item->entity;
          }
        }
      }
    }
  }
  return $items;
}