Pimcore:如何覆盖内置控制器

Posted

技术标签:

【中文标题】Pimcore:如何覆盖内置控制器【英文标题】:Pimcore: How to override built-in controller 【发布时间】:2016-11-19 04:13:26 【问题描述】:

我正在尝试扩展 Pimcore 中的批量对象导入功能,以将用户限制在自定义布局中可用的字段。

我需要覆盖的文件是

pimcore/modules/admin/controllers/ObjectController.php

我相信我需要扩展的方法是:

public function importGetFileInfoAction()

    $success = true;
    $supportedFieldTypes = ["checkbox", "country", "date", "datetime", "href", "image", "input", "language", "table", "multiselect", "numeric", "password", "select", "slider", "textarea", "wysiwyg", "objects", "multihref", "geopoint", "geopolygon", "geobounds", "link", "user", "email", "gender", "firstname", "lastname", "newsletterActive", "newsletterConfirmed", "countrymultiselect", "objectsMetadata"];
    $file = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $this->getParam("id");
    // determine type
    $dialect = Tool\Admin::determineCsvDialect(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $this->getParam("id") . "_original");
    $count = 0;
    if (($handle = fopen($file, "r")) !== false) 
        while (($rowData = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar)) !== false) 
            if ($count == 0) 
                $firstRowData = $rowData;
            
            $tmpData = [];
            foreach ($rowData as $key => $value) 
                $tmpData["field_" . $key] = $value;
            
            $data[] = $tmpData;
            $cols = count($rowData);
            $count++;
            if ($count > 18) 
                break;
            
        
        fclose($handle);
    
    // get class data
    $class = Object\ClassDefinition::getById($this->getParam("classId"));
    $fields = $class->getFieldDefinitions();
    $availableFields = [];
    foreach ($fields as $key => $field) 
        $config = null;
        $title = $field->getName();
        if (method_exists($field, "getTitle")) 
            if ($field->getTitle()) 
                $title = $field->getTitle();
            
        
        if (in_array($field->getFieldType(), $supportedFieldTypes)) 
            $availableFields[] = [$field->getName(), $title . "(" . $field->getFieldType() . ")"];
        
    
    $mappingStore = [];
    for ($i = 0; $i < $cols; $i++) 
        $mappedField = null;
        if ($availableFields[$i]) 
            $mappedField = $availableFields[$i][0];
        
        $firstRow = $i;
        if (is_array($firstRowData)) 
            $firstRow = $firstRowData[$i];
            if (strlen($firstRow) > 40) 
                $firstRow = substr($firstRow, 0, 40) . "...";
            
        
        $mappingStore[] = [
            "source" => $i,
            "firstRow" => $firstRow,
            "target" => $mappedField
        ];
    
    //How many rows
    $csv = new SplFileObject($file);
    $csv->setFlags(SplFileObject::READ_CSV);
    $csv->setCsvControl($dialect->delimiter, $dialect->quotechar, $dialect->escapechar);
    $rows = 0;
    $nbFields = 0;
    foreach ($csv as $fields) 
        if (0 === $rows) 
            $nbFields = count($fields);
            $rows++;
         elseif ($nbFields == count($fields)) 
            $rows++;
        
    
    $this->_helper->json([
        "success" => $success,
        "dataPreview" => $data,
        "dataFields" => array_keys($data[0]),
        "targetFields" => $availableFields,
        "mappingStore" => $mappingStore,
        "rows" => $rows,
        "cols" => $cols
    ]);

特别是这两行:

$class = Object\ClassDefinition::getById($this-&gt;getParam("classId"));

$fields = $class-&gt;getFieldDefinitions();

我想做的是弄清楚如何获取用户可以在自定义布局中看到的字段,然后相应地过滤掉$fields。我想避免覆盖核心文件,而是希望使用插件对其进行扩展。

【问题讨论】:

您是否覆盖了上述文件,如果您有答案,请作为答案发布。 不幸的是,我没有找到答案。我最终覆盖了核心文件。 【参考方案1】:

您可能必须创建一个插件来覆盖 UI 中的菜单项,以便将标准对象导入替换为您的自定义对象。

您需要创建一个 CustomObjectController,然后可以扩展标准 ObjectController。

【讨论】:

请以编程方式覆盖文件

以上是关于Pimcore:如何覆盖内置控制器的主要内容,如果未能解决你的问题,请参考以下文章

从视图/控制器获取 pimcore 片段名称

Pimcore Extjs 如何在不同的插件中覆盖相同的方法

pimcore:使用本地化字段更新对象

覆盖 UIActivityViewController 默认行为

Pimcore - 如何将自定义按钮添加到对象树菜单

如何扩展 pimcore 对象模型和他的资源类