From 983244156e948b6ceb94b2beb5eecf19948a4930 Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Thu, 8 Feb 2024 09:16:50 -0600 Subject: [PATCH] REFACTOR move to Silverstripe Link (#10) --- composer.json | 2 +- src/Elements/LinksElement.php | 10 +++- src/Model/LinkListObject.php | 53 +++++++++---------- .../Elements/Links/Elements/LinksElement.ss | 23 +++++--- 4 files changed, 48 insertions(+), 40 deletions(-) diff --git a/composer.json b/composer.json index 50c5de7..e5af24a 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,8 @@ ], "require": { "dnadesign/silverstripe-elemental": "^5", - "gorriecoe/silverstripe-linkfield": "^1.1", "silverstripe/framework": "^5", + "silverstripe/linkfield":"^3.0", "symbiote/silverstripe-gridfieldextensions": "^4" }, "require-dev": { diff --git a/src/Elements/LinksElement.php b/src/Elements/LinksElement.php index 47543a8..abedc53 100644 --- a/src/Elements/LinksElement.php +++ b/src/Elements/LinksElement.php @@ -10,7 +10,6 @@ use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; use SilverStripe\Forms\GridField\GridFieldDeleteAction; use SilverStripe\ORM\FieldType\DBField; -use SilverStripe\ORM\FieldType\DBHTMLText; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; /** @@ -23,11 +22,13 @@ class LinksElement extends BaseElement { /** * @var string + * @config */ private static $table_name = 'LinksElement'; /** * @var array + * @config */ private static $db = [ 'Content' => 'HTMLText', @@ -35,21 +36,25 @@ class LinksElement extends BaseElement /** * @var string + * @config */ private static $singular_name = 'Links Element'; /** * @var string + * @config */ private static $plural_name = 'Links Elements'; /** * @var bool + * @config */ private static $inline_editable = false; /** * @var array + * @config */ private static $has_many = [ 'ElementLinks' => LinkListObject::class, @@ -61,6 +66,7 @@ class LinksElement extends BaseElement public function getCMSFields() { $this->beforeUpdateCMSFields(function (FieldList $fields) { + // @phpstan-ignore-next-line $fields->dataFieldByName('Content') ->setRows(8); @@ -87,7 +93,7 @@ public function getCMSFields() } /** - * @return DBHTMLText + * @return string */ public function getSummary() { diff --git a/src/Model/LinkListObject.php b/src/Model/LinkListObject.php index 5200fa9..dd6cce7 100644 --- a/src/Model/LinkListObject.php +++ b/src/Model/LinkListObject.php @@ -2,15 +2,16 @@ namespace Dynamic\Elements\Links\Model; -use gorriecoe\Link\Models\Link; use SilverStripe\ORM\DataObject; use SilverStripe\Forms\FieldList; -use gorriecoe\LinkField\LinkField; +use SilverStripe\LinkField\Form\LinkField; use Dynamic\Elements\Links\Elements\LinksElement; +use SilverStripe\LinkField\Models\Link; /** * Class \Dynamic\Elements\Links\Model\LinkListObject * + * @property string $Title * @property string $Content * @property int $SortOrder * @property int $LinkListID @@ -22,24 +23,29 @@ class LinkListObject extends DataObject { /** * @var string + * @config */ private static $singular_name = 'Link'; /** * @var string + * @config */ private static $plural_name = 'Links'; /** * @var array + * @config */ private static $db = [ + 'Title' => 'Varchar(255)', 'Content' => 'HTMLText', 'SortOrder' => "Int", ]; /** * @var array + * @config */ private static $has_one = [ 'LinkList' => LinksElement::class, @@ -48,14 +54,16 @@ class LinkListObject extends DataObject /** * @var array + * @config */ private static $summary_fields = [ + 'Title', 'Link.Title', - 'Link.LinkURL', ]; /** * @var string + * @config */ private static $table_name = 'LinkListObject'; @@ -67,10 +75,10 @@ public function fieldLabels($includerelations = true) { $labels = parent::fieldLabels($includerelations); - $labels['Description'] = _t(__CLASS__ . '.DescriptionLabel', 'Description'); - $labels['Link.Title'] = _t(__CLASS__ . '.LinkTitleLabel', 'Link'); - $labels['Link.LinkURL'] = _t(__CLASS__ . '.LinkURLLabel', 'Link URL'); + $labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Title'); + $labels['Content'] = _t(__CLASS__ . '.ContentLabel', 'Description'); $labels['Link'] = _t(__CLASS__ . '.LinkLabel', 'Link'); + $labels['Link.Title'] = _t(__CLASS__ . '.LinkTitleLabel', 'Link'); return $labels; } @@ -81,36 +89,23 @@ public function fieldLabels($includerelations = true) public function getCMSFields() { $this->beforeUpdateCMSFields(function (FieldList $fields) { - $fields->replaceField( - 'LinkID', - LinkField::create('Link', $this->fieldLabel('Link'), $this) - ); - - $fields->insertBefore( - 'Content', - $fields->dataFieldByName('Link') - ); - - $fields->dataFieldByName('Content') - ->setTitle($this->fieldLabel('Description')) - ->setRows(5); $fields->removeByName([ 'LinkListID', 'SortOrder', ]); + + // @phpstan-ignore-next-line + $fields->dataFieldByName('Content') + ->setRows(5); + + $fields->replaceField( + 'LinkID', + LinkField::create('Link') + ->setTitle($this->fieldLabel('Link')) + ); }); return parent::getCMSFields(); } - - /** - * return Title of Link for LinkListObject Title - * - * @return void - */ - public function getTitle() - { - return $this->Link()->Title; - } } diff --git a/templates/Dynamic/Elements/Links/Elements/LinksElement.ss b/templates/Dynamic/Elements/Links/Elements/LinksElement.ss index b42935f..5260ec1 100644 --- a/templates/Dynamic/Elements/Links/Elements/LinksElement.ss +++ b/templates/Dynamic/Elements/Links/Elements/LinksElement.ss @@ -5,17 +5,24 @@