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(
'BackLinkCount',
_t(__CLASS__.'.BACKLINKCOUNT', 'Used on:'),
$record->BackLinkTrackingCount() . ' ' . _t(__CLASS__.'.PAGES', 'page(s)')
)
->addExtraClass('cms-description-toggle');
$fields->insertAfter( // Add table
'LastEdited', /** @var DBHTMLText $backlinkHTML */
ReadonlyField::create( $backlinkHTML = $record->BackLinkHTMLList();
'BackLinkCount', if (trim($backlinkHTML->forTemplate())) {
_t(__CLASS__.'.BACKLINKCOUNT', 'Used on:'), $usedOnField->setDescription($backlinkHTML);
$record->BackLinkTrackingCount() . ' ' . _t(__CLASS__.'.PAGES', 'page(s)') }
)
->addExtraClass('cms-description-toggle') // Add field to new tab
->setDescription($record->BackLinkHTMLList()) $tab = Tab::create('Usage', _t(__CLASS__.'.USAGE', 'Usage'), $usedOnField);
); /** @var TabSet $tabset */
$tabset = $fields->fieldByName('Editor');
$tabset->push($tab);
} }
} }