Skip to content

Instantly share code, notes, and snippets.

@jelicanin
Created June 28, 2018 16:36
Show Gist options
  • Save jelicanin/20d11104a89fd9ea3a1e69b8bc91824b to your computer and use it in GitHub Desktop.
Save jelicanin/20d11104a89fd9ea3a1e69b8bc91824b to your computer and use it in GitHub Desktop.
Implementation example for "fractaslabs/silverstripe-elemental-stylings" module
<?php
namespace Fractas\ElementalBlocks\Elements;
use DNADesign\Elemental\Models\BaseElement;
use Fractas\ElementalBlocks\Extensions\StylingHeight;
use Fractas\ElementalBlocks\Extensions\StylingHorizontalAlign;
use Fractas\ElementalBlocks\Extensions\StylingStyle;
use Fractas\ElementalBlocks\Extensions\StylingTextAlign;
use Fractas\ElementalBlocks\Extensions\StylingVerticalAlign;
use Fractas\ElementalBlocks\Extensions\StylingWidth;
use SilverStripe\Assets\Image;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Convert;
use SilverStripe\ElementalBlocks\Form\BlockLinkField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
class ElementContentImage extends BaseElement
{
private static $singular_name = 'Text with Image';
private static $plural_name = 'Text with Image';
private static $description = 'Provides an content and image fields';
private static $table_name = 'ElementContentImage';
private static $db = [
'Content' => 'HTMLText',
'CallToActionLink' => 'Link',
];
private static $has_one = [
'Image' => Image::class,
];
private static $owns = ['Image'];
private static $extensions = [
StylingHorizontalAlign::class,
StylingVerticalAlign::class,
StylingTextAlign::class,
StylingStyle::class,
StylingHeight::class,
StylingWidth::class,
];
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
// Move the file upload field to be before the content
$upload = $fields->fieldByName('Root.Main.Image');
$fields->insertBefore('Content', $upload);
$ctaLink = BlockLinkField::create('CallToActionLink', _t(__CLASS__.'.CallToActionLink', 'Call-To-Action Link'));
$fields->insertBefore($ctaLink, 'Content');
});
// Ensure TinyMCE's javascript is loaded before the blocks overrides
Requirements::javascript(TinyMCEConfig::get('cms')->getScriptURL());
Requirements::javascript('silverstripe/elemental-blocks:client/dist/js/bundle.js');
Requirements::css('silverstripe/elemental-blocks:client/dist/styles/bundle.css');
return parent::getCMSFields();
}
public function getType()
{
return _t(__CLASS__.'.BlockType', self::$singular_name);
}
public function getSummary()
{
$styling = ArrayList::create();
$styling->push($this->getStylingStyleData());
$styling->push($this->getStylingTextAlignData());
$styling->push($this->getStylingVerticalAlignData());
$styling->push($this->getStylingHorizontalAlignData());
$styling->push($this->getStylingHeightData());
$styling->push($this->getStylingWidthData());
$data = ArrayData::create([
'Description' => $this->dbObject('Content')->LimitCharactersToClosestWord(160),
'Styling' => $styling,
]);
return $data->renderWith('SummaryStylingInfo');
}
/**
* For the CMS frontend, return a parsed set of data for use in templates.
*
* @return null|ArrayData
*/
public function CallToActionLink()
{
return $this->decodeLinkData($this->getField('CallToActionLink'));
}
/**
* Given a set of JSON data, decode it, attach the relevant Page object and return as ArrayData.
*
* @param string $linkJson
*
* @return null|ArrayData
*/
protected function decodeLinkData($linkJson)
{
if (!$linkJson || 'null' === $linkJson) {
return;
}
$data = ArrayData::create(Convert::json2obj($linkJson));
// Link page, if selected
if ($data->PageID) {
$data->setField('Page', self::get_by_id(SiteTree::class, $data->PageID));
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment