From 2085566c0814b871326240a99c1d2d23a67f77ff Mon Sep 17 00:00:00 2001 From: Tony Air Date: Sat, 2 Jul 2022 18:58:48 +0200 Subject: [PATCH] FIX: Replace link field --- src/ClassReplacement/SlideImage.php | 350 ++++++++++++++++++++++++++++ 1 file changed, 350 insertions(+) create mode 100644 src/ClassReplacement/SlideImage.php diff --git a/src/ClassReplacement/SlideImage.php b/src/ClassReplacement/SlideImage.php new file mode 100644 index 0000000..4f514f9 --- /dev/null +++ b/src/ClassReplacement/SlideImage.php @@ -0,0 +1,350 @@ + 'Varchar(255)', + 'Headline' => 'Varchar(255)', + 'Description' => 'Text', + 'SortOrder' => 'Int', + 'SlideType' => 'Varchar', + ]; + + /** + * @var array + */ + private static $has_one = [ + 'Image' => Image::class, + 'Video' => Embed::class, + 'Page' => \Page::class, + 'PageLink' => SiteTree::class, + 'SlideLink' => Link::class, + ]; + + /** + * @var array + */ + private static $owns = [ + 'Image', + ]; + + /** + * @var string + */ + private static $table_name = 'SlideImage'; + + /** + * @var string + */ + private static $default_sort = 'SortOrder'; + + /** + * Adds Publish button to SlideImage record + * + * @var bool + */ + private static $versioned_gridfield_extensions = true; + + /** + * @var array + */ + private static $defaults = [ + 'SlideType' => 'Image', + ]; + + /** + * @var array + */ + private static $summary_fields = [ + 'Image.CMSThumbnail' => 'Image', + 'Name' => 'Name', + ]; + + /** + * @var array + */ + private static $searchable_fields = [ + 'Name', + 'Headline', + 'Description', + ]; + + /** + * @var int + */ + private static $image_size_limit = 512000; + + /** + * @var array + */ + private static $slide_types = [ + 'Image', + 'Video', + 'Text', + ]; + + /** + * @param bool $includerelations + * @return array + */ + public function fieldLabels($includerelations = true) + { + $labels = parent::fieldLabels($includerelations); + + $labels['Name'] = _t(__CLASS__ . '.NAME', 'Name'); + $labels['Headline'] = _t(__CLASS__ . '.HEADLINE', 'Headline'); + $labels['Description'] = _t(__CLASS__ . '.DESCRIPTION', 'Description'); + $labels['SlideLinkID'] = _t(__CLASS__ . '.PAGE_LINK', "Call to action link"); + $labels['Image'] = _t(__CLASS__ . '.IMAGE', 'Image'); + $labels['SlideType'] = _t(__CLASS__ . '.SlideType', 'Image or Video'); + $labels['Video'] = _t(__CLASS__ . '.VideoLabel', 'Video URL'); + + return $labels; + } + + /** + * @return \SilverStripe\Forms\FieldList + */ + public function getCMSFields() + { + $this->beforeUpdateCMSFields(function ($fields) { + $fields->removeByName([ + 'ShowSlide', + 'SortOrder', + 'PageID', + 'Image', + 'SlideType', + 'Video', + 'VideoID', + 'SlideLinkID', + ]); + + // Name + $fields->dataFieldByName('Name') + ->setDescription( + _t(__CLASS__ . '.INTERNAL_USE', 'for internal reference only') + ); + + // Headline + $fields->dataFieldByName('Headline') + ->setDescription( + _t(__CLASS__ . '.USED_IN_TEMPLATE', 'optional, used in template') + ); + + // Description + $fields->dataFieldByName('Description') + ->setDescription( + _t(__CLASS__ . '.USED_IN_TEMPLATE', 'optional, used in template') + ); + + // Page link + $fields->replaceField( + 'PageLinkID', + LinkField::create('SlideLink', $this->fieldLabel('SlideLinkID'), $this) + ); + + // Image + $image = UploadField::create('Image', $this->fieldLabel('Image')) + ->setFolderName('Uploads/SlideImages'); + + $image->getValidator()->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif']); + + $fields->addFieldToTab( + 'Root.Main', + CompositeField::create(FieldList::create( + DropdownField::create('SlideType', $this->fieldLabel('SlideType')) + ->setSource($this->getTypeSource()), + Wrapper::create( + $image + )->displayIf('SlideType')->isEqualTo('Image')->orIf('SlideType')->isEqualTo('Video')->end(), + Wrapper::create( + $videoField = HasOneButtonField::create( + 'Video', + 'Video', + $this + ) + ->setDescription(_t(__CLASS__ . '.VideoDescription', 'Supported links: YouTube, Vimeo')) + )->displayIf('SlideType')->isEqualTo('Video')->end() + ))->setName('MediaFields'), + 'Description' + ); + }); + + $fields = parent::getCMSFields(); + + $this->extend('updateSlideImageFields', $fields); + + return $fields; + } + + /** + * @return \SilverStripe\ORM\ValidationResult + */ + public function validate() + { + $result = parent::validate(); + + if (!$this->Name) { + $result->addError( + _t(__CLASS__ . '.NAME_REQUIRED', 'A Name is required before you can save') + ); + } + + $types = $this->getTypeSource(); + + if (isset($types['Video']) && $this->SlideType == 'Video' && !$this->VideoID) { + $result->addError( + _t(__CLASS__ . '.VIDEO_REQUIRED', 'An Video Link is required before you can save') + ); + } + + if (isset($types['Image']) && $this->SlideType == 'Image' && !$this->ImageID) { + $result->addError( + _t(__CLASS__ . '.IMAGE_REQUIRED', 'An Image is required before you can save') + ); + } + + if (isset($types['Text']) && $this->SlideType == 'Text' && !$this->Description) { + $result->addError( + _t(__CLASS__ . '.DESCRIPTION_REQUIRED', 'A Description is required before you can save') + ); + } + + return $result; + } + + /** + * @return array + */ + public function providePermissions() + { + return [ + 'Slide_EDIT' => 'Slide Edit', + 'Slide_DELETE' => 'Slide Delete', + 'Slide_CREATE' => 'Slide Create', + ]; + } + + /** + * @param null $member + * @param array $context + * + * @return bool|int + */ + public function canCreate($member = null, $context = []) + { + return Permission::check('Slide_CREATE', 'any', $member); + } + + /** + * @param null $member + * @param array $context + * + * @return bool|int + */ + public function canEdit($member = null, $context = []) + { + return Permission::check('Slide_EDIT', 'any', $member); + } + + /** + * @param null $member + * @param array $context + * + * @return bool|int + */ + public function canDelete($member = null, $context = []) + { + return Permission::check('Slide_DELETE', 'any', $member); + } + + /** + * @param null $member + * @param array $context + * + * @return bool + */ + public function canView($member = null, $context = []) + { + return true; + } + + /** + * @return array + */ + public function getTypeSource() + { + $types = $this->config()->get('slide_types'); + asort($types); + return array_combine($types, $types); + } + + /** + * @param null $template + * @param null $customFields + * @return \SilverStripe\ORM\FieldType\DBHTMLText + */ + public function renderWith($template = null, $customFields = null) + { + if ($template === null) { + $template = static::class; + $template = ($this->SlideType) ? $template . "_{$this->SlideType}" : ''; + } + + return parent::renderWith($template); + } + + /** + * @return \SilverStripe\ORM\FieldType\DBHTMLText + */ + public function forTemplate() + { + return $this->renderWith(); + } +}