diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php index 78b62b3..c0f85c9 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php @@ -24,7 +24,9 @@ * label = @Translation("Rendered entity"), * description = @Translation("Display the referenced entities rendered by entity_view()."), * field_types = { - * "entity_reference" + * "entity_reference", + * "file", + * "image" * } * ) */ diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php index 2cf8c20..da68764 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php @@ -17,7 +17,9 @@ * label = @Translation("Entity ID"), * description = @Translation("Display the ID of the referenced entities."), * field_types = { - * "entity_reference" + * "entity_reference", + * "file", + * "image" * } * ) */ diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php index eb005bc..98bdd80 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php @@ -19,7 +19,9 @@ * label = @Translation("Label"), * description = @Translation("Display the label of the referenced entities."), * field_types = { - * "entity_reference" + * "entity_reference", + * "file", + * "image" * } * ) */ diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php index 9cbe807..b47b5f7 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileFormatterBase.php @@ -9,8 +9,10 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; +use Drupal\file\Plugin\Field\FieldType\FileItem; /** * Base class for file formatters. @@ -21,7 +23,12 @@ * {@inheritdoc} */ protected function needsEntityLoad(EntityReferenceItem $item) { - return parent::needsEntityLoad($item) && $item->isDisplayed(); + if ($item instanceof FileItem) { + return parent::needsEntityLoad($item) && $item->isDisplayed(); + } + else { + return parent::needsEntityLoad($item); + } } /** @@ -39,4 +46,13 @@ protected function checkAccess(EntityInterface $entity) { } } + /** + * {@inheritdoc} + */ + public static function isApplicable(FieldDefinitionInterface $field_definition) { + // Since file formatters may be re-used on entity reference fields, ensure + // that a file entity type is being referenced by the field. + return $field_definition->getSetting('target_type') === 'file'; + } + } diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php index 6219cd0..a9b96f2 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php @@ -16,7 +16,9 @@ * id = "file_default", * label = @Translation("Generic file"), * field_types = { - * "file" + * "entity_reference", + * "file", + * "image" * } * ) */ diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php index 2f0a65f..c3cf41b 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/RSSEnclosureFormatter.php @@ -16,7 +16,9 @@ * id = "file_rss_enclosure", * label = @Translation("RSS enclosure"), * field_types = { - * "file" + * "entity_reference", + * "file", + * "image" * } * ) */ diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php index fda843e..a755cb7 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php @@ -16,7 +16,9 @@ * id = "file_table", * label = @Translation("Table of files"), * field_types = { - * "file" + * "entity_reference", + * "file", + * "image" * } * ) */ diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php index 0795be2..42a98c1 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php @@ -16,7 +16,9 @@ * id = "file_url_plain", * label = @Translation("URL to file"), * field_types = { - * "file" + * "entity_reference", + * "file", + * "image" * } * ) */ diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php index 029e7a5..e3e43a0 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php @@ -26,7 +26,9 @@ * id = "image", * label = @Translation("Image"), * field_types = { - * "image" + * "entity_reference", + * "file", + * "image", * } * ) */ diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php index 899aa49..b1cd28d 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php @@ -47,7 +47,34 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item } } - return parent::getEntitiesToView($items, $langcode); + $entities = parent::getEntitiesToView($items, $langcode); + + // If this formatter is being used on a non-image field, ensure only images + // are rendered, and have width and height properties set. + if ($this->fieldDefinition->getType() !== 'image') { + foreach ($entities as $delta => $entity) { + $image = \Drupal::service('image.factory')->get($entity->getFileUri()); + if ($image->isValid()) { + $item = $entity->_referringItem; + $properties = $item->getProperties(); + // @todo Setting width and height manually could be avoided if these + // were available on the file entity itself. + // @see https://www.drupal.org/node/1448124 + if (!isset($properties['width']) || $item->get('width') === NULL) { + $item->set('width', $image->getWidth()); + } + if (!isset($properties['height']) || $item->get('height') === NULL) { + $item->set('height', $image->getHeight()); + } + } + else { + // Files not supported as images should not be displayed. + unset($entities[$delta]); + } + } + } + + return $entities; } } diff --git a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php index e66cb82..7c115e7 100644 --- a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php +++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php @@ -26,6 +26,8 @@ * id = "responsive_image", * label = @Translation("Responsive image"), * field_types = { + * "entity_reference", + * "file", * "image", * } * )