Skip to content

Commit

Permalink
Issue CollaboraOnline#27: Use the media entity from the field items i…
Browse files Browse the repository at this point in the history
…n the field formatter.
  • Loading branch information
donquixote committed Sep 13, 2024
1 parent 84b01de commit 7a3d297
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
15 changes: 0 additions & 15 deletions src/Cool/CoolUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,6 @@ public static function setMediaSource(Media $media, File $source) {
$media->set($name, $source);
}

/** Get the media source entity for the file.
* It will get the first in the list if there are more than one.
*/
public static function getMediaSourceForFile(File $file) {
$media_entities = \Drupal::entityTypeManager()->getStorage('media')->loadByProperties([
'field_media_document' => $file->id(),
]);
if (!is_array($media_entities)) {
\Drupal::logger('cool')->error('Media for file ' . $file->id() . ' not found.');
return NULL;
}

return array_pop($media_entities);
}

/** Obtain the signing key from the key storage */
static function getKey() {
$default_config = \Drupal::config('collabora_online.settings');
Expand Down
11 changes: 6 additions & 5 deletions src/Plugin/Field/FieldFormatter/CoolPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ public function settingsSummary() {
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$media = $items->getEntity();
if (!$media instanceof MediaInterface) {
// The formatter is being used on the wrong entity type.
// @todo Log this.
return [];
}

foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {
$media = CoolUtils::getMediaSourceForFile($file);
if (!$media) {
continue;
}

$url = CoolUtils::getEditorUrl($media, false);

$render_array = [
Expand Down

0 comments on commit 7a3d297

Please sign in to comment.