我一直在尝试扩展ProductCatalogAdmin,因为那是包含我想要导出的产品的ModelAdmin。下面的代码在添加到核心代码(我不想做)时可以正常工作,但在添加为扩展时无法执行任何操作。
PHP
new_lines = [
debug('CUR UPDATED LINE', change(line))
for line
in debug('ALL LINES', get_lines_from_file(filename))
if debug('CUR LINE EMPTY?', not_empty(line))
]
YML
<?php
class ProductCatalogAdminExtension extends DataExtension {
public function getExportFields() {
return array(
'ID' => 'ID',
'InternalItemID' => 'InternalItemID',
'Model' => 'Model',
'Content' => 'Content',
'CostPrice' => 'CostPrice',
'BasePrice' => 'BasePrice',
'Weight' => 'Weight',
'Height' => 'Height',
'Width' => 'Width',
'Depth' => 'Depth',
'Featured' => 'Featured',
'AllowPurchase' => 'AllowPurchase',
'Popularity' => 'Popularity',
'PromoActive' => 'PromoActive',
'PromoDisplay' => 'PromoDisplay',
'PromoType' => 'PromoType',
'PromoAmount' => 'PromoAmount',
'PromoPercent' => 'PromoPercent',
'PromoStartDate' => 'PromoStartDate',
'PromoEndDate' => 'PromoEndDate',
'Image.URL' => 'Image',
'WholesalePrice' => 'WholesalePrice',
'ParentID' => 'ParentID',
'ProductCategory.ID' => 'AdditionalCategories'
);
}
}
我被告知,ModelAdmin的getExportFields()没有extend()调用,因此我必须使用继承而不是扩展。但是,在ModelAdmin的继承下进行它似乎也没有做任何事情。 有趣的是,我没有收到任何错误信息,但它并没有真正失败。
答案 0 :(得分:1)
您可以将ProductCatalogAdmin
子类化并使用Injector,如wmk所指出的那样,但您也可以使用Extension。这适用于任何ModelAdmin
设置:
<?php
class CustomExportExtension extends Extension
{
private $exportFields = [
'ID' => 'ID',
'Reference' => 'Order Number',
// … all your other fields
];
public function updateEditForm($form) {
// Get the gridfield for the model we want, in this case 'Product'
if ($gridField = $form->Fields()->fieldByName('Product')) {
// Get the export button instance from the GridField config
if ($exportButton = $gridField->getConfig()->getComponentByType(GridFieldExportButton::class)) {
// Apply custom export columns to the export button
$exportButton->setExportColumns($this->exportFields);
}
}
}
}
然后只需将扩展名应用于ProductCatalogAdmin
,就像通过YML一样:
ProductCatalogAdmin:
extensions:
- CustomExportExtension
您还可以通过将扩展所附加到DataObject配置的exportFields
部分重写扩展,使其更灵活,可重用于任何ModelAdmin。但对于像你这样的单个用例,上面的工作就可以了。
以上是上述扩展程序的略微修改版本,可用于配置任何 ModelAdmin上的导出字段,而无需创建多个扩展程序。
<?php
class CustomExportExtension extends Extension
{
private $modelClass = null;
public function updateEditForm($form)
{
// Get the gridfield for the current model
if ($gridField = $form->Fields()->fieldByName($this->modelClass)) {
// Get the export button instance from the gridfield config
if ($exportButton = $gridField->getConfig()->getComponentByType(GridFieldExportButton::class)) {
// Look for custom exportFields config
$exportFields = Config::inst()->get($this->modelClass, 'exportFields');
// If custom exportFields aren't set, fall back to summaryfields
if (!$exportFields || !is_array($exportFields)) {
$exportFields = $this->owner->getExportFields();
}
$exportButton->setExportColumns($exportFields);
}
}
}
public function onBeforeInit() {
// Grab the current model-class from the controller
$this->modelClass = $this->owner->getRequest()->param('ModelClass');
}
}
此扩展程序在应导出的模型上查找配置设置exportFields
。如果没有给出,则使用默认的summary_fields
。
以下是如何将该扩展应用于不同的ModelAdmins:
# In your YML File
ProductCatalogAdmin:
extensions:
- CustomExportExtension
Product:
exportFields:
ID: ID
Model: Model
# More fields to export
OrdersAdmin:
extensions:
- CustomExportExtension
Order:
exportFields:
ID: ID
Reference: 'Order Number'
# More fields to export
答案 1 :(得分:0)
是的,在这种情况下你需要继承ProductCatalogAdmin
并告诉SilverStripe使用你的子类。
class MyProductCatalogAdmin extends ProductCatalogAdmin
public function getExportFields() {
//your stuff here
}
}
在你写的config.yml
中:
Injector:
ProductCatalogAdmin:
class: MyProductCatalogAdmin
现在,当调用任何地方ProductCatalogAdmin::create()
时,您的子类将由Injector返回。它不适用于new ProductCatalogAdmin()