Skip to content

Commit

Permalink
Update GalleryExtension.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Otis Wright authored Nov 21, 2016
1 parent 44bf166 commit 33e08bc
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions code/GalleryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,43 @@ class GalleryExtension extends SiteTreeExtension
{

private static $has_many = [
'PrimaryGalleryImages' => 'PrimaryGalleryImage',
'PrimaryGalleryImages' => 'PrimaryGalleryImage',
'SecondaryGalleryImages' => 'SecondaryGalleryImage',
];

public function updateCMSFields(FieldList $fields)
{
foreach (['primary', 'secondary'] as $lower) {
$upper = ucfirst($lower);
$upper = ucfirst($lower);

$fields->removeByName("{$upper}GalleryImages");
$config = $this->owner->config()->get("{$lower}_gallery");
if (is_null($config) || (isset($config['enabled']) && $config['enabled'] === false)) {
continue;
}

$config['title'] = isset($config['title']) ? $config['title'] : "{$upper} Gallery";
$config['title'] = isset($config['title']) ? $config['title'] : "{$upper} Gallery";
$config['folder'] = isset($config['folder']) ? $config['folder'] : "{$upper}-Gallery-Images";

$GridFieldConfig = new GridFieldConfig_RecordEditor();
$GridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
$GridFieldConfig->addComponent($bulkUploadConfig = new GridFieldBulkUpload());
$GridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$GridFieldConfig->addComponent(new GridFieldOrderableRows('SortOrder'));
$GridFieldConfig->addComponent(new GridFieldGalleryTheme('Image'));
$bulkUploadConfig->setUfSetup('setFolderName', "Images/{$config['folder']}");
$GridField = new GridField("{$upper}GalleryGridField", $config['title'], $this->owner->{"{$upper}GalleryImages"}(), $GridFieldConfig);

/** @var TabSet $rootTab */
//We need to repush Metadata to ensure it is the last tab
$rootTab = $fields->fieldByName('Root');
$rootTab->push($tab = Tab::create("{$upper}Gallery"));
if ($rootTab->fieldByName('Metadata')) {
$metaChildren = $rootTab->fieldByName('Metadata')->getChildren();
$rootTab->removeByName('Metadata');
$rootTab->push(Tab::create('Metadata')->setChildren($metaChildren));
if ($this->owner->exists()) {
$rootTab->push($tab = Tab::create("{$upper}Gallery"));
}

$tab->setTitle($config['title']);
$fields->addFieldToTab("Root.{$upper}Gallery", $GridField);
if ($this->owner->exists()) {
$fields->addFieldToTab("Root.{$upper}Gallery", $GridField);
$tab->setTitle($config['title']);
}
}
}
}

0 comments on commit 33e08bc

Please sign in to comment.