Source of file RichLinksExtension.php
Size: 1,715 Bytes - Last Modified: 2021-12-23T10:34:47+00:00
/var/www/docs.ssmods.com/process/src/src/extensions/RichLinksExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 | <?php use SilverStripe\Assets\File; use SilverStripe\ORM\DataObject; use SilverStripe\Core\Extension; /** * Adds capability to augment links with extra attributes and meta information. * * Usage in the templates: * $Content.RichLinks * * Note: will only work with content produced by HtmlEditorField. */ class RichLinksExtension extends Extension { private static $casting = array( 'RichLinks' => 'HTMLText' ); public function RichLinks() { // Note: // Assume we can use Regexes because the link will always be formatted // in the same way coming from the CMS. $content = $this->owner->value; // Find all file links for processing. preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches); // Attach the file type and size to each of the links. for ($i = 0; $i < count($matches[0]); $i++){ $file = DataObject::get_by_id(File::class, $matches[1][$i]); if ($file) { $size = $file->getSize(); $ext = strtoupper($file->getExtension()); // Replace the closing </a> tag with the size span (and reattach the closing tag). $newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4) . "<span class='fileExt'> [$ext, $size]</span></a>"; $content = str_replace($matches[0][$i], $newLink, $content); } } // Inject extra attributes into the external links. $pattern = '/(<a.*)(href=\"https?:\/\/[^\"]*\"[^>]*>.*)(<\/a>)/iU'; $replacement = sprintf( '$1class="external" rel="external" title="%s" $2 <span class="nonvisual-indicator">(external link)</span> $3', _t('RichLinks.OpenLinkTitle', 'Open external link') ); $content = preg_replace($pattern, $replacement, $content, -1); return $content; } } |