diff --git a/_config/config.yml b/_config/config.yml index 5f1677a..7b3db94 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -1,6 +1,3 @@ --- name: silverstripe-elemental-links-config --- -gorriecoe\Link\Models\Link: - extensions: - - Dynamic\Elements\Links\Extensions\LinkDataExtension diff --git a/src/Elements/LinksElement.php b/src/Elements/LinksElement.php index 1efdcd5..47543a8 100644 --- a/src/Elements/LinksElement.php +++ b/src/Elements/LinksElement.php @@ -3,6 +3,7 @@ namespace Dynamic\Elements\Links\Elements; use DNADesign\Elemental\Models\BaseElement; +use Dynamic\Elements\Links\Model\LinkListObject; use gorriecoe\Link\Models\Link; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridField; @@ -10,15 +11,13 @@ use SilverStripe\Forms\GridField\GridFieldDeleteAction; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBHTMLText; -use SilverStripe\ORM\HasManyList; -use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; /** * Class LinksElement - * @package Dynamic\Elements\Links\Elements * - * @method HasManyList ElementLinks() + * @property string $Content + * @method DataList|LinkListObject[] ElementLinks() */ class LinksElement extends BaseElement { @@ -53,7 +52,7 @@ class LinksElement extends BaseElement * @var array */ private static $has_many = [ - 'ElementLinks' => Link::class, + 'ElementLinks' => LinkListObject::class, ]; /** @@ -72,8 +71,7 @@ public function getCMSFields() $links->getConfig() ->addComponents([ - new GridFieldOrderableRows('ElementLinksSort'), - new GridFieldAddExistingSearchButton(), + new GridFieldOrderableRows('SortOrder'), ]) ->removeComponentsByType([ GridFieldAddExistingAutocompleter::class, diff --git a/src/Extensions/LinkDataExtension.php b/src/Extensions/LinkDataExtension.php deleted file mode 100644 index 0b61f31..0000000 --- a/src/Extensions/LinkDataExtension.php +++ /dev/null @@ -1,45 +0,0 @@ - 'Int', - ]; - - /** - * @var array - */ - private static $has_one = [ - 'LinksElement' => LinksElement::class, - ]; - - /** - * @param FieldList $fields - */ - public function updateCMSFields(FieldList $fields) - { - if (($linksElement = $this->owner->LinksElementID) && $linksElement > 0) { - $fields->replaceField( - 'LinksElementID', - HiddenField::create('LinksElementID') - ->setValue($linksElement) - ); - } else { - $fields->removeByName('LinksElementID'); - } - } -} diff --git a/src/Model/LinkListObject.php b/src/Model/LinkListObject.php new file mode 100644 index 0000000..5200fa9 --- /dev/null +++ b/src/Model/LinkListObject.php @@ -0,0 +1,116 @@ + 'HTMLText', + 'SortOrder' => "Int", + ]; + + /** + * @var array + */ + private static $has_one = [ + 'LinkList' => LinksElement::class, + 'Link' => Link::class, + ]; + + /** + * @var array + */ + private static $summary_fields = [ + 'Link.Title', + 'Link.LinkURL', + ]; + + /** + * @var string + */ + private static $table_name = 'LinkListObject'; + + /** + * @param bool $includerelations + * @return array + */ + 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['Link'] = _t(__CLASS__ . '.LinkLabel', 'Link'); + + return $labels; + } + + /** + * @return \SilverStripe\Forms\FieldList + */ + 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', + ]); + }); + + 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 79be0a3..b42935f 100644 --- a/templates/Dynamic/Elements/Links/Elements/LinksElement.ss +++ b/templates/Dynamic/Elements/Links/Elements/LinksElement.ss @@ -3,10 +3,22 @@ <% if $ElementLinks %> <% end_if %>