From bd1ce547f7a8eee86d24c0612136af7bb625bf5a Mon Sep 17 00:00:00 2001 From: Konstantinos Platis <konstantinos.platis@cern.ch> Date: Tue, 25 Jun 2019 16:59:16 +0200 Subject: [PATCH] Fixed issue of event full content pattern not displaying link in event_type --- .../event-display/event-display.html.twig | 26 +++++----- .../CernComponentsTwigExtension.php | 47 +++++++++++++++++-- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/patterns/molecules/event-display/event-display.html.twig b/patterns/molecules/event-display/event-display.html.twig index e587d882..976458ae 100755 --- a/patterns/molecules/event-display/event-display.html.twig +++ b/patterns/molecules/event-display/event-display.html.twig @@ -110,16 +110,21 @@ {% if link | render %} <div class="event-node-full-content-place"> {% if link | render %} - {% if cern_node_type != "event_page" %} - <div class="hard-text"> - <p> - <a href="{{ link }}" target="_blank">{{ 'Go to Indico Event'|trans }}</a> - </p> - </div> - <div class="custom-text"> - <p></p> - </div> - {% endif %} + <div class="hard-text"> + {# If the link is a URL (not plain text)#} + {% if is_url(link) %} + <p> + <a href="{{ link | get_url_link }}" target="_blank">{{ 'Go to Indico Event'|trans }}</a> + </p> + {% else %} + <p> + <a href="{{ link }}" target="_blank">{{ 'Go to Indico Event'|trans }}</a> + </p> + {% endif %} + </div> + <div class="custom-text"> + <p></p> + </div> {% endif %} </div> {% endif %} @@ -136,7 +141,6 @@ <p>{{ 'Where: '|trans }} </p> </div> <div class="custom-text"> - {% if map_room | render %} <a href="{{ map_room }}"> {{ room }} </a> {% if room | render %} {% if place | render %} {{ 'at'|trans }} {% endif %} {% endif %} {{ place }} {% else %} diff --git a/src/TwigExtension/CernComponentsTwigExtension.php b/src/TwigExtension/CernComponentsTwigExtension.php index fef3331b..1f259050 100755 --- a/src/TwigExtension/CernComponentsTwigExtension.php +++ b/src/TwigExtension/CernComponentsTwigExtension.php @@ -54,6 +54,8 @@ class CernComponentsTwigExtension extends \Twig_Extension { */ public function getFilters() { return [ + new \Twig_SimpleFilter('is_url', [$this,'isURL']), + new \Twig_SimpleFilter('get_url_link', [$this,'getUrlLink']), new \Twig_SimpleFilter('addbasepath', [$this, 'addbasepath']), new \Twig_SimpleFilter('htmlspecialchars', [$this, 'htmlSpecialChars']), new \Twig_SimpleFilter('openlink', [$this, 'openLink']), @@ -77,7 +79,7 @@ class CernComponentsTwigExtension extends \Twig_Extension { new \Twig_SimpleFilter('get_tag_attribute', [$this,'getTagAttribute']), new \Twig_SimpleFilter('set_tag_attribute', [$this,'setTagAttribute']), new \Twig_SimpleFilter('get_path', [$this,'getPath']), - ]; + ]; } /** @@ -85,6 +87,8 @@ class CernComponentsTwigExtension extends \Twig_Extension { */ public function getFunctions() { return [ + new \Twig_SimpleFunction('is_url', [$this,'isURL']), + new \Twig_SimpleFunction('get_url_link', [$this,'getUrlLink']), new \Twig_SimpleFunction('get_current_language', [$this, 'getCurrentLanguage']), new \Twig_SimpleFunction('addbasepath', [$this, 'addBasePath']), new \Twig_SimpleFunction('htmlspecialchars', [$this, 'htmlSpecialChars']), @@ -109,10 +113,47 @@ class CernComponentsTwigExtension extends \Twig_Extension { new \Twig_SimpleFunction('get_cds_info', [$this,'getCdsInfo']), new \Twig_SimpleFunction('get_tag_attribute', [$this,'getTagAttribute']), new \Twig_SimpleFunction('set_tag_attribute', [$this,'setTagAttribute']), - new \Twig_SimpleFilter('get_path', [$this,'getPath']), - ]; + ]; } + /** + * Checks if the given variable is a URL + * + * @param $variable + * @return bool + */ + public function isURL($variable) { + reset($variable); + $first_key = key($variable); + + if (array_key_exists('#type', $variable[$first_key]['0']) && $variable[$first_key]['0']['#type'] == "link"){ + return true; + } + else{ + return false; + } + } + + /** + * Returns the URL of a link. Developed because for pattern I don't know in advance the field name so I get the first + * one. + * + * @param $link + * @return string + */ + public function getUrlLink($link){ + reset($link); + $first_key = key($link); + + if ( array_key_exists('#url', $link[$first_key]['0'])){ + return $link[$first_key]['0']['#url']; + } + else{ + return ''; + } + } + + /** * Returns the current language of the * -- GitLab