Skip to content

Commit

Permalink
FEAT: added link shortcode feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliain committed Jul 26, 2023
1 parent 2996b4e commit b2678d1
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 15 deletions.
2 changes: 2 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/Extensions/AccLinkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
110 changes: 97 additions & 13 deletions src/Extensions/AccShortcodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
}
}

/**
Expand All @@ -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');
Expand Down Expand Up @@ -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';
}
}
}
}
14 changes: 14 additions & 0 deletions templates/Iliain/Accessible/Includes/AccessibleShortcodeLink.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<a href="{$href}" title="{$title}" <% if $target %>target="{$target}"<% end_if %>>
{$text}
<% if $type = 'External' || $target %>
<i class="fa-solid fa-up-right-from-square"></i>
<% else_if $type = 'Anchor' %>
<i class="fa-solid fa-anchor"></i>
<% else_if $type = 'Download' %>
<i class="fa-solid fa-cloud-arrow-down"></i>
<% else_if $type = 'Email' %>
<i class="fa-solid fa-envelope"></i>
<% else_if $type = 'Phone' %>
<i class="fa-solid fa-phone"></i>
<% end_if %>
</a>

0 comments on commit b2678d1

Please sign in to comment.