【问题标题】:Export products table via CSV in CMS在 CMS 中通过 CSV 导出产品表
【发布时间】:2017-07-04 01:56:17
【问题描述】:

我一直在尝试扩展 ProductCatalogAdmin,因为那是保存我要导出的产品的 ModelAdmin。下面的代码在添加到核心代码时可以正常工作(我不想这样做),但在作为扩展添加时无法执行任何操作。

PHP

<?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'
            );
    }

}

YML

---
Name: mysite
After:
  - 'framework/*'
  - 'cms/*'
---
# YAML configuration for SilverStripe
# See http://doc.silverstripe.org/framework/en/topics/configuration
# Caution: Indentation through two spaces, not tabs
SSViewer:
  theme: 'simple'
SiteConfig:
  extensions:
    - SiteConfigExtension
ProductCatalogAdmin:
  extensions:
    - ProductCatalogAdminExtension

有人告诉我,ModelAdmin 的 getExportFields() 没有 extend() 调用,所以我必须使用继承而不是扩展。但是,在 ModelAdmin 的继承下执行此操作似乎也无济于事。 有趣的是,我没有收到任何错误消息,它并没有真正失败。

【问题讨论】:

    标签: php silverstripe silvershop


    【解决方案1】:

    正如 wmk 所指出的,您可以继承 ProductCatalogAdmin 并使用 Injector,但您也可以使用 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
    

    您还可以通过将 exportFields 作为扩展附加到的 DataObject 配置的一部分来重写扩展,使其对任何 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

    您可以通过以下方式将该扩展应用到不同的 ModelAdmin:

    # 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
    

    【讨论】:

    • 这很有帮助。我必须做一些调试才能弄清楚,由于我的模型在命名空间下,所以在定义 $gridField var 时,我需要在名称字段中写出完整的命名空间,所以 $gridField = $form-&gt;Fields()-&gt;fieldByName('MyProject\Name\Space\Product')
    【解决方案2】:

    是的,在这种情况下,您需要继承 ProductCatalogAdmin 并告诉 SilverStripe 使用您的子类。

    class MyProductCatalogAdmin extends ProductCatalogAdmin
    
        public function getExportFields() {
            //your stuff here
        }
    }
    

    在您的config.yml 中您写道:

    Injector:
      ProductCatalogAdmin:
        class: MyProductCatalogAdmin
    

    现在,当任何地方调用ProductCatalogAdmin::create() 时,Injector 将返回您的子类。它不适用于new ProductCatalogAdmin()

    另见Injector Docs

    【讨论】:

      猜你喜欢
      • 2014-03-12
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多