You are here

class FastlyImageFormatter in Fastly 8.3

Plugin implementation of the fastly 'image' formatter.

Plugin annotation


@FieldFormatter(
  id = "fastly_image",
  label = @Translation("Fastly Image Formatter"),
  field_types = {
    "image"
  },
  quickedit = {
    "editor" = "image"
  }
)

Hierarchy

Expanded class hierarchy of FastlyImageFormatter

File

src/Plugin/Field/FieldFormatter/FastlyImageFormatter.php, line 30

Namespace

Drupal\fastly\Plugin\Field\FieldFormatter
View source
class FastlyImageFormatter extends ImageFormatterBase implements ContainerFactoryPluginInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The image style entity storage.
   *
   * @var \Drupal\image\ImageStyleStorageInterface
   */
  protected $imageStyleStorage;

  /**
   * Constructs an ImageFormatter object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings settings.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
   *   The image style storage.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->currentUser = $current_user;
    $this->imageStyleStorage = $image_style_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
      ->get('current_user'), $container
      ->get('entity_type.manager')
      ->getStorage('image_style'));
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'width' => '',
      'height' => '',
      'image_link' => '',
      'dpr' => '',
      'fit' => '',
      'bg' => '',
      'bg-color' => '',
      'brightness' => '',
      'contrast' => '',
      'saturation' => '',
      'blur' => '',
      'format' => '',
      'quality' => '',
      'optimize' => '',
      'auto' => '',
      'enable' => '',
      'disable' => '',
      'resize-filter' => '',
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element['width'] = [
      '#title' => t('Width'),
      '#type' => 'number',
      '#description' => $this
        ->t('Entering integer is setting image in pixels (1-8192) and entering float 0.0-0.99 is setting percentage'),
      '#min' => 0,
      '#max' => 8192,
      '#step' => 0.01,
      '#default_value' => $this
        ->getSetting('width'),
    ];
    $element['height'] = [
      '#title' => t('Height'),
      '#type' => 'number',
      '#description' => $this
        ->t('Entering integer is setting image in pixels (1-8192) and entering float 0.0-0.99 is setting percentage'),
      '#min' => 0,
      '#max' => 8192,
      '#step' => 0.01,
      '#default_value' => $this
        ->getSetting('height'),
    ];
    $element['quality'] = [
      '#title' => t('Quality'),
      '#type' => 'number',
      '#description' => $this
        ->t('Parameter enables control over the compression level for lossy file-formatted images. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/quality',
      ]),
      '#default_value' => $this
        ->getSetting('quality'),
    ];
    $element['optimize'] = [
      '#title' => t('Optimize'),
      '#type' => 'select',
      '#description' => $this
        ->t('Automatically applies optimal quality compression to produce an output image with as much visual fidelity as possible, while minimizing the file size. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/enable',
      ]),
      '#default_value' => $this
        ->getSetting('optimize'),
      '#empty_option' => t('None'),
      '#options' => [
        'low' => 'low',
        'medium' => 'medium',
        'high' => 'high',
      ],
    ];
    $element['dpr'] = [
      '#title' => t('Device pixel ratio'),
      '#type' => 'select',
      '#description' => $this
        ->t('Parameter adds the ability to serve correctly sized images for devices that expose a device pixel ratio. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/dpr',
      ]),
      '#options' => [
        '1' => '1x',
        '1.5' => '1.5x',
        '2' => '2',
        '3' => '3',
        '4' => '4',
      ],
      '#empty_option' => t('None'),
      '#default_value' => $this
        ->getSetting('dpr'),
    ];
    $element['fit'] = [
      '#title' => t('Fit'),
      '#type' => 'select',
      '#description' => $this
        ->t('Set how the image will fit within the size bounds provided. Parameter controls how the image will be constrained within the provided size (width | height) values.'),
      '#default_value' => $this
        ->getSetting('fit'),
      '#empty_option' => t('None'),
      '#options' => [
        'bounds' => 'bounds',
        'cover' => 'cover',
        'crop' => 'crop',
      ],
    ];

    // @todo trim and crop
    $element['bg-color'] = [
      '#title' => t('Background color of an image'),
      '#type' => 'textfield',
      '#description' => $this
        ->t('Background color of an image in HEX value. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/bg-color',
      ]),
      '#default_value' => $this
        ->getSetting('bg-color'),
    ];
    $element['brightness'] = [
      '#title' => t('Brightness'),
      '#type' => 'number',
      '#description' => $this
        ->t('Brightness of the output image. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/brightness',
      ]),
      '#default_value' => $this
        ->getSetting('saturation'),
      '#min' => -100,
      '#max' => 100,
      '#step' => 0.01,
    ];
    $element['contrast'] = [
      '#title' => t('Contrast'),
      '#type' => 'number',
      '#description' => $this
        ->t('The contrast parameter increases or decreases the difference between the darkest and lightest tones in an image. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/contrast',
      ]),
      '#default_value' => $this
        ->getSetting('saturation'),
      '#min' => -100,
      '#max' => 100,
      '#step' => 0.01,
    ];
    $element['saturation'] = [
      '#title' => t('Saturation'),
      '#type' => 'number',
      '#description' => $this
        ->t('The saturation parameter increases or decreases the intensity of the colors in an image. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/saturation',
      ]),
      '#default_value' => $this
        ->getSetting('saturation'),
      '#min' => -100,
      '#max' => 100,
      '#step' => 0.01,
    ];

    // @todo Sharpen needs to be on the widget ?
    $element['blur'] = [
      '#title' => t('Blur'),
      '#type' => 'number',
      '#description' => $this
        ->t('Blurriness of the output image. Decimal number between 1 and 1000. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/blur',
      ]),
      '#default_value' => $this
        ->getSetting('blur'),
    ];
    $element['format'] = [
      '#title' => t('Format'),
      '#type' => 'select',
      '#description' => $this
        ->t('The format parameter enables the source image to be converted (a.k.a., "transcoded") from one encoded format to another. This is useful when the source image has been saved in a sub-optimal file format that hinders performance.. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/enable',
      ]),
      '#default_value' => $this
        ->getSetting('format'),
      '#empty_option' => t('None'),
      '#options' => [
        'gif' => $this
          ->t('Graphics Interchange Format'),
        'png' => $this
          ->t('Portable Network Graphics'),
        'png8' => $this
          ->t('Portable Network Graphics palette image with 256 colors and 8-bit transparency'),
        'png24' => $this
          ->t('Portable Network Graphics RGB image with 16.8 million colors'),
        'png32' => $this
          ->t('Portable Network Graphics RGBA image with 16.8 million colors and 8-bit transparency'),
        'jpg' => $this
          ->t('JPEG'),
        'pjpg' => $this
          ->t('Progressive JPEG'),
        'bjpg' => $this
          ->t('Baseline JPEG'),
        'webp' => $this
          ->t('WebP'),
        'webpll' => $this
          ->t('WebP (Lossless)'),
        'webply' => $this
          ->t('WebP (Lossy)'),
      ],
    ];

    //@todo frame
    $element['auto'] = [
      '#title' => t('Automatic optimization'),
      '#type' => 'select',
      '#description' => $this
        ->t('The auto parameter enables functionality that automates certain optimization features, like WebP image format support. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/auto',
      ]),
      '#default_value' => $this
        ->getSetting('auto'),
      '#empty_option' => t('None'),
      '#options' => [
        'webp' => 'webp',
      ],
    ];
    $element['resize-filter'] = [
      '#title' => t('Resize filter'),
      '#type' => 'select',
      '#description' => $this
        ->t('Enables control over the resizing filter used to generate a new image with a higher or lower number of pixels. More details <a href=":url" target="_blank">here</a>.', [
        ':url' => 'https://docs.fastly.com/en/image-optimization-api/resize-filter',
      ]),
      '#default_value' => $this
        ->getSetting('resize-filter'),
      '#empty_option' => t('None'),
      '#options' => [
        'nearest' => 'nearest',
        'bilinear' => 'bilinear',
        'bicubic' => 'bicubic',
        'lanczos2' => 'lanczos2',
        'lanczos3' => 'lanczos3',
      ],
    ];
    $link_types = [
      'content' => t('Content'),
      'file' => t('File'),
    ];
    $element['image_link'] = [
      '#title' => t('Link image to'),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('image_link'),
      '#empty_option' => t('Nothing'),
      '#options' => $link_types,
    ];
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $settings = $this
      ->getSettingsForParameters();
    foreach ($settings as $option) {
      if ($value = $this
        ->getSetting($option)) {
        $summary[] = $option . ' : ' . $value;
      }
    }
    $link_types = [
      'content' => t('Linked to content'),
      'file' => t('Linked to file'),
    ];

    // Display this setting only if image is linked.
    $image_link_setting = $this
      ->getSetting('image_link');
    if (isset($link_types[$image_link_setting])) {
      $summary[] = $link_types[$image_link_setting];
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    $files = $this
      ->getEntitiesToView($items, $langcode);

    // Early opt-out if the field is empty.
    if (empty($files)) {
      return $elements;
    }
    $url = NULL;
    $image_link_setting = $this
      ->getSetting('image_link');

    // Check if the formatter involves a link.
    if ($image_link_setting == 'content') {
      $entity = $items
        ->getEntity();
      if (!$entity
        ->isNew()) {
        $url = $entity
          ->toUrl();
      }
    }
    elseif ($image_link_setting == 'file') {
      $link_file = TRUE;
    }

    // Collect cache tags to be added for each item in the field.
    $base_cache_tags = [];
    foreach ($files as $delta => $file) {
      $cache_contexts = [];
      if (isset($link_file)) {
        $image_uri = $file
          ->getFileUri();

        // @todo Wrap in file_url_transform_relative(). This is currently
        // impossible. As a work-around, we currently add the 'url.site' cache
        // context to ensure different file URLs are generated for different
        // sites in a multisite setup, including HTTP and HTTPS versions of the
        // same site. Fix in https://www.drupal.org/node/2646744.
        $url = Url::fromUri(file_create_url($image_uri));
        $cache_contexts[] = 'url.site';
      }
      $cache_tags = Cache::mergeTags($base_cache_tags, $file
        ->getCacheTags());

      // Extract field item attributes for the theme function, and unset them
      // from the $item so that the field template does not re-render them.
      $item = $file->_referringItem;
      $item_attributes = $item->_attributes;
      unset($item->_attributes);
      $settings = $this
        ->getSettingsForParameters();
      $query = [];
      foreach ($settings as $option) {
        if ($value = $this
          ->getSetting($option)) {
          if ($value && ($option == 'width' || $option == 'height')) {
            $item
              ->set($option, $value);
          }
          $query[$option] = $value;
        }
      }
      if ($query) {
        $uri = $file
          ->getFileUri();
        $image_url = Url::fromUri(file_create_url($uri));
        $image_url
          ->setOption('query', $query);
        $file
          ->setFileUri($image_url
          ->toUriString());
      }
      $elements[$delta] = [
        '#theme' => 'image_formatter',
        '#item' => $item,
        '#item_attributes' => $item_attributes,
        '#image_style' => '',
        '#url' => $url,
        '#cache' => [
          'tags' => $cache_tags,
          'contexts' => $cache_contexts,
        ],
      ];
    }
    return $elements;
  }

  /**
   * Returns settings machine names that are used to set API parameters.
   *
   * @return string[]
   */
  public function getSettingsForParameters() {
    return [
      'width',
      'height',
      'dpr',
      'fit',
      'bg-color',
      'brightness',
      'contrast',
      'saturation',
      'blur',
      'format',
      'quality',
      'optimize',
      'auto',
      'resize-filter',
      'pad',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityReferenceFormatterBase::prepareView public function Loads the entities referenced in that field across all the entities being viewed. Overrides FormatterBase::prepareView
EntityReferenceFormatterBase::view public function Overrides FormatterBase::view
FastlyImageFormatter::$currentUser protected property The current user.
FastlyImageFormatter::$imageStyleStorage protected property The image style entity storage.
FastlyImageFormatter::create public static function Creates an instance of the plugin. Overrides FormatterBase::create
FastlyImageFormatter::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsBase::defaultSettings
FastlyImageFormatter::getSettingsForParameters public function Returns settings machine names that are used to set API parameters.
FastlyImageFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterBase::settingsForm
FastlyImageFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterBase::settingsSummary
FastlyImageFormatter::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements
FastlyImageFormatter::__construct public function Constructs an ImageFormatter object. Overrides FormatterBase::__construct
FileFormatterBase::checkAccess protected function Checks access to the given entity. Overrides EntityReferenceFormatterBase::checkAccess
FileFormatterBase::needsEntityLoad protected function Returns whether the entity referenced by an item needs to be loaded. Overrides EntityReferenceFormatterBase::needsEntityLoad 1
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::getFieldSetting protected function Returns the value of a field setting.
FormatterBase::getFieldSettings protected function Returns the array of field settings.
FormatterBase::isApplicable public static function Returns if the formatter can be used for the provided field. Overrides FormatterInterface::isApplicable 14
ImageFormatterBase::getEntitiesToView protected function Returns the referenced entities for display. Overrides EntityReferenceFormatterBase::getEntitiesToView
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.