Refactor and cleanup SiteTreeFileFormFactoryExtension

This commit is contained in:
Damian Mooyman 2018-01-16 16:53:11 +13:00
parent 8b19afe235
commit cb2745c737
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
3 changed files with 35 additions and 22 deletions

8
_config/asset-admin.yml Normal file
View File

@ -0,0 +1,8 @@
---
Name: cms-asset-admin
OnlyIf:
ModuleExists: silverstripe/asset-admin
---
SilverStripe\AssetAdmin\Forms\FileFormFactory:
extensions:
- SilverStripe\CMS\Model\SiteTreeFileFormFactoryExtension

View File

@ -7,9 +7,6 @@ SilverStripe\Admin\LeftAndMain:
SilverStripe\Assets\File: SilverStripe\Assets\File:
extensions: extensions:
- SilverStripe\CMS\Model\SiteTreeFileExtension - SilverStripe\CMS\Model\SiteTreeFileExtension
SilverStripe\AssetAdmin\Forms\FileFormFactory:
extensions:
- SilverStripe\CMS\Model\SiteTreeFileFormFactoryExtension
--- ---
Name: cmsmodals Name: cmsmodals
--- ---

View File

@ -3,36 +3,44 @@
namespace SilverStripe\CMS\Model; namespace SilverStripe\CMS\Model;
use SilverStripe\Assets\File; use SilverStripe\Assets\File;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\ReadonlyField; use SilverStripe\Forms\ReadonlyField;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataExtension;
use SilverStripe\Versioned\Versioned; use SilverStripe\ORM\FieldType\DBHTMLText;
/** /**
* Extension applied to {@see FileFormFactoryExtension} object to track links to {@see SiteTree} records. * Extension applied to {@see FileFormFactory} to decorate with a "Used on:" information area.
* * Uses tracking provided by {@see SiteTreeFileExtension} to generate this.
* {@see SiteTreeLinkTracking} for the extension applied to {@see SiteTree}
*
* Note that since both SiteTree and File are versioned, LinkTracking and ImageTracking will
* only be enabled for the Stage record.
* *
* @property File $owner * @property File $owner
*/ */
class SiteTreeFileFormFactoryExtension extends DataExtension class SiteTreeFileFormFactoryExtension extends DataExtension
{ {
public function updateForm($form, $controller, $name, $context) public function updateFormFields(FieldList $fields, $controller, $formName, $context)
{ {
// Create field
/** @var File|SiteTreeFileExtension $record */
$record = $context['Record']; $record = $context['Record'];
$fields = $form->Fields(); $usedOnField = ReadonlyField::create(
$fields->insertAfter(
'LastEdited',
ReadonlyField::create(
'BackLinkCount', 'BackLinkCount',
_t(__CLASS__.'.BACKLINKCOUNT', 'Used on:'), _t(__CLASS__.'.BACKLINKCOUNT', 'Used on:'),
$record->BackLinkTrackingCount() . ' ' . _t(__CLASS__.'.PAGES', 'page(s)') $record->BackLinkTrackingCount() . ' ' . _t(__CLASS__.'.PAGES', 'page(s)')
) )
->addExtraClass('cms-description-toggle') ->addExtraClass('cms-description-toggle');
->setDescription($record->BackLinkHTMLList())
); // Add table
/** @var DBHTMLText $backlinkHTML */
$backlinkHTML = $record->BackLinkHTMLList();
if (trim($backlinkHTML->forTemplate())) {
$usedOnField->setDescription($backlinkHTML);
}
// Add field to new tab
$tab = Tab::create('Usage', _t(__CLASS__.'.USAGE', 'Usage'), $usedOnField);
/** @var TabSet $tabset */
$tabset = $fields->fieldByName('Editor');
$tabset->push($tab);
} }
} }