diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php index 278ebeb..029e7a5 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php @@ -7,10 +7,17 @@ namespace Drupal\image\Plugin\Field\FieldFormatter; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Link; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Url; +use Drupal\image\Entity\ImageStyle; +use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Cache\Cache; -use Drupal\Core\Url; /** * Plugin implementation of the 'image' formatter. @@ -23,32 +30,105 @@ * } * ) */ -class ImageFormatter extends ImageFormatterBase { +class ImageFormatter 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. + */ + 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.manager')->getStorage('image_style') + ); + } /** * {@inheritdoc} */ public static function defaultSettings() { - return ['image_link' => ''] + parent::defaultSettings(); + return array( + 'image_style' => '', + 'image_link' => '', + ) + parent::defaultSettings(); } /** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $element = parent::settingsForm($form, $form_state); - - $link_types = [ - 'content' => $this->t('Content'), - 'file' => $this->t('File'), + $image_styles = image_style_options(FALSE); + $description_link = Link::fromTextAndUrl( + $this->t('Configure Image Styles'), + Url::fromRoute('entity.image_style.collection') + ); + $element['image_style'] = [ + '#title' => t('Image style'), + '#type' => 'select', + '#default_value' => $this->getSetting('image_style'), + '#empty_option' => t('None (original image)'), + '#options' => $image_styles, + '#description' => $description_link->toRenderable() + [ + '#access' => $this->currentUser->hasPermission('administer image styles') + ], ]; - $element['image_link'] = [ - '#title' => $this->t('Link image to'), + $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' => $this->t('Nothing'), + '#empty_option' => t('Nothing'), '#options' => $link_types, - ]; + ); return $element; } @@ -57,14 +137,28 @@ public function settingsForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function settingsSummary() { - $summary = parent::settingsSummary(); - $link_types = [ - 'content' => $this->t('Linked to content'), - 'file' => $this->t('Linked to file'), - ]; + $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 ($image_link_setting && isset($link_types[$image_link_setting])) { + if (isset($link_types[$image_link_setting])) { $summary[] = $link_types[$image_link_setting]; } @@ -76,7 +170,6 @@ public function settingsSummary() { */ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = array(); - /** @var \Drupal\file\Entity\File[] $files */ $files = $this->getEntitiesToView($items, $langcode); // Early opt-out if the field is empty. @@ -142,4 +235,41 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + $dependencies = parent::calculateDependencies(); + $style_id = $this->getSetting('image_style'); + /** @var \Drupal\image\ImageStyleInterface $style */ + if ($style_id && $style = ImageStyle::load($style_id)) { + // If this formatter uses a valid image style to display the image, add + // the image style configuration entity as dependency of this formatter. + $dependencies[$style->getConfigDependencyKey()][] = $style->getConfigDependencyName(); + } + return $dependencies; + } + + /** + * {@inheritdoc} + */ + public function onDependencyRemoval(array $dependencies) { + $changed = parent::onDependencyRemoval($dependencies); + $style_id = $this->getSetting('image_style'); + /** @var \Drupal\image\ImageStyleInterface $style */ + if ($style_id && $style = ImageStyle::load($style_id)) { + if (!empty($dependencies[$style->getConfigDependencyKey()][$style->getConfigDependencyName()])) { + $replacement_id = $this->imageStyleStorage->getReplacementId($style_id); + // If a valid replacement has been provided in the storage, replace the + // image style with the replacement and signal that the formatter plugin + // settings were updated. + if ($replacement_id && ImageStyle::load($replacement_id)) { + $this->setSetting('image_style', $replacement_id); + $changed = TRUE; + } + } + } + return $changed; + } + } diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php index e54c4e1..899aa49 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php @@ -7,141 +7,14 @@ namespace Drupal\image\Plugin\Field\FieldFormatter; -use Drupal\Core\Access\AccessResult; -use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; -use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Link; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Session\AccountInterface; -use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase; -use Drupal\image\Entity\ImageStyle; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class for image file formatters. */ -abstract class ImageFormatterBase extends FileFormatterBase implements ContainerFactoryPluginInterface { - - /** - * The current user service. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** - * The image style entity storage. - * - * @var \Drupal\Core\Entity\EntityStorageInterface - */ - protected $imageStyleStorage; - - /** - * The entity repository service. - * - * @var \Drupal\Core\Entity\EntityRepositoryInterface - */ - protected $entityRepository; - - /** - * Constructs an ImageFormatterBase 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 setting. - * @param string $view_mode - * The view mode. - * @param array $third_party_settings - * Any third party settings. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user. - * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage - * The image style entity 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 ['image_style' => ''] + parent::defaultSettings(); - } - - /** - * {@inheritdoc} - */ - public function settingsForm(array $form, FormStateInterface $form_state) { - $image_styles = image_style_options(FALSE); - $description_link = Link::fromTextAndUrl( - $this->t('Configure image styles'), - Url::fromRoute('entity.image_style.collection') - ); - $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' => $description_link->toRenderable() + [ - '#access' => AccessResult::allowedIfHasPermission($this->currentUser, 'administer image styles'), - ], - ]; - - return $element; - } - - /** - * {@inheritdoc} - */ - public function settingsSummary() { - $summary = []; - - $image_styles = image_style_options(FALSE); - // Unset possible 'No defined styles' option. - unset($image_styles['']); - - $image_style_setting = $this->getSetting('image_style'); - if (isset($image_styles[$image_style_setting])) { - $summary[] = $this->t('Image style: @style', ['@style' => $image_styles[$image_style_setting]]); - } - else { - $summary[] = $this->t('Original image'); - } - - return $summary; - } +abstract class ImageFormatterBase extends FileFormatterBase { /** * {@inheritdoc} @@ -155,12 +28,12 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) { $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image'); } - if (!empty($default_image['uuid']) && $file = $this->getEntityRepository()->loadEntityByUuid('file', $default_image['uuid'])) { + if (!empty($default_image['uuid']) && $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid'])) { // Clone the FieldItemList into a runtime-only object for the formatter, // so that the fallback image can be rendered without affecting the // field values in the entity being rendered. $items = clone $items; - $items->setValue([ + $items->setValue(array( 'target_id' => $file->id(), 'alt' => $default_image['alt'], 'title' => $default_image['title'], @@ -169,7 +42,7 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item 'entity' => $file, '_loaded' => TRUE, '_is_default' => TRUE, - ]); + )); $file->_referringItem = $items[0]; } } @@ -177,55 +50,4 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item return parent::getEntitiesToView($items, $langcode); } - /** - * {@inheritdoc} - */ - public function calculateDependencies() { - $dependencies = parent::calculateDependencies(); - $style_id = $this->getSetting('image_style'); - /** @var \Drupal\image\ImageStyleInterface $style */ - if ($style_id && $style = ImageStyle::load($style_id)) { - // If this formatter uses a valid image style to display the image, add - // the image style configuration entity as dependency of this formatter. - $dependencies[$style->getConfigDependencyKey()][] = $style->getConfigDependencyName(); - } - return $dependencies; - } - - /** - * {@inheritdoc} - */ - public function onDependencyRemoval(array $dependencies) { - $changed = parent::onDependencyRemoval($dependencies); - $style_id = $this->getSetting('image_style'); - /** @var \Drupal\image\ImageStyleInterface $style */ - if ($style_id && $style = ImageStyle::load($style_id)) { - if (!empty($dependencies[$style->getConfigDependencyKey()][$style->getConfigDependencyName()])) { - $replacement_id = $this->imageStyleStorage->getReplacementId($style_id); - // If a valid replacement has been provided in the storage, replace the - // image style with the replacement and signal that the formatter plugin - // settings were updated. - if ($replacement_id && ImageStyle::load($replacement_id)) { - $this->setSetting('image_style', $replacement_id); - $changed = TRUE; - } - } - } - return $changed; - } - - /** - * Returns the entity repository service. - * - * @return \Drupal\Core\Entity\EntityRepositoryInterface - * - * @todo Inject this service in constructor, in Drupal 9.0.x. - */ - protected function getEntityRepository() { - if (!isset($this->entityRepository)) { - $this->entityRepository = \Drupal::service('entity.repository'); - } - return $this->entityRepository; - } - } diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php index 8c7efa9..7d526df 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php @@ -25,16 +25,15 @@ * } * ) */ -class ImageUrlFormatter extends ImageFormatterBase { +class ImageUrlFormatter extends ImageFormatter { /** * {@inheritdoc} */ public static function defaultSettings() { return [ - 'url_link' => FALSE, - 'trim_length' => 80, - ] + parent::defaultSettings(); + 'image_style' => '', + ]; } /** @@ -49,20 +48,8 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#description' => $this->t('If checked, the URL to image will get a click-able link otherwise, the plain URL will be displayed.'), '#default_value' => $this->getSetting('url_link'), ]; - $field_name = $this->fieldDefinition->getName(); - $element['trim_length'] = [ - '#type' => 'number', - '#title' => $this->t('Trim link text length'), - '#field_suffix' => $this->t('characters'), - '#default_value' => $this->getSetting('trim_length'), - '#min' => 1, - '#description' => $this->t('Leave blank to allow unlimited link text length.'), - '#states' => [ - 'visible' => [ - ':input[name="fields[' . $field_name . '][settings_edit_form][settings][url_link]"]' => ['checked' => TRUE], - ], - ], - ]; + + unset($element['image_link']);; return $element; } @@ -71,19 +58,19 @@ public function settingsForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function settingsSummary() { - $summary = parent::settingsSummary(); - - if (!$this->getSetting('url_link')) { - $summary[] = $this->t('Show URL as plain-text'); + $summary = []; + + $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 { - $trim_length = $this->getSetting('trim_length'); - if (empty($trim_length)) { - $summary[] = $this->t('Show URL as link'); - } - else { - $summary[] = $this->t('Show URL as link trimmed to @limit characters', ['@limit' => $trim_length]); - } + $summary[] = t('Original image'); } return $summary; @@ -117,19 +104,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $cacheable_metadata->addCacheableDependency(CacheableMetadata::createFromObject($image_style)); } - if (!$this->getSetting('url_link')) { - // Plain text URL. - $elements[$delta] = ['#markup' => $url]; - } - else { - // Link URL. - $text = $url; - // Trim the link text to the desired length. - if (!empty($trim_length = $this->getSetting('trim_length'))) { - $text = Unicode::truncate($text, $trim_length, FALSE, TRUE); - } - $elements[$delta] = Link::fromTextAndUrl($text, Url::fromUri($url))->toRenderable(); - } + // Plain text URL. + $elements[$delta] = ['#markup' => $url]; $cacheable_metadata->applyTo($elements[$delta]); } diff --git a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php index 6dc8620..e66cb82 100644 --- a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php +++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php @@ -12,6 +12,7 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Url; use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -29,13 +30,27 @@ * } * ) */ -class ResponsiveImageFormatter extends ImageFormatterBase { +class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFactoryPluginInterface { /** * @var EntityStorageInterface */ protected $responsiveImageStyleStorage; + /* + * The image style entity storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $imageStyleStorage; + + /** + * The current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + /** * The link generator. * @@ -70,9 +85,12 @@ class ResponsiveImageFormatter extends ImageFormatterBase { * The current user. */ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, EntityStorageInterface $image_style_storage, LinkGeneratorInterface $link_generator, AccountInterface $current_user) { - parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage); + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + $this->responsiveImageStyleStorage = $responsive_image_style_storage; + $this->imageStyleStorage = $image_style_storage; $this->linkGenerator = $link_generator; + $this->currentUser = $current_user; } /** @@ -101,7 +119,7 @@ public static function defaultSettings() { return array( 'responsive_image_style' => '', 'image_link' => '', - ); + ) + parent::defaultSettings(); } /**