Skip to content

Commit

Permalink
Add ObjectField Operator for extracting fields from Data Objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
cancan101 committed Nov 20, 2024
1 parent 9037901 commit 30c3bc4
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/Mapping/Operator/Simple/ObjectField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* 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
*/

namespace Pimcore\Bundle\DataImporterBundle\Mapping\Operator\Simple;

use Pimcore\Bundle\DataImporterBundle\Exception\InvalidConfigurationException;
use Pimcore\Bundle\DataImporterBundle\Mapping\Operator\AbstractOperator;
use Pimcore\Bundle\DataImporterBundle\Mapping\Type\TransformationDataTypeService;
use Pimcore\Model\Element\ElementInterface;


class ObjectField extends AbstractOperator
{
private string $attribute;

private string $forwardParameter;

public function setSettings(array $settings): void
{
// are there better defautls than empty string?
$this->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));
}
}
}
1 change: 1 addition & 0 deletions src/PimcoreDataImporterBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services/mapping.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
# -------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
},
];
}

});
1 change: 1 addition & 0 deletions src/Resources/translations/admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 30c3bc4

Please sign in to comment.