Last active
January 18, 2021 13:10
-
-
Save sheadawson/12c5e5a2b42272bd90f703941450d677 to your computer and use it in GitHub Desktop.
An example of a Shortcodable DataObject. See https://github.com/sheadawson/silverstripe-shortcodable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shortcodable: | |
# essential - classes that your want to be shortcodable | |
shortcodable_classes: | |
- ImageGallery | |
# optional - additional htmleditorfields | |
htmleditor_names: | |
- cwp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ImageGallery | |
* | |
* An example of a Shortcodable DataObject. | |
* See https://github.com/sheadawson/silverstripe-shortcodable | |
**/ | |
class ImageGallery extends DataObject | |
{ | |
private static $db = array( | |
'Title' => 'Varchar' | |
); | |
/** | |
* Parse the shortcode and render as a string, probably with a template | |
* @param array $attributes the list of attributes of the shortcode | |
* @param string $content the shortcode content | |
* @param ShortcodeParser $parser the ShortcodeParser instance | |
* @param string $shortcode the raw shortcode being parsed | |
* @return String | |
**/ | |
public static function parse_shortcode($attributes, $content, $parser, $shortcode) | |
{ | |
// check the gallery exists | |
if (isset($attributes['id']) && $gallery = ImageGallery::get()->byID($attributes['id'])) { | |
// collect custom attributes from the shortcode popup form | |
$data = array(); | |
if (isset($attributes['Style'])) { | |
$data['Style'] = $attributes['Style']; | |
} | |
// render with template | |
return $gallery->customise($data)->renderWith('ImageGallery'); | |
} | |
} | |
/** | |
* Returns a list of fields for editing the shortcode's attributes | |
* in the insert shortcode popup window | |
* | |
* @return Fieldlist | |
**/ | |
public function getShortcodeFields() | |
{ | |
return FieldList::create( | |
DropdownField::create( | |
'Style', | |
'Gallery Style', | |
array('Carousel' => 'Carousel', 'Lightbox' => 'Lightbox') | |
) | |
); | |
} | |
/** | |
* Returns a link to an image to be displayed as a placeholder in the editor | |
* In this example we make easy work of this task by using the placehold.it service | |
* But you could also return a link to an image in the filesystem - perharps the first | |
* image in this ImageGallery | |
* a placeholder | |
* | |
* @param array $attributes the list of attributes of the shortcode | |
* @return String | |
**/ | |
public function getShortcodePlaceHolder($attributes) | |
{ | |
$text = $this->Title; | |
if (isset($attributes['Style'])) { | |
$text .= ' (' . $attributes['Style'] . ')'; | |
} | |
$params = array( | |
'txt' => $text, | |
'w' => '400', | |
'h' => '200', | |
'txtsize' => '27', | |
'bg' => '000000', | |
'txtclr' => 'cccccc' | |
); | |
return 'https://placeholdit.imgix.net/~text?' . http_build_query($params); | |
} | |
/** | |
* 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. | |
* | |
* @return array | |
*/ | |
public function getShortcodableRecords() { | |
return ImageGallery::get()->filter('SomeField', 'SomeValue')->map()->toArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://placeholdit.imgix.net does not work any longer. Developer should find alternative.