You are here

public function WebformImageFile::getItemFormats in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformImageFile.php \Drupal\webform\Plugin\WebformElement\WebformImageFile::getItemFormats()

Get an element's available single value formats.

Return value

array An associative array of single value formats containing name/label pairs.

Overrides WebformManagedFileBase::getItemFormats

File

src/Plugin/WebformElement/WebformImageFile.php, line 64

Class

WebformImageFile
Provides a 'webform_image_file' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function getItemFormats() {
  $formats = parent::getItemFormats();

  // Instead of 'file' the item default format is ':image'.
  unset($formats['file']);

  // Add support :image, :link, and :modal.
  $label = (string) $this
    ->t('Original Image');
  $t_args = [
    '@label' => $label,
  ];
  $formats[$label][":image"] = $this
    ->t('@label: Image', $t_args);
  $formats[$label][":link"] = $this
    ->t('@label: Link', $t_args);
  $formats[$label][":modal"] = $this
    ->t('@label: Modal', $t_args);
  if ($this->moduleHandler
    ->moduleExists('image')) {
    $image_styles = $this
      ->getEntityStorage('image_style')
      ->loadMultiple();
    foreach ($image_styles as $id => $image_style) {
      $label = (string) $image_style
        ->label();
      $t_args = [
        '@label' => $label,
      ];
      $formats[$label]["{$id}:image"] = $this
        ->t('@label: Image', $t_args);
      $formats[$label]["{$id}:link"] = $this
        ->t('@label: Link', $t_args);
      $formats[$label]["{$id}:modal"] = $this
        ->t('@label: Modal', $t_args);
    }
  }
  return $formats;
}