Skip to content

Instantly share code, notes, and snippets.

@wernerkrauss
Created June 4, 2015 14:15
Show Gist options
  • Save wernerkrauss/a71896b1394d7044c788 to your computer and use it in GitHub Desktop.
Save wernerkrauss/a71896b1394d7044c788 to your computer and use it in GitHub Desktop.
Silverstripe GalleryPic Dataobject with Shortcodable interface
<?php
/**
* Class GalleryPic
* @todo: sync Copyright if Image has a db field for it
*/
class GalleryPic extends DataObject implements Shortcodable
{
private static $db = array(
'Title' => 'Text',
'Description' => 'Text',
'Copyright' => 'Text',
'SortOrder' => 'Int'
);
private static $has_one = array(
'Attachment' => 'Image',
'ResourcePage' => 'Page'
);
private static $default_sort = 'SortOrder';
private static $singular_name = 'Galeriebild';
private static $plural_name = 'Galeriebilder';
public function getCMSFields()
{
$fields = new FieldList(
TextField::create('Title', 'Titel'),
TextareaField::create('Description', 'Beschreibung'),
TextField::create('Copyright', 'Copyright / Quelle'),
$imageField = UploadField::create('Attachment')
);
$imageField->setAllowedFileCategories('image');
$imageField->setAllowedMaxFileNumber(1);
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
* @inheritdoc
* @return Fieldlist|void
*/
public static function shortcode_attribute_fields()
{
return FieldList::create(
TextField::create('Text','Text')
);
}
/**
* Parse the shortcode and render as a string
* @param array $arguments 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($arguments, $content, $parser, $shortcode){
if (isset($arguments['Text']) &&
isset($arguments['id']) &&
$img = GalleryPic::get()->byID($arguments['id'])) {
return '<span data-rel-imgid="' . $img->ID . '">' . $arguments['Text'] . '</span>';
}
}
/**
* returns the records for shortcode form
* @return array
*/
public static function get_shortcodable_records() {
$currentPage = $_SESSION['CMSMain']['currentPage'];
return GalleryPic::get()
->filter(array('ResourcePageID' => $currentPage))
->map()
->toArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment