diff --git a/helfi_media_map.module b/helfi_media_map.module index dd9589b..700b9d2 100644 --- a/helfi_media_map.module +++ b/helfi_media_map.module @@ -17,3 +17,15 @@ function helfi_media_map_entity_bundle_field_info_alter(&$fields, EntityTypeInte } } } + +/** + * Implements hook_theme(). + */ +function helfi_media_map_theme($existing, $type, $theme, $path) { + return [ + 'helfi_media_map' => [ + 'variables' => ['link' => NULL, 'iframe' => NULL], + 'template' => 'helfi-media-map' + ], + ]; +} diff --git a/src/Plugin/Field/FieldFormatter/MediaMapFormatter.php b/src/Plugin/Field/FieldFormatter/MediaMapFormatter.php index 238583c..4f30928 100644 --- a/src/Plugin/Field/FieldFormatter/MediaMapFormatter.php +++ b/src/Plugin/Field/FieldFormatter/MediaMapFormatter.php @@ -7,6 +7,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; use Drupal\helfi_media_map\UrlParserTrait; /** @@ -51,7 +52,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) : arra /** * {@inheritdoc} */ - public function viewElements(FieldItemListInterface $items, $langcode) { + public function viewElements(FieldItemListInterface $items, $langcode) :array { $element = []; foreach ($items as $delta => $item) { @@ -59,7 +60,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $link = $this->getMapUrl($uri); $element[$delta] = [ - 'iframe' => [ + '#theme' => 'helfi_media_map', + '#iframe' => [ '#type' => 'html_tag', '#tag' => 'iframe', '#value' => '', @@ -72,14 +74,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { ]; if ($link_title = $this->getSetting('link_title')) { - $element[$delta]['link'] = [ - '#type' => 'html_tag', - '#tag' => 'a', - '#value' => $link_title, - '#attributes' => [ - 'href' => $link, - 'target' => '_blank', - ], + $element[$delta]['#link'] = [ + '#type' => 'link', + '#title' => $link_title, + '#url' => Url::fromUri($link), + '#attributes' => ['target' => '_blank'], ]; } } diff --git a/templates/helfi-media-map.html.twig b/templates/helfi-media-map.html.twig new file mode 100644 index 0000000..ade7e89 --- /dev/null +++ b/templates/helfi-media-map.html.twig @@ -0,0 +1,2 @@ +{{ iframe }} +{{ link }}