diff --git a/image_link_attributes.module b/image_link_attributes.module index a89fbd212d44daf87c473c0462d3b97f248e5226..a21974a4968889b7724c8d4feadd6a77b1653655 100644 --- a/image_link_attributes.module +++ b/image_link_attributes.module @@ -9,6 +9,8 @@ use Drupal\Core\Render\Element; use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Component\Utility\Unicode; +use Drupal\image\Entity\ImageStyle; +use Drupal\file\Entity\File; /** * Implements hook_help(). @@ -44,6 +46,7 @@ function image_link_attributes_field_formatter_settings_summary_alter(&$summary, // Normalize the settings. $extended = $settings['extended']; + $alternate_image = $settings['alternate_images']; $summary_items = []; @@ -80,6 +83,17 @@ function image_link_attributes_field_formatter_settings_summary_alter(&$summary, $summary[] = $list; } + + if ($alternate_image) { + $alternate_image_style = $settings['alternate_image_styles']; + $image_list = [ + '#theme' => 'item_list', + '#items' => ['Image Style' => $alternate_image_style], + '#title' => 'Linked Image Style', + ]; + + $summary[] = $image_list; + } } /** @@ -149,6 +163,38 @@ function image_link_attributes_field_formatter_third_party_settings_form($plugin '#default_value' => $settings['advanced'][$attribute] ?? '', ]; } + + // Settings for alternate images. + if (!array_key_exists('alternate_images', $settings)) { + $settings['alternate_images'] = 0; + } + + // Checkbox for alternate images. + $element['alternate_images'] = [ + '#title' => t('Link to alternate Image Style?'), + '#type' => 'checkbox', + '#default_value' => $settings['alternate_images'], + '#states' => [ + 'invisible' => [ + ['select[name$="[settings][image_link]"]' => [['value' => ''],['value' => 'content'],]], + ], + ], + ]; + + // Options for Alternate Image Styles + $element['alternate_image_styles'] = [ + '#type' => 'select', + '#title' => t('Alternate Image Styles'), + '#default_value' => $settings['alternate_image_styles'], + '#options' => image_style_options(FALSE), + '#states' => [ + 'invisible' => [ + ['select[name$="[settings][image_link]"]' => [['value' => ''],['value' => 'content'],]], + ['input[name$="[third_party_settings][image_link_attributes][alternate_images]"]' => ['unchecked' => TRUE]], + ], + ], + ]; + return $element; } } @@ -176,16 +222,32 @@ function image_link_attributes_preprocess_field(array &$variables) { $is_enabled = $custom_settings['extended'] ?? FALSE; + // If there is an alternate image style, get it, assign path. + $alternate_image = $custom_settings['alternate_images']; + if ($alternate_image) { + $alternate_image_style = $custom_settings['alternate_image_styles']; + $style = ImageStyle::load($alternate_image_style); + foreach (Element::children($items) as $child_name) { + $delta =& $items[$child_name]; + $content =& $delta['content']; + $alternate_path = $content['#alternate_path'] ?? ""; + $image_id = $items[$child_name]['content']['#item']->getValue()['target_id']; + if (!empty($image_id)) { + $image_file = File::load($image_id); + $alternate_path = $style->buildUrl($image_file->uri->value); + $content['#alternate_path'] = $alternate_path; + } + } + } + + // If there are advanced attributes, get them and assign them. if ($is_enabled) { $advanced_attributes = array_filter($custom_settings['advanced']); - foreach (Element::children($items) as $child_name) { $delta =& $items[$child_name]; $content =& $delta['content']; - $link_attributes = $content['#link_attributes'] ?? []; $link_attributes = array_merge($link_attributes, $advanced_attributes); - $content['#link_attributes'] = $link_attributes; } } @@ -196,11 +258,8 @@ function image_link_attributes_preprocess_field(array &$variables) { * Implements hook_theme_registry_alter(). */ function image_link_attributes_theme_registry_alter(array &$theme_registry) { - $module_path = - \Drupal::service('extension.list.module')->getPath('image_link_attributes'); - - $theme_registry['image_formatter']['path'] = - $module_path . '/templates/field'; + $module_path = \Drupal::service('extension.list.module')->getPath('image_link_attributes'); + $theme_registry['image_formatter']['path'] = $module_path . '/templates/field'; } /** @@ -215,6 +274,7 @@ function image_link_attributes_theme($existing, $type, $theme, $path) { 'link_attributes' => [], 'url' => NULL, 'image_style' => NULL, + 'alternate_path' => NULL, ], ], ]; diff --git a/templates/field/image-formatter.html.twig b/templates/field/image-formatter.html.twig index 7d397af9ab2f529eefcb19796ae068f422bfb784..cbb42b6ad703d1a2364a2a3012ff31b6524026e6 100644 --- a/templates/field/image-formatter.html.twig +++ b/templates/field/image-formatter.html.twig @@ -14,7 +14,11 @@ #} {% if url %} - {{ link(image, url, link_attributes) }} + {% if alternate_path|trim is not empty %} + {{ link(image, alternate_path, link_attributes) }} + {% else %} + {{ link(image, url, link_attributes) }} + {% endif %} {% else %} {{ image }} {% endif %}