Skip to content

Commit

Permalink
REFACTOR move to Silverstripe Link (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirish authored Feb 8, 2024
1 parent 786da9e commit 9832441
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 8 additions & 2 deletions src/Elements/LinksElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -23,33 +22,39 @@ class LinksElement extends BaseElement
{
/**
* @var string
* @config
*/
private static $table_name = 'LinksElement';

/**
* @var array
* @config
*/
private static $db = [
'Content' => 'HTMLText',
];

/**
* @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,
Expand All @@ -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);

Expand All @@ -87,7 +93,7 @@ public function getCMSFields()
}

/**
* @return DBHTMLText
* @return string
*/
public function getSummary()
{
Expand Down
53 changes: 24 additions & 29 deletions src/Model/LinkListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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';

Expand All @@ -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;
}
Expand All @@ -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;
}
}
23 changes: 15 additions & 8 deletions templates/Dynamic/Elements/Links/Elements/LinksElement.ss
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
<div class="row element__links__list">
<div class="col-md-12">
<ul class="list-group">
<% loop $ElementLinks %>
<a{$Link.IDAttr} href="$Link.LinkURL" class="list-group-item list-group-item-action" title="$Link.Title"{$Link.TargetAttr}>
<% loop $ElementLinks.Sort('SortOrder') %>
<a href="$Link.URL" class="list-group-item list-group-item-action" title="$Title"<% if $Link.OpenInNew %> target="_blank" rel="noopener noreferrer"<% end_if %>>
<div class="d-flex w-100 justify-content-between">
<h4 class="mb-1">
<i class="bi bi-link-45deg"></i>
$Link.Title
</h4>
<small>updated $LastEdited.Ago</small>
<h4 class="mb-1">$Title</h4>
</div>
$Content
<% if $Link.Type == 'URL' %><small>$Link.LinkURL</small><% end_if %>
<% if $Link %>
<div class="d-flex gap-3">
<% with $Link %>
<i class="bi bi-link-45deg"></i>
<div class="d-flex gap-2 w-100">
<small>
<% if $Title %>$Title<br><% end_if %>{$URL}
</small>
</div>
<% end_with %>
</div>
<% end_if %>
</a>
<% end_loop %>
</ul>
Expand Down

0 comments on commit 9832441

Please sign in to comment.