Merge pull request #128 from robbieaverill/feature/configurable-shortcode-handler-key

NEW Allow shortcode handler key to be configurable
This commit is contained in:
Franco Springveldt 2017-05-18 15:11:54 +12:00 committed by GitHub
commit 03fe480ca5
9 changed files with 81 additions and 7 deletions

View File

@ -11,7 +11,7 @@ if (!file_exists(BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR)) {
CMSMenu::remove_menu_item('DMSDocumentAddController');
ShortcodeParser::get('default')->register(
'dms_document_link',
$config->get('DMS', 'shortcode_handler_key'),
array('DMSShortcodeHandler', 'handle')
);

View File

@ -17,6 +17,13 @@ class DMS implements DMSInterface
*/
public static $dmsFolderSize = 1000;
/**
* The shortcode handler key. Can be changed by user code.
*
* @config
* @var string
*/
private static $shortcode_handler_key = 'dms_document_link';
/**
* Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface.
@ -166,4 +173,14 @@ class DMS implements DMSInterface
$folderName = intval($id / self::$dmsFolderSize);
return $folderName;
}
/**
* Get the shortcode handler key
*
* @return string
*/
public function getShortcodeHandlerKey()
{
return (string) Config::inst()->get('DMS', 'shortcode_handler_key');
}
}

View File

@ -138,7 +138,13 @@ class DMSDocumentSet extends DataObject
$addNewButton->setDocumentSetId($self->ID);
$fields->removeByName('Documents');
$fields->addFieldToTab('Root.Main', $gridField);
$fields->addFieldsToTab(
'Root.Main',
array(
$gridField,
HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey())
)
);
$self->addQueryFields($fields);
}
});

View File

@ -46,12 +46,13 @@ class ShortCodeRelationFinder
$list = DataList::create('SiteTree');
$where = array();
$fields = $this->getShortCodeFields('SiteTree');
$shortcode = DMS::inst()->getShortcodeHandlerKey();
foreach ($fields as $ancClass => $ancFields) {
foreach ($ancFields as $ancFieldName => $ancFieldSpec) {
if ($ancClass != "SiteTree") {
$list = $list->leftJoin($ancClass, '"'.$ancClass.'"."ID" = "SiteTree"."ID"');
}
$where[] = "\"$ancClass\".\"$ancFieldName\" LIKE '%[dms_document_link,id=$number]%'"; //."%s" LIKE ""',
$where[] = "\"$ancClass\".\"$ancFieldName\" LIKE '%[{$shortcode},id=$number]%'"; //."%s" LIKE ""',
}
}

View File

@ -56,3 +56,22 @@ DMSDocument:
Filename:
title: 'File name'
```
## Change the shortcode handler
If you need to change the `dms_document_link` shortcode handler for some reason, you can do so with YAML configuration
and some PHP:
```yaml
DMS:
shortcode_handler_key: your_shortcode
```
And for example in `_config.php`:
```php
ShortcodeParser::get('default')->register(
Config::inst()->get('DMS', 'shortcode_handler_key'),
array('DMSShortcodeHandler', 'handle')
);
```

View File

@ -29,11 +29,14 @@
});
$('form.htmleditorfield-linkform').entwine({
getShortcodeKey: function () {
return this.find(':input[name=DMSShortcodeHandlerKey]').val();
},
insertLink: function () {
var href, target = null;
var checkedValue = this.find(':input[name=LinkType]:checked').val();
if (checkedValue === 'document') {
href = '[dms_document_link,id=' + this.find('.selected-document').data('document-id') + ']';
href = '[' + this.getShortcodeKey() + ',id=' + this.find('.selected-document').data('document-id') + ']';
// Determine target
if (this.find(':input[name=TargetBlank]').is(':checked')) {
@ -88,7 +91,7 @@
}
//match a document or call the regular link handling
if (href.match(/^\[dms_document_link(\s*|%20|,)?id=([0-9]+)\]?$/i)) {
if (href.match(new RegExp("/^\[" + this.getShortcodeKey() + "(\s*|%20|,)?id=([0-9]+)\]?$/", "i"))) {
var returnArray = {
LinkType: 'document',
DocumentID: RegExp.$2,

View File

@ -122,6 +122,23 @@ class DMSDocumentSetTest extends SapphireTest
);
}
/**
* Ensure that the configurable shortcode handler key is a hidden field in the CMS
*/
public function testShortcodeHandlerKeyFieldExists()
{
Config::inst()->update('DMS', 'shortcode_handler_key', 'unit-test');
$set = DMSDocumentSet::create();
$set->write();
$fields = $set->getCMSFields();
$field = $fields->fieldByName('Root.Main.DMSShortcodeHandlerKey');
$this->assertInstanceOf('HiddenField', $field);
$this->assertSame('unit-test', $field->Value());
}
/**
* Test that extra documents are added after write
*/

View File

@ -184,6 +184,15 @@ class DMSTest extends FunctionalTest
$this->assertContainsOnlyInstancesOf('DMSDocument', $documents);
}
/**
* Ensure the shortcode handler key is configurable
*/
public function testShortcodeHandlerKeyIsConfigurable()
{
Config::inst()->update('DMS', 'shortcode_handler_key', 'testing');
$this->assertSame('testing', DMS::inst()->getShortcodeHandlerKey());
}
/**
* Test that document sets can be retrieved for a given page
*/

View File

@ -5,6 +5,8 @@ class ShortCodeRelationFinderTest extends SapphireTest
public function testFindInRate()
{
Config::inst()->update('DMS', 'shortcode_handler_key', 'dms_document_link');
$d1 = $this->objFromFixture('DMSDocument', 'd1');
$d2 = $this->objFromFixture('DMSDocument', 'd2');