diff --git a/_config/config.yml b/_config/config.yml index 6acd465..6d12258 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -4,5 +4,7 @@ Name: accessibility-config Iliain\Accessible\Config: settings: enable_image_shortcode: true # (Boolean) Enable the image shortcode feature + enable_link_shortcode: true # (Boolean) Enable the link shortcode feature customise: image_shortcode_template: 'Iliain\Accessible\Includes\AccessibleShortcodeImage' # (String) Template to use for the image shortcode + link_shortcode_template: 'Iliain\Accessible\Includes\AccessibleShortcodeLink' # (String) Template to use for the link shortcode diff --git a/src/Extensions/AccLinkExtension.php b/src/Extensions/AccLinkExtension.php index b9c0a28..92085f3 100644 --- a/src/Extensions/AccLinkExtension.php +++ b/src/Extensions/AccLinkExtension.php @@ -48,9 +48,9 @@ public function updateCMSFields(FieldList $fields) * * @return string */ - public function getBaseAccessibleType() + public function getBaseAccessibleType($url = null) { - $url = $this->owner->getLinkURL(); + if (!$url) $url = $this->owner->getLinkURL(); // @todo implement additional checks for differences between the two modules if necessary if ((class_exists("gorriecoe\\Link\\Models\\Link") && $this->owner->ClassName == GorrieCoeLink::class) || diff --git a/src/Extensions/AccShortcodeExtension.php b/src/Extensions/AccShortcodeExtension.php index ce404f8..d865c06 100644 --- a/src/Extensions/AccShortcodeExtension.php +++ b/src/Extensions/AccShortcodeExtension.php @@ -5,14 +5,16 @@ use DOMDocument; use SilverStripe\Assets\Image; use SilverStripe\Core\Extension; +use SilverStripe\View\ArrayData; +use SilverStripe\Control\Director; use SilverStripe\Admin\LeftAndMain; +use SilverStripe\View\ViewableData; use SilverStripe\Control\Controller; use SilverStripe\Core\Config\Config; -use SilverStripe\View\ArrayData; -use SilverStripe\View\ViewableData; /** - * Extends the shortcode parser to allow AltText and Captions to appear in WYSIWYG images + * Extends the shortcode parser to allow AltText and Captions to appear in WYSIWYG images, + * and to add accessible information to links * * @package silverstripe * @subpackage silverstripe-accessible @@ -27,20 +29,24 @@ class AccShortcodeExtension extends Extension */ public function onAfterParse(&$content) { - $config = Config::inst()->get('Iliain\Accessible\Config', 'settings')['enable_image_shortcode']; + $isCMS = Controller::curr() instanceof LeftAndMain; - if ($config) { - $isCMS = Controller::curr() instanceof LeftAndMain; - - if (!$isCMS) { - if ($content) { - $doc = new DOMDocument(); - @$doc->loadHTML($content); + if (!$isCMS) { + if ($content) { + $doc = new DOMDocument(); + @$doc->loadHTML($content); + $config = Config::inst()->get('Iliain\Accessible\Config', 'settings')['enable_image_shortcode']; + if ($config) { $content = $this->applyImageTemplate($doc); } + + $config = Config::inst()->get('Iliain\Accessible\Config', 'settings')['enable_link_shortcode']; + if ($config) { + $content = $this->applyLinkTemplate($doc); + } } - } + } } /** @@ -53,7 +59,7 @@ public function applyImageTemplate($doc) { $template = Config::inst()->get('Iliain\Accessible\Config', 'customise')['image_shortcode_template']; if (!$template) { - return; + return $doc; } $tags = $doc->getElementsByTagName('img'); @@ -86,4 +92,82 @@ public function applyImageTemplate($doc) return $doc->saveHTML(); } + + /** + * Alter and return the links in an accessible format + * + * @param string $content + * @return void + */ + public function applyLinkTemplate($doc) + { + $template = Config::inst()->get('Iliain\Accessible\Config', 'customise')['link_shortcode_template']; + if (!$template) { + return $doc; + } + + $tags = $doc->getElementsByTagName('a'); + foreach ($tags as $tag) { + $attributeArr = []; + + if ($tag->hasAttributes()) { + foreach ($tag->attributes as $attr) { + $attributeArr[$attr->nodeName] = $attr->nodeValue; + } + } + + $attributeArr['text'] = $tag->nodeValue; + $attributeArr['type'] = $this->checkLinkType($attributeArr['href']); + + $attrData = ArrayData::create($attributeArr); + $viewableData = ViewableData::create(); + $render = $viewableData->renderWith($template, $attrData); + + $newNode = $doc->createDocumentFragment(); + $newNode->appendXML($render->getValue()); + $tag->parentNode->replaceChild($newNode, $tag); + } + + return $doc->saveHTML(); + } + + public function checkLinkType($url) + { + $currentDomain = Director::absoluteBaseURL(); + $downloadFileTypes = [ + '.pdf', '.doc', '.docx', '.mp4', '.mp3', '.exe' + ]; + + if (strpos($url, 'mailto:') !== false) { + return 'Email'; + } else if (strpos($url, 'tel:') !== false) { + return 'Phone'; + } else { + if (strpos($url, 'http') === false) { + $url = Director::absoluteURL($url); + } + + if (strpos($url, $currentDomain) !== false) { + if (strpos($url, '#') !== false) { + return 'Anchor'; + } + + foreach ($downloadFileTypes as $type) { + if (strpos($url, $type) !== false) { + return 'Download'; + } + } + + return 'Internal'; + } else { + foreach ($downloadFileTypes as $type) { + if (strpos($url, $type) !== false) { + return 'Download'; + } + } + + return 'External'; + } + } + } } diff --git a/templates/Iliain/Accessible/Includes/AccessibleShortcodeLink.ss b/templates/Iliain/Accessible/Includes/AccessibleShortcodeLink.ss new file mode 100644 index 0000000..826d593 --- /dev/null +++ b/templates/Iliain/Accessible/Includes/AccessibleShortcodeLink.ss @@ -0,0 +1,14 @@ +target="{$target}"<% end_if %>> + {$text} + <% if $type = 'External' || $target %> + + <% else_if $type = 'Anchor' %> + + <% else_if $type = 'Download' %> + + <% else_if $type = 'Email' %> + + <% else_if $type = 'Phone' %> + + <% end_if %> +