Skip to content

Commit

Permalink
REFACTOR migrate Silverstripe Link to has_one
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirish committed Feb 14, 2024
1 parent 4970c09 commit 0902b4a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
42 changes: 37 additions & 5 deletions src/Element/ElementCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Dynamic\Elements\Card\Elements;

use SilverStripe\Forms\HTMLEditor\HtmlEditorField;
use SilverStripe\Assets\Image;
use SilverStripe\Forms\FieldList;
use SilverStripe\LinkField\ORM\DBLink;
use SilverStripe\ORM\FieldType\DBField;
use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Models\Link;

/**
* Class \LakeshoreProductions\Element\Card
Expand All @@ -20,68 +20,100 @@
* @property string $CardLink
* @property int $CardImageID
* @method Image CardImage()
* @mixin AOSExtension
*/
class ElementCard extends BaseElement
{
/**
* @var string
* @config
*/
private static string $table_name = 'ElementCard';

/**
* @var string
* @config
*/
private static string $singular_name = 'Card';

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

/**
* @var string
* @config
*/
private static string $description = 'A card element';

/**
* @var string
* @config
*/
private static string $icon = 'font-icon-block-content';

/**
* @var array|string[]
* @config
*/
private static array $db = [
'Content' => 'HTMLText',
'ElementLink' => DBLink::class,
];

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

/**
* @var array|string[]
* @config
*/
private static array $owns = [
'Image',
];

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

$labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Title');
$labels['Content'] = _t(__CLASS__ . '.ContentLabel', 'Description');
$labels['ElementLink'] = _t(__CLASS__ . '.ElementLinkLabel', 'Link');

return $labels;
}

/**
* @return FieldList
*/
public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
// @phpstan-ignore-next-line
$fields->dataFieldByName('Image')
->setFolderName('Uploads/Elements/Card');

$fields->insertBefore('Content', $fields->dataFieldByName('Image'));
$fields->insertBefore('Content', $fields->dataFieldByName('ElementLink'));

// @phpstan-ignore-next-line
$fields->dataFieldByName('Content')
->setRows(5);

$fields->replaceField(
'ElementLinkID',
LinkField::create('ElementLink')
->setTitle($this->fieldLabel('ElementLink'))
);
});

return parent::getCMSFields();
Expand Down
2 changes: 1 addition & 1 deletion templates/Dynamic/Elements/Card/Elements/ElementCard.ss
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="card-body">
<% if $Title && $ShowTitle %><$TitleTag class="element__title $TitleSizeClass">$Title</$TitleTag><% end_if %>
<% if $Content %><div class="card-text">$Content</div><% end_if %>
<% if $ElementLink %><p>$ElementLink</p><% end_if %>
<% if $ElementLink %><p><a href="$ElementLink.URL" class="btn btn-primary" title="$ElementLink.Title"<% if $ElementLink.OpenInNew %> target="_blank" rel="noopener noreferrer"<% end_if %>>$ElementLink.Title</a></p><% end_if %>
</div>
</div>
</div>

0 comments on commit 0902b4a

Please sign in to comment.