Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable customised name and keyword for shortcodes instead of class name #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ class ImageGallery extends DataObject

Create the ImageGallery.ss template then that's it, done!

### Custom shortcode names

You can specify a name that will appear in the modal dropdown when inserting shortcode through TinyMCE.
Following code would e.g. change the name from *CustomSectionsContentBlock* to *Content block with custom sections*.

```php
public function getShortcodeNiceName()
{
return 'Content block with custom sections';
}
```

You can as well change the actual shortcode (keyword) that will appear in the wysiwyg editor.
Stick this function into your shortcodable dataobject and the output will look like *[Sections_ContentBlock id=""]*

```php
public function getShortcodeKeyword()
{
return 'Sections_ContentBlock';
}
```

**TODO**: Enable changing this via config YAML files.

## Restricting data object selection

If you would like to customise or filter the list of available shortcodable DataObject records available in the dropdown, you can supply a custom getShortcodableRecords method on your shortcodable DataObject. The method should return an associative array suitable for the DropdownField. For example:
Expand Down
8 changes: 6 additions & 2 deletions code/Shortcodable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ public static function register_class($class)
if (!singleton($class)->hasMethod('parse_shortcode')) {
user_error("Failed to register \"$class\" with shortcodable. $class must have the method parse_shortcode(). See /shortcodable/README.md", E_USER_ERROR);
}
ShortcodeParser::get('default')->register($class, array($class, 'parse_shortcode'));
singleton('ShortcodableParser')->register($class);
$kw = $class;
if (singleton($class)->hasMethod('getShortcodeKeyword')) {
$kw = singleton($class)->getShortcodeKeyword();
}
ShortcodeParser::get('default')->register($kw, array($class, 'parse_shortcode'));
singleton('ShortcodableParser')->register($kw);
}
}

Expand Down
13 changes: 9 additions & 4 deletions code/controllers/ShortcodableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function ShortcodeForm()
$classList = ShortCodable::get_shortcodable_classes();
$classes = array();
foreach ($classList as $class) {
$classes[$class] = singleton($class)->hasMethod('singular_name') ? singleton($class)->singular_name() : $class;
$classes[$class] = singleton($class)->hasMethod('getShortcodeNiceName') ? singleton($class)->getShortcodeNiceName() : (singleton($class)->hasMethod('singular_name') ? singleton($class)->singular_name() : $class);
}

// load from the currently selected ShortcodeType or Shortcode data
Expand Down Expand Up @@ -62,8 +62,8 @@ public function ShortcodeForm()
)
)->addExtraClass('CompositeField composite cms-content-header nolabel'),
LiteralField::create('shortcodablefields', '<div class="ss-shortcodable content">'),
DropdownField::create('ShortcodeType', 'ShortcodeType', $classes, $classname)
->setHasEmptyDefault(true)
DropdownField::create('ShortcodeType', 'Shortcode type', $classes, $classname)
->setHasEmptyDefault(true)->setEmptyString('--- select one ---')
->addExtraClass('shortcode-type'),

));
Expand All @@ -80,14 +80,19 @@ public function ShortcodeForm()
}
$fields->push(
DropdownField::create('id', $class->singular_name(), $dataObjectSource)
->setHasEmptyDefault(true)
->setHasEmptyDefault(true)->setEmptyString('--- select one ---')
);
}
if (singleton($classname)->hasMethod('getShortcodeFields')) {
if ($attrFields = singleton($classname)->getShortcodeFields()) {
$fields->push(CompositeField::create($attrFields)->addExtraClass('attributes-composite'));
}
}
$kw = $classname;
if (singleton($classname)->hasMethod('getShortcodeKeyword')) {
$kw = singleton($classname)->getShortcodeKeyword();
}
$fields->push(HiddenField::create('ShortcodeKeyword', '', $kw));
}
}

Expand Down
6 changes: 4 additions & 2 deletions javascript/shortcodable.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
// get the html to insert
getHTML: function(){
var data = this.getAttributes();
var html = data.shortcodeType;
var html = data.shortcodeKeyword;

for (var key in data.attributes) {
html += ' ' + key + '="' + data.attributes[key] + '"';
Expand All @@ -71,6 +71,7 @@
// get shortcode attributes from shortcode form
getAttributes: function() {
var attributes = {};
var shortcodeKeyword = this.find(':input[name=ShortcodeKeyword]').val();
var shortcodeType = this.find(':input[name=ShortcodeType]').val();
var id = this.find(':input[name=id]').val();
if (id) {
Expand All @@ -91,7 +92,8 @@
}

return {
'shortcodeType' : this.find(':input[name=ShortcodeType]').val(),
'shortcodeType' : shortcodeType,
'shortcodeKeyword' : shortcodeKeyword,
'attributes' : attributes
};
},
Expand Down