mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 14:05:56 +02:00
NEW: Add metadata to DMS links.
Shortlink links are now augmented with data attributes containing the file extension and size.
This commit is contained in:
parent
2c22ff476d
commit
76c4692d13
@ -5,28 +5,35 @@
|
|||||||
*/
|
*/
|
||||||
class DMSShortcodeHandler {
|
class DMSShortcodeHandler {
|
||||||
|
|
||||||
public static function handle($arguments, $content = null, $parser = null) {
|
public static function handle(
|
||||||
$linkText = null;
|
$arguments, $content, ShortcodeParser $parser, $tag, array $extra = array()
|
||||||
|
) {
|
||||||
if(!empty($arguments['id'])) {
|
if(!empty($arguments['id'])) {
|
||||||
$document = DMSDocument::get()->filter(array('ID' => $arguments['id']))->First();
|
$document = DMSDocument::get()->byID($arguments['id']);
|
||||||
|
|
||||||
if($document && !$document->isHidden()) {
|
if($document && !$document->isHidden()) {
|
||||||
if (!empty($content)) {
|
if($content) {
|
||||||
$linkText = sprintf('<a href="%s">%s</a>', $document->Link(), $parser->parse($content));
|
return sprintf(
|
||||||
|
'<a href="%s">%s</a>', $document->Link(), $parser->parse($content)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
$linkText = $document->Link();
|
if(isset($extra['element'])) {
|
||||||
|
$extra['element']->setAttribute('data-ext', $document->getExtension());
|
||||||
|
$extra['element']->setAttribute('data-size', $document->getFileSizeFormatted());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $document->Link();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($linkText)) {
|
$error = ErrorPage::get()->filter('ErrorCode', '404')->First();
|
||||||
$errorPage = ErrorPage::get()->filter(array('ErrorCode' => '404'))->First();
|
|
||||||
if ($errorPage) {
|
if($error) {
|
||||||
$linkText = $errorPage->Link();
|
return $error->Link();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $linkText;
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
23
tests/DMSShortcodeTest.php
Normal file
23
tests/DMSShortcodeTest.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Tests DMS shortcode linking functionality.
|
||||||
|
*/
|
||||||
|
class DMSShortcodeTest extends SapphireTest {
|
||||||
|
|
||||||
|
public function testShortcodeOperation() {
|
||||||
|
$file = 'dms/tests/DMS-test-lorum-file.pdf';
|
||||||
|
$document = DMS::inst()->storeDocument($file);
|
||||||
|
|
||||||
|
$result = ShortcodeParser::get('default')->parse(sprintf(
|
||||||
|
'<p><a href="[dms_document_link,id=\'%d\']">Document</a></p>', $document->ID
|
||||||
|
));
|
||||||
|
|
||||||
|
$value = Injector::inst()->create('HTMLValue', $result);
|
||||||
|
$link = $value->query('//a')->item(0);
|
||||||
|
|
||||||
|
$this->assertEquals("/dmsdocument/$document->ID", $link->getAttribute('href'));
|
||||||
|
$this->assertEquals($document->getExtension(), $link->getAttribute('data-ext'));
|
||||||
|
$this->assertEquals($document->getFileSizeFormatted(), $link->getAttribute('data-size'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user