Commit a621b42e authored by Quang-Minh DANG's avatar Quang-Minh DANG
Browse files

Issue #3324945 by Dakwamine: MIME type info for file decoupled formatters

parent f3681922
Loading
Loading
Loading
Loading
+61 −1
Original line number Diff line number Diff line
@@ -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;

@@ -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}
   */
@@ -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;
  }

  /**
@@ -61,6 +120,7 @@ class FileDecoupledFormatter extends EntityReferenceDecoupledFormatterBase {
  public static function getOutputDefinitions() {
    return [
      'url' => 'string',
      'mime_type' => 'string',
    ];
  }

+23 −12
Original line number Diff line number Diff line
@@ -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.
@@ -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];
  }

  /**
@@ -261,6 +271,7 @@ class ImageDecoupledFormatter extends FileDecoupledFormatter {
    return [
      'image_style' => 'string',
      'url' => 'string',
      'mime_type' => 'string',
      'title' => 'string',
      'alt' => 'string',
      'width' => 'string',
+8 −1
Original line number Diff line number Diff line
@@ -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;
  }

  /**
@@ -143,6 +149,7 @@ class ImageGenericDecoupledFormatter extends FileDecoupledFormatter {
    return [
      'source' => 'string',
      'uri' => 'string',
      'mime_type' => 'string',
      'title' => 'string',
      'alt' => 'string',
    ];