From 30c3bc42a1d9c3cff213662c85793fc49fa7bac9 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Wed, 20 Nov 2024 15:54:04 -0500 Subject: [PATCH] Add ObjectField Operator for extracting fields from Data Objects. --- src/Mapping/Operator/Simple/ObjectField.php | 84 +++++++++++++++++++ src/PimcoreDataImporterBundle.php | 1 + src/Resources/config/services/mapping.yml | 4 + .../mapping/operator/objectField.js | 51 +++++++++++ src/Resources/translations/admin.en.yml | 1 + 5 files changed, 141 insertions(+) create mode 100644 src/Mapping/Operator/Simple/ObjectField.php create mode 100644 src/Resources/public/js/pimcore/configuration/components/mapping/operator/objectField.js diff --git a/src/Mapping/Operator/Simple/ObjectField.php b/src/Mapping/Operator/Simple/ObjectField.php new file mode 100644 index 00000000..6c2edff1 --- /dev/null +++ b/src/Mapping/Operator/Simple/ObjectField.php @@ -0,0 +1,84 @@ +attribute = $settings['attribute'] ?? ''; + $this->forwardParameter = $settings['forwardParameter'] ?? ''; + } + + public function process(mixed $inputData, bool $dryRun = false): mixed + { + if (!$inputData instanceof ElementInterface) { + // is this how to handle type mismatch? + return null; + } + + if(!$this->attribute){ + // is this how to handle no attrinute + return null; + } + + // better to pull full logic from ObjectFieldGetter / AnyGetter + $getter = 'get' . ucfirst($this->attribute); + + if (!method_exists($inputData, $getter)) { + // is there a better default here? + return null; + } + + if ($this->forwardParameter) { + $value = $inputData->$getter($this->forwardParameter); + } else { + $value = $inputData->$getter(); + } + + // this expands paths + if ($value instanceof ElementInterface) { + $value = $value->getFullPath(); + } + + return $value; + } + + /** + * + * @throws InvalidConfigurationException + */ + public function evaluateReturnType(string $inputType, int $index = null): string + { + if ($inputType === TransformationDataTypeService::DATA_OBJECT) { + // for numerics? + return TransformationDataTypeService::DEFAULT_TYPE; + } else { + throw new InvalidConfigurationException(sprintf("Unsupported input type '%s' for load data object operator at transformation position %s", $inputType, $index)); + } + } +} \ No newline at end of file diff --git a/src/PimcoreDataImporterBundle.php b/src/PimcoreDataImporterBundle.php index 0998dff5..f345250a 100644 --- a/src/PimcoreDataImporterBundle.php +++ b/src/PimcoreDataImporterBundle.php @@ -125,6 +125,7 @@ public function getJsPaths(): array '/bundles/pimcoredataimporter/js/pimcore/configuration/components/mapping/operator/gallery.js', '/bundles/pimcoredataimporter/js/pimcore/configuration/components/mapping/operator/imageAdvanced.js', '/bundles/pimcoredataimporter/js/pimcore/configuration/components/mapping/operator/loadDataObject.js', + '/bundles/pimcoredataimporter/js/pimcore/configuration/components/mapping/operator/objectField.js', '/bundles/pimcoredataimporter/js/pimcore/configuration/components/mapping/operator/reduceArrayKeyValuePairs.js', '/bundles/pimcoredataimporter/js/pimcore/configuration/components/mapping/operator/flattenArray.js', '/bundles/pimcoredataimporter/js/pimcore/configuration/components/mapping/operator/staticText.js', diff --git a/src/Resources/config/services/mapping.yml b/src/Resources/config/services/mapping.yml index a5038056..8e7da9cc 100644 --- a/src/Resources/config/services/mapping.yml +++ b/src/Resources/config/services/mapping.yml @@ -62,6 +62,10 @@ services: tags: - { name: "pimcore.datahub.data_importer.operator", type: "loadDataObject" } + Pimcore\Bundle\DataImporterBundle\Mapping\Operator\Simple\ObjectField: + tags: + - { name: "pimcore.datahub.data_importer.operator", type: "objectField" } + # ------------------- # factory operators # ------------------- diff --git a/src/Resources/public/js/pimcore/configuration/components/mapping/operator/objectField.js b/src/Resources/public/js/pimcore/configuration/components/mapping/operator/objectField.js new file mode 100644 index 00000000..780555dc --- /dev/null +++ b/src/Resources/public/js/pimcore/configuration/components/mapping/operator/objectField.js @@ -0,0 +1,51 @@ +/** + * Pimcore + * + * This source file is available under two different licenses: + * - GNU General Public License version 3 (GPLv3) + * - Pimcore Commercial License (PCL) + * Full copyright and license information is available in + * LICENSE.md which is distributed with this source code. + * + * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) + * @license http://www.pimcore.org/license GPLv3 and PCL + */ + +pimcore.registerNS("pimcore.plugin.pimcoreDataImporterBundle.configuration.components.mapping.operator.objectField"); +pimcore.plugin.pimcoreDataImporterBundle.configuration.components.mapping.operator.objectField = Class.create(pimcore.plugin.pimcoreDataImporterBundle.configuration.components.mapping.abstractOperator, { + + type: 'objectField', + + getMenuGroup: function() { + return this.menuGroups.dataManipulation; + }, + + getIconClass: function() { + return "pimcore_nav_icon_object pimcore_icon_overlay_add"; + }, + + getFormItems: function() { + return [ + { + xtype: 'textfield', + fieldLabel: t('attribute'), + value: this.data.settings ? this.data.settings.attribute : '', + name: 'settings.attribute', + listeners: { + change: this.inputChangePreviewUpdate.bind(this) + } + }, + + { + xtype: 'textfield', + fieldLabel: t('forward_parameter'), + value: this.data.settings ? this.data.settings.forward_parameter : '', + name: 'settings.forward_parameter', + listeners: { + change: this.inputChangePreviewUpdate.bind(this) + } + }, + ]; + } + +}); diff --git a/src/Resources/translations/admin.en.yml b/src/Resources/translations/admin.en.yml index 918854e2..5c0ae5d9 100644 --- a/src/Resources/translations/admin.en.yml +++ b/src/Resources/translations/admin.en.yml @@ -176,6 +176,7 @@ plugin_pimcore_datahub_data_importer_configpanel_transformation_pipeline_accept_ plugin_pimcore_datahub_data_importer_configpanel_transformation_pipeline_data_types: Data Types plugin_pimcore_datahub_data_importer_configpanel_transformation_pipeline_data_manipulation: Data Manipulation plugin_pimcore_datahub_data_importer_configpanel_transformation_pipeline_load_import: Load/Import +plugin_pimcore_datahub_data_importer_configpanel_transformation_pipeline_objectField: ObjectField Getter plugin_pimcore_datahub_data_importer_configpanel_preview_dataindex: Data Index plugin_pimcore_datahub_data_importer_configpanel_preview_label: Label plugin_pimcore_datahub_data_importer_configpanel_preview_data: Data