Skip to content

Commit

Permalink
Added AssetAdmin fixes, applied EditForm fix to CMSMain
Browse files Browse the repository at this point in the history
  • Loading branch information
colintucker committed Oct 23, 2016
1 parent 338cf2f commit c0b057a
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 42 deletions.
5 changes: 1 addition & 4 deletions _config/moderno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ LeftAndMain:
extensions:
- AwesomeIconExtension
- ModernoAdminExtension
- CMSMainEditFormExtension

SiteConfig:
extensions:
- ModernoConfigExtension

SiteConfigLeftAndMain:
extensions:
- SiteConfigEditFormExtension

ModernoConfigExtension:
resize_methods:
CropWidth: Crop Width
Expand Down
88 changes: 88 additions & 0 deletions code/extensions/CMSMainEditFormExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* An extension which loads the required EditForm scripts for CMSMain (workaround for a bug in 3.4.0 + Secure Assets).
*/
class CMSMainEditFormExtension extends LeftAndMainExtension
{
/**
* Extension init() method, called by LeftAndMain init() method.
*/
public function init()
{
if (defined('CMS_DIR')) {
Requirements::javascript(CMS_DIR . '/javascript/CMSMain.EditForm.js');
}

if ($this->isAssetAdmin() && $this->hasSecureAssets()) {
Requirements::javascript(MODERNO_ADMIN_DIR . '/javascript/AssetAdmin.EditForm.js');
}
}

/**
* Updates the edit form object from AssetAdmin to provide a workaround for a bug in Secure Assets.
*
* @param Form $form
*/
public function updateEditForm($form)
{
if ($this->isAssetAdmin() && $this->hasSecureAssets()) {

if (($record = $form->getRecord()) && $record instanceof Folder) {

if ($field = $form->Fields()->dataFieldByName('ViewerGroups')) {

// Create Form Object:

$dummy = Form::create(
$this->owner,
'EditForm',
FieldList::create(),
FieldList::create()
);

// Populate Form Data:

$dummy->loadDataFrom($record);

// Define Form Action (allows ViewerGroups field to work in asset EditForm):

$dummy->setFormAction(
sprintf(
'%s/field/File/item/%d/ItemEditForm',
$form->FormAction(),
$record->ID
)
);

// Update Field Object:

$field->setForm($dummy);

}

}

}
}

/**
* Answers true if the extended object is AssetAdmin.
*
* @return boolean
*/
private function isAssetAdmin()
{
return ($this->owner->class == 'AssetAdmin');
}

/**
* Answers true if the File class has the Secure Assets extension applied.
*
* @return boolean
*/
private function hasSecureAssets()
{
return Object::has_extension('File', 'SecureFileExtension');
}
}
17 changes: 0 additions & 17 deletions code/extensions/SiteConfigEditFormExtension.php

This file was deleted.

Loading

0 comments on commit c0b057a

Please sign in to comment.