Skip to content

Commit

Permalink
REFACTOR ImageSlide has_one ElementLink (#25)
Browse files Browse the repository at this point in the history
remove $db DBLink config
  • Loading branch information
jsirish authored Aug 12, 2024
1 parent ea3c8bf commit b5cd0c2
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/Model/ImageSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use SilverStripe\Assets\Image;
use SilverStripe\Forms\FieldList;
use SilverStripe\LinkField\ORM\DBLink;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Models\Link;

/**
* Class \Dynamic\Carousel\Model\ImageSlide
Expand Down Expand Up @@ -34,20 +34,13 @@ class ImageSlide extends Slide
*/
private static $plural_name = 'Image Slides';

/**
* @var array
* @config
*/
private static $db = [
'DbLink' => DBLink::class
];

/**
* @var array
* @config
*/
private static $has_one = [
'Image' => Image::class,
'ElementLink' => Link::class,
];

/**
Expand All @@ -64,20 +57,38 @@ class ImageSlide extends Slide
*/
private static $hide_ancestor = Slide::class;

/**
* @param bool $includerelations
* @return array
*/
public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels($includerelations);

$labels['ElementLink'] = _t(__CLASS__ . '.ElementLinkLabel', 'Link');

return $labels;
}

/**
* @return FieldList
*/
public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->replaceField(
'ElementLinkID',
$link = LinkField::create('ElementLink')
->setTitle($this->fieldLabel('ElementLink'))
);

$fields->addFieldsToTab(
'Root.Main',
[
// @phpstan-ignore-next-line
$fields->dataFieldByName('Image')
->setFolderName('Uploads/Carousel/Slides'),
LinkField::create('DbLink')
->setTitle('Link'),
$link
],
'Content'
);
Expand Down

0 comments on commit b5cd0c2

Please sign in to comment.