Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ObjectField Operator for extracting fields from Data Objects. #436

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ and the second item reflects the value for the longitude attribute.
- **Trim**: Removes leading and/or tailing white spaces from string.
- **Static Text**: Adds a static text to the value - either prepends or appends it.
- **Conditional Conversion**: String values are converted to other string values (e.g. '0' to '1' or 'csv-value' to 'object-value'). Multiple conversions can be configured by separating the values with pipe symbol ('|') (e.g. '0|1|2' to 'some|other|values'). An asterisk can be used as a wildcard (e.g. '0|\*' to 'no value|default' where '0' will be converted to 'no value' and all other values to 'default').
- **ObjectField**: Extracts the value for a specified field from a DataObject. This is similar to `ObjectFieldGetter` or `AnyGetter`.

#### What Operators are NOT supposed to do:
- **Complex statistical calculations**: you possibly get an idea like that you can explode string to array and then calculate some average value, but it is wrong place to do it. Please keep the import peformance light.
Expand Down
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
Loading