From 63ecddab6715666c4f9e0170f597bb39febe36f7 Mon Sep 17 00:00:00 2001 From: James Cocker Date: Thu, 24 Sep 2015 12:31:16 +0100 Subject: [PATCH] Abstracted Photo Gallery Features to an Extension --- _config/galleries.yml | 5 +++- code/PhotoGalleryExtension.php | 47 +++++++++++++++++++++++++++++ code/PhotoGalleryImage.php | 2 +- code/PhotoGalleryPage.php | 55 +++++++--------------------------- 4 files changed, 63 insertions(+), 46 deletions(-) create mode 100644 code/PhotoGalleryExtension.php diff --git a/_config/galleries.yml b/_config/galleries.yml index 2af7d95..45c7494 100644 --- a/_config/galleries.yml +++ b/_config/galleries.yml @@ -1,3 +1,6 @@ LeftAndMain: extra_requirements_css: - - basic-galleries/css/cms.css \ No newline at end of file + - basic-galleries/css/cms.css +PhotoGalleryPage: + extensions: + - PhotoGalleryExtension \ No newline at end of file diff --git a/code/PhotoGalleryExtension.php b/code/PhotoGalleryExtension.php new file mode 100644 index 0000000..eb2b08c --- /dev/null +++ b/code/PhotoGalleryExtension.php @@ -0,0 +1,47 @@ + 'PhotoGalleryImage' + ); + + public function updateCMSFields(FieldList $fields) { + + $gridFieldConfig = GridFieldConfig_RecordEditor::create(); + $gridFieldConfig->addComponent(new GridFieldBulkUpload()); + $gridFieldConfig->addComponent(new GridFieldGalleryTheme('Image')); + $bulkUpload = $gridFieldConfig->getComponentByType('GridFieldBulkUpload'); + $bulkUpload->setUfSetup('setFolderName', "Managed/PhotoGalleries/".$this->owner->ID."-".$this->owner->URLSegment); + $bulkUpload->setUfConfig('canAttachExisting',false); + $bulkUpload->setUfConfig('canPreviewFolder',false); + + $gridFieldConfig->removeComponentsByType('GridFieldPaginator'); + $gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder')); + $gridFieldConfig->addComponent(new GridFieldPaginator(100)); + $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton'); + + $gridfield = new GridField("PhotoGalleryImages", "Image Gallery", $this->owner->PhotoGalleryImages()->sort("SortOrder"), $gridFieldConfig); + $fields->addFieldToTab('Root.ImageGallery', $gridfield); + + $fields->addFieldToTab('Root.ImageGallery', new LiteralField('help'," +

To upload new images:

+
    +
  1. 1. Click the From your computer button above.
  2. +
  3. 2. Locate and select the image(s) you wish to upload.
  4. +
  5. 3. Click on Open/Choose and the image(s) will begin to upload.
  6. +
  7. 4. Click Finish.
  8. +
")); + + $fields->renameField("Content", "Top Content"); + + return $fields; + + } + + public function GetGalleryImages() { + return $this->owner->PhotoGalleryImages()->sort("SortOrder"); + } + +} \ No newline at end of file diff --git a/code/PhotoGalleryImage.php b/code/PhotoGalleryImage.php index cad5566..1350fb8 100644 --- a/code/PhotoGalleryImage.php +++ b/code/PhotoGalleryImage.php @@ -9,7 +9,7 @@ class PhotoGalleryImage extends DataObject { private static $has_one = array( 'Image' => 'Image', - 'PhotoGalleryPage' => 'PhotoGalleryPage' + 'PhotoGalleryPage' => 'Page' ); private static $summary_fields = array( diff --git a/code/PhotoGalleryPage.php b/code/PhotoGalleryPage.php index f1aa71f..7485171 100644 --- a/code/PhotoGalleryPage.php +++ b/code/PhotoGalleryPage.php @@ -5,55 +5,25 @@ class PhotoGalleryPage extends Page { private static $description = "A page including a photo gallery and the ability to bulk upload images"; private static $icon = 'basic-galleries/images/gallery'; private static $singular_name = "Photo Gallery"; - - // One gallery page has many gallery images - private static $has_many = array( - 'PhotoGalleryImages' => 'PhotoGalleryImage' - ); + private static $defaults = array( 'ShowInMenus' => false ); public function getCMSFields() { - - $fields = parent::getCMSFields(); - - // Makes Image Gallery Tab Default, if images exist - if ($this->PhotoGalleryImages()->exists()) { - $fields->insertBefore(new Tab('ImageGallery'), 'Main'); - } - - $gridFieldConfig = GridFieldConfig_RecordEditor::create(); - $gridFieldConfig->addComponent(new GridFieldBulkUpload()); - $gridFieldConfig->addComponent(new GridFieldGalleryTheme('Image')); - $bulkUpload = $gridFieldConfig->getComponentByType('GridFieldBulkUpload'); - $bulkUpload->setUfSetup('setFolderName', "Managed/PhotoGalleries/".$this->ID."-".$this->URLSegment); - $bulkUpload->setUfConfig('canAttachExisting',false); - $bulkUpload->setUfConfig('canPreviewFolder',false); - $gridFieldConfig->removeComponentsByType('GridFieldPaginator'); - $gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder')); - $gridFieldConfig->addComponent(new GridFieldPaginator(100)); - $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton'); - - $gridfield = new GridField("PhotoGalleryImages", "Image Gallery", $this->PhotoGalleryImages()->sort("SortOrder"), $gridFieldConfig); - $fields->addFieldToTab('Root.ImageGallery', $gridfield); + $this->beforeUpdateCMSFields(function($fields) { + // Makes Image Gallery Tab Default, if images exist + if ($this->PhotoGalleryImages()->exists()) { + $fields->insertBefore(new Tab('ImageGallery'), 'Main'); + } + }); - $fields->addFieldToTab('Root.ImageGallery', new LiteralField('help'," -

To upload new images:

-
    -
  1. 1. Click the From your computer button above.
  2. -
  3. 2. Locate and select the image(s) you wish to upload.
  4. -
  5. 3. Click on Open/Choose and the image(s) will begin to upload.
  6. -
  7. 4. Click Finish.
  8. -
")); - - $fields->renameField("Content", "Top Content"); - + $fields = parent::getCMSFields(); + return $fields; - - + } function onBeforeWrite() { @@ -68,9 +38,6 @@ function onBeforeWrite() { } class PhotoGalleryPage_Controller extends Page_Controller { - - public function GetGalleryImages() { - return $this->PhotoGalleryImages()->sort("SortOrder"); - } + } \ No newline at end of file