Skip to content

Commit

Permalink
UPDATE utilize Silverstripe Link for utility navigation links (#153)
Browse files Browse the repository at this point in the history
* UPDATE utilize Silverstripe Link for utility navigation links

* UPDATE NavigationGroup and Social Links to use new Link class

* UPDATE require silverstripe/linkfield: ^4.0
  • Loading branch information
muskie9 authored Jul 23, 2024
1 parent 5925336 commit 4a247ae
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 155 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dynamic/silverstripe-site-tools": "^3",
"jonom/silverstripe-betternavigator": "^6",
"jonom/silverstripe-text-target-length": "^2",
"silverstripe/linkfield": "^4.0",
"silverstripe/recipe-cms": "^5",
"silverstripe/sharedraftcontent": "^3",
"tractorcow/silverstripe-robots": "^4",
Expand Down
102 changes: 24 additions & 78 deletions src/Extension/TemplateDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,17 @@
use Dynamic\Base\Model\NavigationColumn;
use Dynamic\Base\Model\SocialLink;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\ToggleCompositeField;
use SilverStripe\LinkField\Form\MultiLinkField;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Versioned\GridFieldArchiveAction;
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;

/**
Expand All @@ -44,62 +36,49 @@ class TemplateDataExtension extends DataExtension
/**
* @var array
*/
private static $db = [
private static array $db = [
'TitleLogo' => "Enum(array('Logo', 'Title'))",
];

/**
* @var array
*/
private static $has_one = [
private static array $has_one = [
'Logo' => Image::class,
'LogoRetina' => Image::class,
];

/**
* @var array
*/
private static $owns = [
'Logo',
'LogoRetina',
];

/**
* @var array
*/
private static $has_many = [
private static array $has_many = [
'NavigationColumns' => NavigationColumn::class,
'SocialLinks' => SocialLink::class,
'SocialLinks' => SocialLink::class . '.Owner',
'UtilityLinks' => Link::class . '.Owner',
];

/**
* @var array
*/
private static $many_many = [
'UtilityLinks' => SiteTree::class,
];

/**
* @var array
*/
private static $many_many_extraFields = [
'UtilityLinks' => [
'SortOrder' => 'Int',
],
private static array $owns = [
'Logo',
'LogoRetina',
'UtilityLinks',
'Sociallinks',
];


/**
* @var array
*/
private static $defaults = [
private static array $defaults = [
'TitleLogo' => 'Title',
];

/**
* @param FieldList $fields
* @return void
*/
public function updateCMSFields(FieldList $fields)
public function updateCMSFields(FieldList $fields): void
{
// options for logo or title display
$logoOptions = [
Expand All @@ -108,43 +87,18 @@ public function updateCMSFields(FieldList $fields)
];

$fields->addFieldsToTab('Root.Main', [
//HeaderField::create('BrandingHD', 'Branding', 3),
//LiteralField::create('HeaderDescrip', '<p>Adjust the settings of your template header.</p>'),
$titlelogo = OptionsetField::create('TitleLogo', 'Branding', $logoOptions),
//$title = TextField::create("Title", _t(SiteConfig::class . '.SITETITLE', "Site title")),
//$tagline = TextField::create("Tagline", _t(SiteConfig::class . '.SITETAGLINE', "Site Tagline/Slogan")),
// normal logos
$logo = UploadField::create('Logo', 'Logo'),
$retinaLogo = UploadField::create('LogoRetina', 'Retina Logo'),
]);

//$title->hideUnless($titlelogo->getName())->isEqualTo('Title');
//$tagline->hideUnless($titlelogo->getName())->isEqualTo('Title');

$logo->hideUnless($titlelogo->getName())->isEqualTo('Logo');
$retinaLogo->hideUnless($titlelogo->getName())->isEqualTo('Logo');

if ($this->owner->ID) {
// utility navigation
$config = GridFieldConfig_RelationEditor::create()
->removeComponentsByType([
GridFieldAddNewButton::class,
GridFieldAddExistingAutocompleter::class,
GridFieldEditButton::class,
GridFieldArchiveAction::class,
])->addComponents(
new GridFieldOrderableRows('SortOrder'),
new GridFieldAddExistingSearchButton()
);
$linksField = GridField::create(
'UtilityLinks',
'Utility Navigation',
$this->owner->UtilityLinks()->sort('SortOrder'),
$config
);

if ($this->getOwner()->ID) {
$fields->addFieldsToTab('Root.Links.Utility', [
$linksField
MultiLinkField::create('UtilityLinks')
->setTitle('Utility Links')
->setDescription('Add links to the utility navigation area of your template'),
]);

Expand All @@ -159,7 +113,7 @@ public function updateCMSFields(FieldList $fields)
$footerLinks = GridField::create(
'NavigationColumns',
'Footer Navigation',
$this->owner->NavigationColumns()->sort('SortOrder'),
$this->getOwner()->NavigationColumns()->sort('SortOrder'),
$config
);

Expand All @@ -170,20 +124,12 @@ public function updateCMSFields(FieldList $fields)
]);
}

// social links
$config = GridFieldConfig_RecordEditor::create();
$config->addComponent(new GridFieldOrderableRows('SortOrder'));

$socialLinks = GridField::create(
'SocialLinks',
'Social Properties',
$this->owner->SocialLinks()->sort('SortOrder'),
$config
);

$fields->addFieldsToTab('Root.Links.Social', [
$socialLinks
->setDescription('Add links to your social media properties'),
MultiLinkField::create('SocialLinks')
->setDescription('Add links to your social media properties')
->setAllowedTypes([
SocialLink::class,
]),
]);
}
}
61 changes: 19 additions & 42 deletions src/Model/NavigationGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace Dynamic\Base\Model;

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\LinkField\Form\MultiLinkField;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\GridFieldArchiveAction;
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
Expand Down Expand Up @@ -40,33 +39,31 @@ class NavigationGroup extends DataObject
/**
* @var array
*/
private static $db = array(
private static $db = [
'Title' => 'Varchar(255)',
'SortOrder' => 'Int',
);
];

/**
* @var array
*/
private static $has_one = array(
private static $has_one = [
'NavigationColumn' => NavigationColumn::class,
);
];

/**
* @var array
*/
private static $many_many = array(
'NavigationLinks' => SiteTree::class,
);
private static $has_many = [
'NavigationLinks' => Link::class . '.Owner',
];

/**
* @var array
* @var array|string[]
*/
private static $many_many_extraFields = array(
'NavigationLinks' => array(
'SortOrder' => 'Int',
),
);
private static array $owns = [
'NavigationLinks',
];

/**
* @var string
Expand Down Expand Up @@ -94,13 +91,10 @@ class NavigationGroup extends DataObject
public function LinkList()
{
if ($this->NavigationLinks()) {
$i = 0;
foreach ($this->NavigationLinks()->sort('SortOrder') as $link) {
++$i;
}
return $this->NavigationLinks()->count();
}

return $i;
return 0;
}

/**
Expand All @@ -111,36 +105,19 @@ public function getCMSFields()
$this->beforeUpdateCMSFields(function (FieldList $fields) {


$fields->removeByName(array(
$fields->removeByName([
'SortOrder',
'NavigationColumnID',
'NavigationLinks',
));
]);

$fields->dataFieldByName('Title')
->setDescription('For internal reference only');

if ($this->ID) {
$config = GridFieldConfig_RelationEditor::create()
->removeComponentsByType([
GridFieldAddNewButton::class,
GridFieldAddExistingAutocompleter::class,
GridFieldEditButton::class,
GridFieldArchiveAction::class,
])->addComponents(
new GridFieldOrderableRows('SortOrder'),
new GridFieldAddExistingSearchButton()
);

$linksField = GridField::create(
'NavigationLinks',
'Links',
$this->NavigationLinks()->sort('SortOrder'),
$config
);

$fields->addFieldsToTab('Root.Main', [
$linksField
MultiLinkField::create('NavigationLinks')
->setTitle('Links')
->setDescription('Add links to this group to display in your footer navigation'),
]);
}
Expand Down
Loading

0 comments on commit 4a247ae

Please sign in to comment.