sirv-8.x-3.0-beta4/src/Plugin/Field/FieldFormatter/SirvImageFormatter.php
src/Plugin/Field/FieldFormatter/SirvImageFormatter.php
<?php
namespace Drupal\sirv\Plugin\Field\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'sirv_image' formatter.
*
* @FieldFormatter(
* id = "sirv_image",
* label = @Translation("Sirv image"),
* field_types = {
* "image"
* },
* weight = 10
* )
*/
class SirvImageFormatter extends SirvImageFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'responsive' => TRUE,
'responsive_scale_crop' => '',
'responsive_resize_step' => '',
'lazy_load' => TRUE,
'lazy_load_threshold' => '',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['profile'] = [
'#title' => $this->t('Sirv profile'),
'#type' => 'textfield',
'#description' => $this->t('The name of a Sirv profile to use for processing images in this field.'),
'#default_value' => $this->getSetting('profile'),
];
$element['extra_options'] = [
'#title' => $this->t('Extra Sirv options'),
'#type' => 'textfield',
'#description' => $this->t('These options will override options from the profile. Example of format: %example', ['%example' => 'w=250&grayscale=true']),
'#default_value' => $this->getSetting('extra_options'),
];
$element['responsive'] = [
'#title' => $this->t('Make responsive'),
'#type' => 'checkbox',
'#description' => $this->t('Enable responsive behavior for this field.'),
'#default_value' => $this->getSetting('responsive'),
];
$element['responsive_scale_crop'] = [
'#title' => $this->t('Automatic scale/crop'),
'#type' => 'select',
'#description' => $this->t('The method by which to scale and crop an image to fit a container.'),
'#default_value' => $this->getSetting('responsive_scale_crop'),
'#options' => [
'contain' => $this->t('Contain (default)'),
'cover' => $this->t('Cover'),
'crop' => $this->t('Crop'),
],
];
$element['responsive_resize_step'] = [
'#title' => $this->t('Resize step'),
'#type' => 'number',
'#description' => $this->t('The change in browser width required for a new responsive image to be generated.'),
'#default_value' => $this->getSetting('responsive_resize_step'),
'#size' => 10,
'#field_suffix' => $this->t('px'),
];
$element['lazy_load'] = [
'#title' => $this->t('Use lazy loading'),
'#type' => 'checkbox',
'#description' => $this->t('Enable lazy loading for this field.'),
'#default_value' => $this->getSetting('lazy_load'),
];
$element['lazy_load_threshold'] = [
'#title' => $this->t('Lazy load threshold'),
'#type' => 'textfield',
'#description' => $this->t('A value, in either number of pixels or percentage, to indicate when to lazy load the image before it comes into view. (Examples: 200, 50%)'),
'#default_value' => $this->getSetting('lazy_load_threshold'),
'#size' => 10,
];
$image_styles = image_style_options(FALSE);
$element['image_style'] = [
'#title' => $this->t('Image style'),
'#type' => 'select',
'#default_value' => $this->getSetting('image_style'),
'#empty_option' => $this->t('None (original image)'),
'#options' => $image_styles,
'#description' => $this->t('The original image is usually desired, though an image style may be selected if it provides an effect that is not available in Sirv, such as custom cropping.'),
];
$link_types = [
'content' => $this->t('Content'),
'file' => $this->t('File'),
];
$element['image_link'] = [
'#title' => $this->t('Link image to'),
'#type' => 'select',
'#default_value' => $this->getSetting('image_link'),
'#empty_option' => $this->t('Nothing'),
'#options' => $link_types,
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
if (!empty($this->getSetting('profile'))) {
$summary[] = $this->t('Sirv profile: @profile', ['@profile' => $this->getSetting('profile')]);
}
if (!empty($this->getSetting('extra_options'))) {
$summary[] = $this->t('Extra Sirv options');
}
if (!empty($this->getSetting('responsive'))) {
$summary[] = $this->t('Responsive');
}
if (!empty($this->getSetting('lazy_load'))) {
$summary[] = $this->t('Lazy load');
}
if (!empty($this->getSetting('image_style'))) {
$summary[] = $this->t('Image style: @style', ['@style' => $this->getSetting('image_style')]);
}
$link_types = [
'content' => $this->t('Linked to content'),
'file' => $this->t('Linked to file'),
];
$image_link_setting = $this->getSetting('image_link');
if (isset($link_types[$image_link_setting])) {
$summary[] = $link_types[$image_link_setting];
}
return $summary;
}
/**
* {@inheritdoc}
*/
protected function processElement(array $element) {
$element = parent::processElement($element);
$element['#sirv']['type'] = 'image';
return $element;
}
}
