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

FIX: localized fields from brick and fieldcollections can be added to index #1872

Merged
merged 16 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
54 changes: 41 additions & 13 deletions src/CoreShop/Bundle/IndexBundle/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,28 @@ protected function getObjectbrickFields(array $allowedBricks, &$result): array
$result[$key]['childs'] = [];

foreach ($fields as $field) {
$fieldConfig = $this->getFieldConfiguration($field);
$fieldConfig['getter'] = 'brick';
$fieldConfig['configuration'] = [
'className' => $key,
'key' => $field->getName(),
];
$result[$key]['childs'][] = $fieldConfig;
if ($field instanceof DataObject\ClassDefinition\Data\Localizedfields) {
$localizedfields = $this->getLocalizedFields($field)['localizedfields'];

foreach ($localizedfields['childs'] as &$child) {
$child['getter'] = 'brick';
$child['configuration'] = [
'className' => $key,
'key' => $field->getName(),
];
}

$result[$key]['childs'][] = $localizedfields;
} else {
$fieldConfig = $this->getFieldConfiguration($field);

$fieldConfig['getter'] = 'brick';
$fieldConfig['configuration'] = [
'className' => $key,
'key' => $field->getName(),
];
$result[$key]['childs'][] = $fieldConfig;
}
}
}
}
Expand All @@ -260,13 +275,26 @@ protected function getFieldcollectionFields(array $allowedCollections, &$result)
$result[$key]['childs'] = [];

foreach ($fieldDefinition as $fieldcollectionField) {
$fieldConfig = $this->getFieldConfiguration($fieldcollectionField);
$fieldConfig['getter'] = 'fieldcollection';
$fieldConfig['configuration'] = [
'className' => $key,
];
if ($fieldcollectionField instanceof DataObject\ClassDefinition\Data\Localizedfields) {
$localizedfields = $this->getLocalizedFields($fieldcollectionField)['localizedfields'];

foreach ($localizedfields['childs'] as &$child) {
$child['getter'] = 'fieldcollection';
$child['configuration'] = [
'className' => $key,
];
}

$result[$key]['childs'][] = $fieldConfig;
$result[$key]['childs'][] = $localizedfields;
} else {
$fieldConfig = $this->getFieldConfiguration($fieldcollectionField);

$fieldConfig['getter'] = 'fieldcollection';
$fieldConfig['configuration'] = [
'className' => $key,
];
$result[$key]['childs'][] = $fieldConfig;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/CoreShop/Bundle/IndexBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ services:

# Index Getter
CoreShop\Component\Index\Getter\BrickGetter:
arguments:
- '@coreshop.translation_locale_provider'
tags:
- { name: coreshop.index.getter, type: brick, form-type: CoreShop\Bundle\IndexBundle\Form\Type\Getter\BrickGetterFormType}

Expand All @@ -140,6 +142,8 @@ services:
- { name: coreshop.index.getter, type: classificationstore, form-type: CoreShop\Bundle\IndexBundle\Form\Type\Getter\ClassificationStoreGetterFormType}

CoreShop\Component\Index\Getter\FieldCollectionGetter:
arguments:
- '@coreshop.translation_locale_provider'
tags:
- { name: coreshop.index.getter, type: fieldcollection, form-type: CoreShop\Bundle\IndexBundle\Form\Type\Getter\FieldCollectionGetterFormType}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,17 @@ coreshop.index.fields = Class.create({
});

tree.addListener('itemdblclick', function (tree, record, item, index, e, eOpts) {
if (!record.data.root && record.datatype !== 'layout' && record.data.dataType !== 'localizedfields') {
if (
!record.data.root &&
(
record.datatype &&
record.datatype !== 'layout'
) ||
(
record.data.dataType &&
record.data.dataType !== 'localizedfields'
)
) {
var copy = Ext.apply({}, record.data);

copy.id = Ext.id();
Expand Down Expand Up @@ -317,7 +327,29 @@ coreshop.index.fields = Class.create({

baseNode = tree.getRootNode().appendChild(baseNode);
for (var j = 0; j < data[keys[i]].childs.length; j++) {
var node = this.addDataChild.call(baseNode, data[keys[i]].childs[j].fieldtype, data[keys[i]].childs[j], data[keys[i]].nodeType, data[keys[i]].className);
if (
(data[keys[i]].nodeType == 'objectbricks' ||
data[keys[i]].nodeType == 'fieldcollections') &&
data[keys[i]].childs[j].nodeLabel == 'localizedfields') {

var text = t(data[keys[i]].childs[j].nodeLabel);

var node = {
type: 'layout',
allowDrag: false,
iconCls: 'pimcore_icon_' + data[keys[i]].childs[j].nodeType,
text: text
};

node = tree.getRootNode().appendChild(node);

for (var k = 0; k < data[keys[i]].childs[j].childs.length; k++) {
var innerNode = this.addDataChild.call(node, data[keys[i]].childs[j].childs[k].fieldtype, data[keys[i]].childs[j].childs[k], data[keys[i]].nodeType, data[keys[i]].className);
node.appendChild(innerNode);
}
} else {
var node = this.addDataChild.call(baseNode, data[keys[i]].childs[j].fieldtype, data[keys[i]].childs[j], data[keys[i]].nodeType, data[keys[i]].className);
}

baseNode.appendChild(node);
}
Expand Down
38 changes: 33 additions & 5 deletions src/CoreShop/Component/Index/Getter/BrickGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@

use CoreShop\Component\Index\Model\IndexableInterface;
use CoreShop\Component\Index\Model\IndexColumnInterface;
use CoreShop\Component\Pimcore\DataObject\LocaleFallbackHelper;
use CoreShop\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data\Localizedfields;
use Pimcore\Model\DataObject\Localizedfield;
use Pimcore\Model\DataObject\Objectbrick;
use Pimcore\Model\DataObject\Objectbrick\Data\AbstractData;
use Pimcore\Tool;

class BrickGetter implements GetterInterface
{
public function __construct(protected TranslationLocaleProviderInterface $localeProvider)
{
}

public function get(IndexableInterface $object, IndexColumnInterface $config): mixed
{
$columnConfig = $config->getConfiguration();
$getterConfig = $config->getGetterConfig();

if (!isset($getterConfig['brickField']) || !isset($columnConfig['className']) || !isset($columnConfig['key'])) {
if (!isset($getterConfig['brickField'], $columnConfig['className'], $columnConfig['key'])) {
return null;
}

Expand All @@ -37,19 +48,36 @@ public function get(IndexableInterface $object, IndexColumnInterface $config): m
}

$brickContainer = $object->$brickContainerGetter();

$brickGetter = 'get' . ucfirst($columnConfig['className']);

if (!$brickContainer) {
if (!$brickContainer instanceof Objectbrick) {
return null;
}

$brick = $brickContainer->$brickGetter();

if ($brick) {
$fieldGetter = 'get' . ucfirst($columnConfig['key']);
if (!$brick instanceof AbstractData) {
return null;
}

$fieldGetter = 'get' . ucfirst($columnConfig['key']);

$fd = $brick->getDefinition()->getFieldDefinition($columnConfig['key']);

if (!$fd instanceof Localizedfields) {
return $brick->$fieldGetter();
}

return null;
$getter = 'get' . ucfirst($config->getObjectKey());

return LocaleFallbackHelper::useFallbackValues(function() use ($brick, $getter) {
$values = [];
foreach ($this->localeProvider->getDefinedLocalesCodes() as $locale) {
$values[$locale] = $brick->$getter($locale);
}

return $values;
}, true);
}
}
32 changes: 30 additions & 2 deletions src/CoreShop/Component/Index/Getter/FieldCollectionGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@

use CoreShop\Component\Index\Model\IndexableInterface;
use CoreShop\Component\Index\Model\IndexColumnInterface;
use CoreShop\Component\Pimcore\DataObject\LocaleFallbackHelper;
use CoreShop\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Pimcore\Model\DataObject\Fieldcollection;
use Pimcore\Model\DataObject\Localizedfield;

class FieldCollectionGetter implements GetterInterface
{
public function __construct(protected TranslationLocaleProviderInterface $localeProvider)
{
}

public function get(IndexableInterface $object, IndexColumnInterface $config): mixed
{
$columnConfig = $config->getConfiguration();
Expand All @@ -44,9 +51,30 @@ public function get(IndexableInterface $object, IndexColumnInterface $config): m
}

foreach ($validItems as $item) {
if (method_exists($item, $fieldGetter)) {
$fieldValues[] = $item->$fieldGetter();
$fd = $item->getDefinition()->getFieldDefinition($config->getObjectKey());

if (!$fd) {
continue;
}

if (!method_exists($item, $fieldGetter)) {
continue;
}

if ($fd instanceof Localizedfield) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dpfaffenbauer I think it would work but $fd is not instance of LocalizedField. It is instance of field under the localizedfields. Thats why I used
if ($item->getDefinition()->getFieldDefinition('localizedfields')) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that only checks if there are localized-fields in the fieldcollection... which is useless basically. We need to know if the selected field is localized or not. But I see that there is an issue here. I will check again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason that checks if that field is inside localized-fields. If you have input and localized input that will be true only for localized input. Not sure why is that the case tho.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed some more, worked for me now.

$fieldValues[] = LocaleFallbackHelper::useFallbackValues(function() use ($item, $fieldGetter) {
$values = [];

foreach ($this->localeProvider->getDefinedLocalesCodes() as $locale) {
$values[$locale] = $item->$fieldGetter($locale);
}

return $values;
});
continue;
}

$fieldValues[] = $item->$fieldGetter();
}

return count($fieldValues) > 0 ? $fieldValues : null;
Expand Down
19 changes: 9 additions & 10 deletions src/CoreShop/Component/Index/Getter/LocalizedFieldGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

use CoreShop\Component\Index\Model\IndexableInterface;
use CoreShop\Component\Index\Model\IndexColumnInterface;
use CoreShop\Component\Pimcore\DataObject\InheritanceHelper;
use CoreShop\Component\Pimcore\DataObject\LocaleFallbackHelper;
use CoreShop\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Pimcore\Model\DataObject;

class LocalizedFieldGetter implements GetterInterface
{
Expand All @@ -29,16 +30,14 @@ public function get(IndexableInterface $object, IndexColumnInterface $config): a
{
$getter = 'get' . ucfirst($config->getObjectKey());

$fallbackMemory = DataObject\Localizedfield::getGetFallbackValues();
DataObject\Localizedfield::setGetFallbackValues(true);
return LocaleFallbackHelper::useFallbackValues(function() use($object, $getter) {
$values = [];

$values = [];
foreach ($this->localeProvider->getDefinedLocalesCodes() as $locale) {
$values[$locale] = $object->$getter($locale);
}
foreach ($this->localeProvider->getDefinedLocalesCodes() as $locale) {
$values[$locale] = $object->$getter($locale);
}

DataObject\Localizedfield::setGetFallbackValues($fallbackMemory);

return $values;
return $values;
});
}
}
36 changes: 36 additions & 0 deletions src/CoreShop/Component/Pimcore/DataObject/LocaleFallbackHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

declare(strict_types=1);

namespace CoreShop\Component\Pimcore\DataObject;

use Pimcore\Model\DataObject;

class LocaleFallbackHelper
{
/**
* This function enables usage of locale fallback in Pimcore and resets the state of locale fallback automatically
* after your functions is finished.
*/
public static function useFallbackValues(\Closure $function, bool $useFallback = true): mixed
{
$backup = DataObject\Localizedfield::getGetFallbackValues();
DataObject\Localizedfield::setGetFallbackValues($useFallback);

$result = $function();

DataObject\Localizedfield::setGetFallbackValues($backup);

return $result;
}
}