Skip to content

Commit

Permalink
REFACTOR LinkListObject (#9)
Browse files Browse the repository at this point in the history
Created an intermediary dataobject for the link. This allows us to set fields like Content and SortOrder on the dataobject, but reference the field values of the Link.
  • Loading branch information
jsirish authored Feb 5, 2024
1 parent 0b09c63 commit 786da9e
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 60 deletions.
3 changes: 0 additions & 3 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
---
name: silverstripe-elemental-links-config
---
gorriecoe\Link\Models\Link:
extensions:
- Dynamic\Elements\Links\Extensions\LinkDataExtension
12 changes: 5 additions & 7 deletions src/Elements/LinksElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
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;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
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
{
Expand Down Expand Up @@ -53,7 +52,7 @@ class LinksElement extends BaseElement
* @var array
*/
private static $has_many = [
'ElementLinks' => Link::class,
'ElementLinks' => LinkListObject::class,
];

/**
Expand All @@ -72,8 +71,7 @@ public function getCMSFields()

$links->getConfig()
->addComponents([
new GridFieldOrderableRows('ElementLinksSort'),
new GridFieldAddExistingSearchButton(),
new GridFieldOrderableRows('SortOrder'),
])
->removeComponentsByType([
GridFieldAddExistingAutocompleter::class,
Expand Down
45 changes: 0 additions & 45 deletions src/Extensions/LinkDataExtension.php

This file was deleted.

116 changes: 116 additions & 0 deletions src/Model/LinkListObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Dynamic\Elements\Links\Model;

use gorriecoe\Link\Models\Link;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use gorriecoe\LinkField\LinkField;
use Dynamic\Elements\Links\Elements\LinksElement;

/**
* Class \Dynamic\Elements\Links\Model\LinkListObject
*
* @property string $Content
* @property int $SortOrder
* @property int $LinkListID
* @property int $LinkID
* @method LinksElement LinkList()
* @method Link Link()
*/
class LinkListObject extends DataObject
{
/**
* @var string
*/
private static $singular_name = 'Link';

/**
* @var string
*/
private static $plural_name = 'Links';

/**
* @var array
*/
private static $db = [
'Content' => '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;
}
}
22 changes: 17 additions & 5 deletions templates/Dynamic/Elements/Links/Elements/LinksElement.ss
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@

<% if $ElementLinks %>
<div class="row element__links__list">
<ul class="list-group">
<% loop $ElementLinks %>
<li class="list-group-item">$Me</li>
<% end_loop %>
</ul>
<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}>
<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>
</div>
$Content
<% if $Link.Type == 'URL' %><small>$Link.LinkURL</small><% end_if %>
</a>
<% end_loop %>
</ul>
</div>
</div>
<% end_if %>

0 comments on commit 786da9e

Please sign in to comment.