Skip to content

Commit

Permalink
Add validation for the plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronGilMartinez committed Jan 10, 2025
1 parent ae43924 commit 7073d3c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
50 changes: 31 additions & 19 deletions src/Plugin/Field/FieldFormatter/CoolPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,58 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

declare(strict_types=1);

namespace Drupal\collabora_online\Plugin\Field\FieldFormatter;

use Drupal\collabora_online\CollaboraUrl;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\media\MediaInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
* Plugin implementation of the 'collabora_preview' formatter.
*
* @FieldFormatter(
* id = "collabora_preview",
* label = @Translation("Collabora Online preview"),
* field_types = {
* "file"
* }
* )
*/
#[FieldFormatter(
id: 'collabora_preview',
label: new TranslatableMarkup('Collabora Online preview'),
field_types: [
'file',
],
)]
class CoolPreview extends EntityReferenceFormatterBase {

/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = $this->t('Preview Collabora Online documents.');
return $summary;
public function settingsSummary(): array {
return [
$this->t('Preview Collabora Online documents.'),
];
}

/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition): bool {
// Entity types other than 'media' are not supported.
if ($field_definition->getTargetEntityTypeId() !== 'media') {
return FALSE;
}
// Collabora online only supports one file per media.
return !$field_definition->getFieldStorageDefinition()->isMultiple();
}

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items */
public function viewElements(FieldItemListInterface $items, $langcode): array {
$elements = [];
/** @var \Drupal\media\MediaInterface $media */
$media = $items->getEntity();
if (!$media instanceof MediaInterface) {
// Entity types other than 'media' are not supported.
return [];
}

$access_result = $media->access('preview in collabora', NULL, TRUE);
(new CacheableMetadata())
Expand All @@ -73,6 +84,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
// Render each element as markup.
$elements[$delta] = $render_array;
}

return $elements;
}

Expand Down
23 changes: 21 additions & 2 deletions tests/src/Kernel/CoolPreviewFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
Expand Down Expand Up @@ -65,10 +66,11 @@ public function testViewElements(): void {
]);
$media->save();

$this->setCurrentUser($this->createUser([
$user = $this->createUser([
'access content',
'preview document in collabora',
]));
]);
$this->setCurrentUser($user);

// Field is rendered correctly.
$this->assertCoolPreviewField(
Expand Down Expand Up @@ -110,6 +112,23 @@ public function testViewElements(): void {
],
$media_display->build($media)[$field_name]
);
$this->setCurrentUser($user);

// Fields with multiple values are not supported.
$field_storage = FieldStorageConfig::load("media.$field_name");
$field_storage->setCardinality(3)->save();
$media_display = EntityViewDisplay::load($media_display->id());
$this->assertCoolPreviewField(
[],
[
'contexts' => [],
'tags' => [],
'max-age' => Cache::PERMANENT,
],
$media_display->build($media)[$field_name]
);
$field_storage->setCardinality(1)->save();
$media_display = EntityViewDisplay::load($media_display->id());

// Iframe is not displayed for empty value field.
$this->setCurrentUser($this->createUser([
Expand Down

0 comments on commit 7073d3c

Please sign in to comment.