class ImageFormatter

Plugin implementation of the 'image' formatter.

Plugin annotation


@Plugin(
  id = "image",
  module = "image",
  label = @Translation("Image"),
  field_types = {
    "image"
  },
  settings = {
    "image_style" = "",
    "image_link" = ""
  }
)

Hierarchy

Expanded class hierarchy of ImageFormatter

File

drupal/core/modules/image/lib/Drupal/image/Plugin/field/formatter/ImageFormatter.php, line 31
Contains \Drupal\image\Plugin\field\formatter\ImageFormatter.

Namespace

Drupal\image\Plugin\field\formatter
View source
class ImageFormatter extends FormatterBase {

  /**
   * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsForm(array $form, array &$form_state) {
    $image_styles = image_style_options(FALSE);
    $element['image_style'] = array(
      '#title' => t('Image style'),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('image_style'),
      '#empty_option' => t('None (original image)'),
      '#options' => $image_styles,
    );
    $link_types = array(
      'content' => t('Content'),
      'file' => t('File'),
    );
    $element['image_link'] = array(
      '#title' => t('Link image to'),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('image_link'),
      '#empty_option' => t('Nothing'),
      '#options' => $link_types,
    );
    return $element;
  }

  /**
   * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary().
   */
  public function settingsSummary() {
    $summary = array();
    $image_styles = image_style_options(FALSE);

    // Unset possible 'No defined styles' option.
    unset($image_styles['']);

    // Styles could be lost because of enabled/disabled modules that defines
    // their styles in code.
    $image_style_setting = $this
      ->getSetting('image_style');
    if (isset($image_styles[$image_style_setting])) {
      $summary[] = t('Image style: @style', array(
        '@style' => $image_styles[$image_style_setting],
      ));
    }
    else {
      $summary[] = t('Original image');
    }
    $link_types = array(
      '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 implode('<br />', $summary);
  }

  /**
   * Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
   */
  public function viewElements(EntityInterface $entity, $langcode, array $items) {
    $elements = array();
    $image_link_setting = $this
      ->getSetting('image_link');

    // Check if the formatter involves a link.
    if ($image_link_setting == 'content') {
      $uri = $entity
        ->uri();
    }
    elseif ($image_link_setting == 'file') {
      $link_file = TRUE;
    }
    $image_style_setting = $this
      ->getSetting('image_style');
    foreach ($items as $delta => $item) {
      if (isset($link_file)) {
        $uri = array(
          'path' => file_create_url($item['uri']),
          'options' => array(),
        );
      }
      $elements[$delta] = array(
        '#theme' => 'image_formatter',
        '#item' => $item,
        '#image_style' => $image_style_setting,
        '#path' => isset($uri) ? $uri : '',
      );
    }
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormatterBase::$field protected property The field definition.
FormatterBase::$instance protected property The field instance 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::$weight protected property The formatter weight.
FormatterBase::prepareView public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView(). Overrides FormatterInterface::prepareView 2
FormatterBase::view public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::view(). Overrides FormatterInterface::view
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct
ImageFormatter::settingsForm public function Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). Overrides FormatterBase::settingsForm
ImageFormatter::settingsSummary public function Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary(). Overrides FormatterBase::settingsSummary
ImageFormatter::viewElements public function Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements(). Overrides FormatterInterface::viewElements
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$discovery protected property The discovery object.
PluginBase::$plugin_id protected property The plugin_id.
PluginBase::getDefinition public function Implements Drupal\Component\Plugin\PluginInterface::getDefinition(). Overrides PluginInspectionInterface::getDefinition
PluginBase::getPluginId public function Implements Drupal\Component\Plugin\PluginInterface::getPluginId(). Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings