To make a module the last one to load we can add something like After: "*"
to our module config.yml
file:
---
Name: silverstripe-cleanup
After: "*"
---
SiteTree:
extensions:
- MetaDataMovementExtension
This should ensure this module is called after all others.
This still may not load after all fields and tabs are added to a page's CMS fields. This is because of the point at which updateCMSFields
is called in a page's getCMSFields
function.
Say this is our updateCMSFields
function:
class MetaDataMovementExtension extends Extension {
function updateCMSFields($fields) {
if ($metadataFields = $fields->fieldByName('Root.Main.Metadata')) {
$fields->removeFieldFromTab('Root.Main', 'Metadata');
$fields->addFieldToTab('Root.Metadata', $metadataFields);
}
}
}
And this is one of our classes:
class HomePage extends Page {
// ...
public function getCMSFields()
{
$fields = parent::getCMSFields();
$slidesField = GridField::create(
'Slides',
'Slide',
$this->Slides(),
GridFieldConfig_RecordEditor::create()
);
$fields->addFieldToTab('Root.Slides', $slidesField);
$fields->addFieldToTab('Root.Column', TextField::create('ColumnTitle', 'Title'));
return $fields;
}
}
The updateCMSFields
hook gets called in SiteTree::getCMSFields
. Looking at our getCMSFields
function above, the updateCMSFields
function will be called at the top of our function at the point that we call parent::getCMSFields()
. After that we then add extra fields. This means our extension will get called before we add extra fields. These extra fields will be placed after our moved metadata tab.
What we can do is wrap our additional fields in each of our getCMSFields
with beforeUpdateCMSFields
:
public function getCMSFields()
{
$self =& $this;
$this->beforeUpdateCMSFields(function ($fields) use ($self) {
$slidesField = new GridField(
'Slides',
'Slide',
$self->Slides(),
GridFieldConfig_RecordEditor::create()
);
$fields->addFieldToTab('Root.Slides', $slidesField);
$fields->addFieldToTab('Root.Column', TextField::create('ColumnTitle', 'Title'));
});
return parent::getCMSFields();
}
This will ensure that our fields are added before updateCMSFields
is called.
An important thing to note when using beforeUpdateCMSFields
is that we need to use $self
instead of $this
inside our beforeUpdateCMSFields
block.