Skip to content

Commit

Permalink
Merge pull request #162 from muskie9/refactor/onBeforeUpdateCMSFields
Browse files Browse the repository at this point in the history
REFACTOR header image getCMSFields
  • Loading branch information
muskie9 authored Nov 14, 2018
2 parents 80746ab + 432eacd commit 71edfdf
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions src/Model/HeaderImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Security;

Expand Down Expand Up @@ -46,35 +47,35 @@ class HeaderImage extends DataObject
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();

$fields->removeByName([
'HeaderLinkID',
'Image',
'PageID',
]);

$fields->insertAfter(
'Content',
LinkField::create('HeaderLinkID', 'Link')
);

$image_field = UploadField::create('Image', 'Header Image')
->setFolderName('Uploads/HeaderImages')
->setIsMultiUpload(false);
$image_field->getValidator()->allowedExtensions = array(
'jpg',
'jpeg',
'gif',
'png',
);

$fields->insertBefore(
'Title',
$image_field
);

return $fields;
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->removeByName([
'HeaderLinkID',
'Image',
'PageID',
]);

$fields->insertAfter(
'Content',
LinkField::create('HeaderLinkID', 'Link')
);

$image_field = UploadField::create('Image', 'Header Image')
->setFolderName('Uploads/HeaderImages')
->setIsMultiUpload(false);
$image_field->getValidator()->allowedExtensions = [
'jpg',
'jpeg',
'gif',
'png',
];

$fields->insertBefore(
'Title',
$image_field
);
});

return parent::getCMSFields();
}

/**
Expand Down

0 comments on commit 71edfdf

Please sign in to comment.