Loading src/Plugin/Field/FieldFormatter/FileDecoupledFormatter.php +61 −1 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ namespace Drupal\decoupled_toolbox\Plugin\Field\FieldFormatter; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Form\FormStateInterface; use Drupal\decoupled_toolbox\Exception\InvalidContentException; use Drupal\decoupled_toolbox\Exception\UnexpectedFormatterException; Loading @@ -21,6 +22,57 @@ use Drupal\decoupled_toolbox\Exception\UnexpectedFormatterException; */ class FileDecoupledFormatter extends EntityReferenceDecoupledFormatterBase { protected const SETTINGS__INCLUDE_MIME_TYPE = 'include_mime_type'; /** * {@inheritdoc} */ public static function defaultSettings() : array { return parent::defaultSettings() + [ self::SETTINGS__INCLUDE_MIME_TYPE => FALSE, ]; } /** * Shortcut to get include_mime_type value. * * @return bool * The include_mime_type value. */ protected function getIncludeMimeTypeSettings(): bool { return $this->settings[self::SETTINGS__INCLUDE_MIME_TYPE]; } /** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state): array { $form = parent::settingsForm($form, $form_state); $form[self::SETTINGS__INCLUDE_MIME_TYPE] = [ '#default_value' => $this->getSetting(self::SETTINGS__INCLUDE_MIME_TYPE), '#description' => $this->t('Check this option to include the MIME type of the file.'), '#title' => $this->t('Include MIME type'), '#type' => 'checkbox', ]; return $form; } /** * {@inheritdoc} */ public function settingsSummary(): array { $settings = $this->getSettings(); $summary = parent::settingsSummary(); if ($settings[self::SETTINGS__INCLUDE_MIME_TYPE]) { $summary[] = $this->t('The MIME type of the file is shown'); } return $summary; } /** * {@inheritdoc} */ Loading Loading @@ -50,9 +102,16 @@ class FileDecoupledFormatter extends EntityReferenceDecoupledFormatterBase { $url = $file->createFileUrl(FALSE); $this->addProcessedFieldCacheTag($file->getCacheTags()); return [ $itemValues = [ 'url' => $url, ]; if ($this->getIncludeMimeTypeSettings()) { $itemValues['mime_type'] = $file->getMimeType(); } return $itemValues; } /** Loading @@ -61,6 +120,7 @@ class FileDecoupledFormatter extends EntityReferenceDecoupledFormatterBase { public static function getOutputDefinitions() { return [ 'url' => 'string', 'mime_type' => 'string', ]; } Loading src/Plugin/Field/FieldFormatter/ImageDecoupledFormatter.php +23 −12 Original line number Diff line number Diff line Loading @@ -230,12 +230,17 @@ class ImageDecoupledFormatter extends FileDecoupledFormatter { $this->addProcessedFieldCacheTag($tag); } return [ array_merge([ $itemValues = array_merge([ 'image_style' => $imageStyleSettings, 'url' => $url, ], $this->getCommonImageProperties($item)), ]; ], $this->getCommonImageProperties($item)); if ($this->getIncludeMimeTypeSettings()) { $itemValues['mime_type'] = $file->getMimeType(); } // @todo Why do we need to wrap in an array like this? return [$itemValues]; } // Use original image. Loading @@ -246,12 +251,17 @@ class ImageDecoupledFormatter extends FileDecoupledFormatter { $this->addProcessedFieldCacheTag($tag); } return [ array_merge([ $itemValues = array_merge([ 'image_style' => NULL, 'url' => $url, ], $this->getCommonImageProperties($item)), ]; ], $this->getCommonImageProperties($item)); if ($this->getIncludeMimeTypeSettings()) { $itemValues['mime_type'] = $file->getMimeType(); } // @todo Why do we need to wrap in an array like this? return [$itemValues]; } /** Loading @@ -261,6 +271,7 @@ class ImageDecoupledFormatter extends FileDecoupledFormatter { return [ 'image_style' => 'string', 'url' => 'string', 'mime_type' => 'string', 'title' => 'string', 'alt' => 'string', 'width' => 'string', Loading src/Plugin/Field/FieldFormatter/ImageGenericDecoupledFormatter.php +8 −1 Original line number Diff line number Diff line Loading @@ -130,10 +130,16 @@ class ImageGenericDecoupledFormatter extends FileDecoupledFormatter { $this->addProcessedFieldCacheTag($tag); } return array_merge([ $itemValues = array_merge([ "source" => self::IMAGE_SOURCE, "uri" => $uri, ], $this->getCommonImageProperties($item)); if ($this->getIncludeMimeTypeSettings()) { $itemValues['mime_type'] = $file->getMimeType(); } return $itemValues; } /** Loading @@ -143,6 +149,7 @@ class ImageGenericDecoupledFormatter extends FileDecoupledFormatter { return [ 'source' => 'string', 'uri' => 'string', 'mime_type' => 'string', 'title' => 'string', 'alt' => 'string', ]; Loading Loading
src/Plugin/Field/FieldFormatter/FileDecoupledFormatter.php +61 −1 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ namespace Drupal\decoupled_toolbox\Plugin\Field\FieldFormatter; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Form\FormStateInterface; use Drupal\decoupled_toolbox\Exception\InvalidContentException; use Drupal\decoupled_toolbox\Exception\UnexpectedFormatterException; Loading @@ -21,6 +22,57 @@ use Drupal\decoupled_toolbox\Exception\UnexpectedFormatterException; */ class FileDecoupledFormatter extends EntityReferenceDecoupledFormatterBase { protected const SETTINGS__INCLUDE_MIME_TYPE = 'include_mime_type'; /** * {@inheritdoc} */ public static function defaultSettings() : array { return parent::defaultSettings() + [ self::SETTINGS__INCLUDE_MIME_TYPE => FALSE, ]; } /** * Shortcut to get include_mime_type value. * * @return bool * The include_mime_type value. */ protected function getIncludeMimeTypeSettings(): bool { return $this->settings[self::SETTINGS__INCLUDE_MIME_TYPE]; } /** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state): array { $form = parent::settingsForm($form, $form_state); $form[self::SETTINGS__INCLUDE_MIME_TYPE] = [ '#default_value' => $this->getSetting(self::SETTINGS__INCLUDE_MIME_TYPE), '#description' => $this->t('Check this option to include the MIME type of the file.'), '#title' => $this->t('Include MIME type'), '#type' => 'checkbox', ]; return $form; } /** * {@inheritdoc} */ public function settingsSummary(): array { $settings = $this->getSettings(); $summary = parent::settingsSummary(); if ($settings[self::SETTINGS__INCLUDE_MIME_TYPE]) { $summary[] = $this->t('The MIME type of the file is shown'); } return $summary; } /** * {@inheritdoc} */ Loading Loading @@ -50,9 +102,16 @@ class FileDecoupledFormatter extends EntityReferenceDecoupledFormatterBase { $url = $file->createFileUrl(FALSE); $this->addProcessedFieldCacheTag($file->getCacheTags()); return [ $itemValues = [ 'url' => $url, ]; if ($this->getIncludeMimeTypeSettings()) { $itemValues['mime_type'] = $file->getMimeType(); } return $itemValues; } /** Loading @@ -61,6 +120,7 @@ class FileDecoupledFormatter extends EntityReferenceDecoupledFormatterBase { public static function getOutputDefinitions() { return [ 'url' => 'string', 'mime_type' => 'string', ]; } Loading
src/Plugin/Field/FieldFormatter/ImageDecoupledFormatter.php +23 −12 Original line number Diff line number Diff line Loading @@ -230,12 +230,17 @@ class ImageDecoupledFormatter extends FileDecoupledFormatter { $this->addProcessedFieldCacheTag($tag); } return [ array_merge([ $itemValues = array_merge([ 'image_style' => $imageStyleSettings, 'url' => $url, ], $this->getCommonImageProperties($item)), ]; ], $this->getCommonImageProperties($item)); if ($this->getIncludeMimeTypeSettings()) { $itemValues['mime_type'] = $file->getMimeType(); } // @todo Why do we need to wrap in an array like this? return [$itemValues]; } // Use original image. Loading @@ -246,12 +251,17 @@ class ImageDecoupledFormatter extends FileDecoupledFormatter { $this->addProcessedFieldCacheTag($tag); } return [ array_merge([ $itemValues = array_merge([ 'image_style' => NULL, 'url' => $url, ], $this->getCommonImageProperties($item)), ]; ], $this->getCommonImageProperties($item)); if ($this->getIncludeMimeTypeSettings()) { $itemValues['mime_type'] = $file->getMimeType(); } // @todo Why do we need to wrap in an array like this? return [$itemValues]; } /** Loading @@ -261,6 +271,7 @@ class ImageDecoupledFormatter extends FileDecoupledFormatter { return [ 'image_style' => 'string', 'url' => 'string', 'mime_type' => 'string', 'title' => 'string', 'alt' => 'string', 'width' => 'string', Loading
src/Plugin/Field/FieldFormatter/ImageGenericDecoupledFormatter.php +8 −1 Original line number Diff line number Diff line Loading @@ -130,10 +130,16 @@ class ImageGenericDecoupledFormatter extends FileDecoupledFormatter { $this->addProcessedFieldCacheTag($tag); } return array_merge([ $itemValues = array_merge([ "source" => self::IMAGE_SOURCE, "uri" => $uri, ], $this->getCommonImageProperties($item)); if ($this->getIncludeMimeTypeSettings()) { $itemValues['mime_type'] = $file->getMimeType(); } return $itemValues; } /** Loading @@ -143,6 +149,7 @@ class ImageGenericDecoupledFormatter extends FileDecoupledFormatter { return [ 'source' => 'string', 'uri' => 'string', 'mime_type' => 'string', 'title' => 'string', 'alt' => 'string', ]; Loading