I am attempting to extend the bulk object import functionality in Pimcore to restrict users to the fields they have available to them in their custom layout.
The file I need to override is
pimcore/modules/admin/controllers/ObjectController.php
and I believe the method I need to extend is:
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
]);
}
Specifically these two lines:
$class = Object\ClassDefinition::getById($this->getParam("classId"));
$fields = $class->getFieldDefinitions();
What I'd like to do is figure out how to get the fields the user can see in the custom layout, then filter out $fields accordingly. I'd like to avoid overwriting the core file, and instead would love to extend it with a plugin.
You probably have to create a plugin that overrides the menu items in the UI, in order to substitute the standard object import with your custom one.
You will need to create a CustomObjectController that can then extend the standard ObjectController.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With