diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml index de28bb2..0006ad7 100644 --- a/core/modules/image/config/schema/image.schema.yml +++ b/core/modules/image/config/schema/image.schema.yml @@ -141,11 +141,8 @@ field.formatter.settings.image: field.formatter.settings.image_url: type: mapping - label: 'Image URL format settings' + label: 'Image URL formatter settings' mapping: - image_link: - type: string - label: 'Link image to' image_style: type: string label: 'Image style' diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php index 72c7721..cfebfa8 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php @@ -7,6 +7,7 @@ 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; @@ -39,7 +40,7 @@ protected $imageStyleStorage; /** - * Constructs an ImageFormatter object. + * Constructs an ImageFormatterBase object. * * @param string $plugin_id * The plugin_id for the formatter. @@ -50,11 +51,11 @@ * @param array $settings * The formatter settings. * @param string $label - * The formatter label display setting. + * The formatter label setting. * @param string $view_mode * The view mode. * @param array $third_party_settings - * Any third party settings settings. + * Any third party settings. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. */ @@ -86,10 +87,10 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public static function defaultSettings() { - return array( + return [ 'image_style' => '', 'image_link' => '', - ) + parent::defaultSettings(); + ] + parent::defaultSettings(); } /** @@ -98,30 +99,30 @@ public static function defaultSettings() { public function settingsForm(array $form, FormStateInterface $form_state) { $image_styles = image_style_options(FALSE); $description_link = Link::fromTextAndUrl( - $this->t('Configure Image Styles'), + $this->$this->t('Configure image styles'), Url::fromRoute('entity.image_style.collection') ); $element['image_style'] = [ - '#title' => t('Image style'), + '#title' => $this->t('Image style'), '#type' => 'select', '#default_value' => $this->getSetting('image_style'), - '#empty_option' => t('None (original image)'), + '#empty_option' => $this->t('None (original image)'), '#options' => $image_styles, '#description' => $description_link->toRenderable() + [ - '#access' => $this->currentUser->hasPermission('administer image styles') - ], + '#access' => AccessResult::allowedIfHasPermission($this->currentUser, 'administer image styles'), + ], ]; - $link_types = array( - 'content' => t('Content'), - 'file' => t('File'), - ); - $element['image_link'] = array( - '#title' => t('Link image to'), + $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' => t('Nothing'), + '#empty_option' => $this->t('Nothing'), '#options' => $link_types, - ); + ]; return $element; } @@ -130,25 +131,24 @@ public function settingsForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function settingsSummary() { - $summary = array(); + $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])); + $summary[] = $this->t('Image style: @style', ['@style' => $image_styles[$image_style_setting]]); } else { - $summary[] = t('Original image'); + $summary[] = $this->t('Original image'); } - $link_types = array( - 'content' => t('Linked to content'), - 'file' => t('Linked to file'), - ); + $link_types = [ + 'content' => $this->t('Linked to content'), + 'file' => $this->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])) { @@ -175,7 +175,7 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item // so that the fallback image can be rendered without affecting the // field values in the entity being rendered. $items = clone $items; - $items->setValue(array( + $items->setValue([ 'target_id' => $file->id(), 'alt' => $default_image['alt'], 'title' => $default_image['title'], @@ -184,7 +184,7 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item 'entity' => $file, '_loaded' => TRUE, '_is_default' => TRUE, - )); + ]); $file->_referringItem = $items[0]; } } @@ -196,7 +196,7 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item * {@inheritdoc} */ public function calculateDependencies() { - // Make sure to include 3rd party dependencies. + // Make sure to include third party dependencies. $dependencies = parent::calculateDependencies(); // Check for image style. @@ -207,4 +207,5 @@ public function calculateDependencies() { return $dependencies; } + } diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php index 06ab823..72c3f23 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php @@ -1,13 +1,14 @@ getSetting('image_link'); - // Url to be linked to. + // URL to be linked to. $link_url = FALSE; // Check if the formatter involves a link. if ($image_link_setting == 'content') { @@ -58,16 +71,16 @@ public function viewElements(FieldItemListInterface $items, $langcode) { ? $image_style->buildUrl($image_uri) : file_create_url($image_uri); - // Set the link url if settings require such. + // Set the link URL if settings require such. $link_url = ($image_link_setting == 'file') ? Url::fromUri($url) : $link_url; // Add cacheable metadata from the image and image style. $cacheable_metadata = CacheableMetadata::createFromObject($image); if ($image_style) { - $cacheable_metadata = $cacheable_metadata->merge(CacheableMetadata::createFromObject($image_style)); + $cacheable_metadata->addCacheableDependency(CacheableMetadata::createFromObject($image_style)); } - // Add a link if we have a valid link url. + // Add a link if we have a valid link URL. if ($link_url instanceof Url) { $elements[$delta] = Link::fromTextAndUrl($url, $link_url)->toRenderable(); }