class PictureFormatter

Plugin for picture formatter.

Plugin annotation


@Plugin(
  id = "picture",
  module = "picture",
  label = @Translation("Picture"),
  field_types = {
    "image",
  },
  settings = {
    "picture_mapping" = "",
    "fallback_image_style" = "",
    "image_link" = "",
  }
)

Hierarchy

Expanded class hierarchy of PictureFormatter

File

drupal/core/modules/picture/lib/Drupal/picture/Plugin/field/formatter/PictureFormatter.php, line 32
Definition of Drupal\picture\Plugin\field\formatter\PictureFormatter.

Namespace

Drupal\picture\Plugin\field\formatter
View source
class PictureFormatter extends FormatterBase {

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsForm(array $form, array &$form_state) {
    $picture_options = array();
    $picture_mappings = entity_load_multiple('picture_mapping');
    if ($picture_mappings && !empty($picture_mappings)) {
      foreach ($picture_mappings as $machine_name => $picture_mapping) {
        if ($picture_mapping
          ->hasMappings()) {
          $picture_options[$machine_name] = $picture_mapping
            ->label();
        }
      }
    }
    $elements['picture_mapping'] = array(
      '#title' => t('Picture mapping'),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('picture_mapping'),
      '#required' => TRUE,
      '#options' => $picture_options,
    );
    $image_styles = image_style_options(FALSE);
    $elements['fallback_image_style'] = array(
      '#title' => t('Fallback image style'),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('fallback_image_style'),
      '#empty_option' => t('Automatic'),
      '#options' => $image_styles,
    );
    $link_types = array(
      'content' => t('Content'),
      'file' => t('File'),
    );
    $elements['image_link'] = array(
      '#title' => t('Link image to'),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('image_link'),
      '#empty_option' => t('Nothing'),
      '#options' => $link_types,
    );
    return $elements;
  }

  /**
   * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
   */
  public function settingsSummary() {
    $summary = array();
    $picture_mapping = entity_load('picture_mapping', $this
      ->getSetting('picture_mapping'));
    if ($picture_mapping) {
      $summary[] = t('Picture mapping: @picture_mapping', array(
        '@picture_mapping' => $picture_mapping
          ->label(),
      ));
      $image_styles = image_style_options(FALSE);
      unset($image_styles['']);
      if (isset($image_styles[$this
        ->getSetting('fallback_image_style')])) {
        $summary[] = t('Fallback Image style: @style', array(
          '@style' => $image_styles[$this
            ->getSetting('fallback_image_style')],
        ));
      }
      else {
        $summary[] = t('Automatic fallback');
      }
      $link_types = array(
        'content' => t('Linked to content'),
        'file' => t('Linked to file'),
      );

      // Display this setting only if image is linked.
      if (isset($link_types[$this
        ->getSetting('image_link')])) {
        $summary[] = $link_types[$this
          ->getSetting('image_link')];
      }
    }
    else {
      $summary[] = t('Select a picture mapping.');
    }
    return implode('<br />', $summary);
  }

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

    // Check if the formatter involves a link.
    if ($this
      ->getSetting('image_link') == 'content') {
      $uri = $entity
        ->uri();
    }
    elseif ($this
      ->getSetting('image_link') == 'file') {
      $link_file = TRUE;
    }
    $breakpoint_styles = array();
    $fallback_image_style = '';
    $picture_mapping = entity_load('picture_mapping', $this
      ->getSetting('picture_mapping'));
    if ($picture_mapping) {
      foreach ($picture_mapping->mappings as $breakpoint_name => $multipliers) {

        // Make sure there are multipliers.
        if (!empty($multipliers)) {

          // Make sure that the breakpoint exists and is enabled.
          // @todo add the following when breakpoint->status is added again:
          // $picture_mapping->breakpointGroup->breakpoints[$breakpoint_name]->status
          if (isset($picture_mapping->breakpointGroup->breakpoints[$breakpoint_name])) {
            $breakpoint = $picture_mapping->breakpointGroup->breakpoints[$breakpoint_name];

            // Determine the enabled multipliers.
            $multipliers = array_intersect_key($multipliers, $breakpoint->multipliers);
            foreach ($multipliers as $multiplier => $image_style) {

              // Make sure the multiplier still exists.
              if (!empty($image_style)) {

                // First mapping found is used as fallback.
                if (empty($fallback_image_style)) {
                  $fallback_image_style = $image_style;
                }
                if (!isset($breakpoint_styles[$breakpoint_name])) {
                  $breakpoint_styles[$breakpoint_name] = array();
                }
                $breakpoint_styles[$breakpoint_name][$multiplier] = $image_style;
              }
            }
          }
        }
      }
    }

    // Check if the user defined a custom fallback image style.
    if ($this
      ->getSetting('fallback_image_style')) {
      $fallback_image_style = $this
        ->getSetting('fallback_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' => 'picture_formatter',
        '#attached' => array(
          'library' => array(
            array(
              'picture',
              'picturefill',
            ),
          ),
        ),
        '#item' => $item,
        '#image_style' => $fallback_image_style,
        '#breakpoints' => $breakpoint_styles,
        '#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
PictureFormatter::settingsForm public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). Overrides FormatterBase::settingsForm
PictureFormatter::settingsSummary public function Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm(). Overrides FormatterBase::settingsSummary
PictureFormatter::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