Skip to content

Commit

Permalink
[Bug] Mutations to tables in Bricks (#875)
Browse files Browse the repository at this point in the history
* 863 tables inside bricks

* Apply php-cs-fixer changes

* 863 Refined approach

* Apply php-cs-fixer changes

---------

Co-authored-by: mcop1 <[email protected]>
  • Loading branch information
mcop1 and mcop1 authored Aug 22, 2024
1 parent d0893cd commit d79d72d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 27 deletions.
2 changes: 0 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,6 @@ public function __clone(): void
/**
* @internal
*
* @param ?User $user
*
* @return bool
*/
public function isAllowed(string $type, ?User $user = null)
Expand Down
15 changes: 13 additions & 2 deletions src/GraphQL/DataObjectInputProcessor/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ public function __construct(array $nodeDef, array $processors)
public function process($object, $newValue, $args, $context, ResolveInfo $info)
{
$attribute = $this->getAttribute();
$getter = 'get' . ucfirst($attribute);
$currentTable = $object->$getter();
$objectBrickParts = Service::parseObjectBrickFieldName($attribute);

if(empty($objectBrickParts)) {
$getter = 'get' . ucfirst($attribute);
$currentTable = $object->$getter();
} else {
$currentTable = Service::getValueFromObjectBrick(
$object,
$objectBrickParts['brickType'],
$objectBrickParts['brickKey'],
$objectBrickParts['brickDescriptor'] ?? null
);
}

Service::setValue($object, $attribute, function ($container, $setter) use ($newValue, $currentTable) {
$newTable = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Table extends Base
public function getGraphQlMutationFieldConfig($nodeDef, $class, $container = null, $params = [])
{
$fieldName = $nodeDef['attributes']['attribute'];
$tableDef = $class->getFieldDefinition($fieldName);
$tableDef = $this->getGraphQlService()->getObjectFieldHelper()->getFieldDefinitionFromKey($class, $fieldName);
$inputItems = [];
$numCols = 0;

Expand Down
92 changes: 70 additions & 22 deletions src/GraphQL/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Pimcore\Model\Factory;
use Pimcore\Translation\Translator;
use Psr\Container\ContainerInterface;
use stdClass;

class Service
{
Expand Down Expand Up @@ -782,7 +783,7 @@ public function getPropertyTypeDefinition($typeName)
* @param string|null $brickKey
* @param Data|null $fieldDefinition
*
* @return \stdclass, value and objectid where the value comes from
* @return stdclass, value and objectid where the value comes from
*/
public static function getValueForObject($object, $key, $brickType = null, $brickKey = null, $fieldDefinition = null, $context = [], $brickDescriptor = null, $args = [])
{
Expand Down Expand Up @@ -837,7 +838,7 @@ public static function getValueForObject($object, $key, $brickType = null, $bric
* @param string $attribute
* @param \Closure $callback
*
* @return \stdclass|null
* @return stdclass|null
*
* @throws \Exception
*/
Expand Down Expand Up @@ -976,31 +977,18 @@ public static function resolveValue(BaseDescriptor $descriptor, Data $fieldDefin
$blockData = call_user_func_array([$itemData, $blockGetter], $descriptorData['args'] ?? []);
}
} elseif (isset($descriptorData['__brickType']) && $descriptorData['__brickType']) {
$context = ['object' => $object];
$brickDescriptor = $descriptorData['__brickDescriptor'] ?? null;

$brickType = $descriptorData['__brickType'];
$brickKey = $descriptorData['__brickKey'];

$key = \Pimcore\Model\DataObject\Service::getFieldForBrickType($object->getclass(), $brickType);

$brickClass = Definition::getByKey($brickType);

if (!$brickClass) {
return null;
}

$context['outerFieldname'] = $key;

$def = $brickClass->getFieldDefinition($brickKey, $context);

if (!$def) {
return null;
}

if (!empty($key)) {
$blockData = self::getValueForObject($object, $key, $brickType, $brickKey, $def, $context, $brickDescriptor, $descriptorData['args'] ?? []);
}
return self::getValueFromObjectBrick(
$object,
$brickType,
$brickKey,
$brickDescriptor,
$descriptorData
);
} else {
$blockGetter = 'get'.ucfirst($descriptorData['__blockName']);
$isLocalizedField = self::isLocalizedField($container, $fieldDefinition->getName());
Expand Down Expand Up @@ -1169,4 +1157,64 @@ public function querySchemaEnabled(string $type)

return $enabled;
}

public static function getValueFromObjectBrick(
Concrete $object,
string $brickType,
string $brickKey,
string $brickDescriptor = null,
array $descriptorData = [],
): stdClass|array|null {
$context = ['object' => $object];

$key = \Pimcore\Model\DataObject\Service::getFieldForBrickType($object->getclass(), $brickType);
$brickClass = Definition::getByKey($brickType);
if (!$brickClass) {
return null;
}

$context['outerFieldname'] = $key;
$def = $brickClass->getFieldDefinition($brickKey, $context);
if (!$def) {
return null;
}

if (!empty($key)) {
return self::getValueForObject(
$object,
$key,
$brickType,
$brickKey,
$def,
$context,
$brickDescriptor,
$descriptorData['args'] ?? []);
}

return null;
}

public static function parseObjectBrickFieldName(
string $fieldName
): array {
$parts = explode('~', $fieldName);
if (count($parts) > 1) {
[$brickType, $brickKey] = $parts;
$brickDescriptor = null;

if (strpos($brickType, '?') !== false) {
$brickDescriptor = substr($brickType, 1);
$brickDescriptor = json_decode($brickDescriptor, true);
$brickType = $brickDescriptor['containerKey'];
}

return [
'brickType' => $brickType,
'brickKey' => $brickKey,
'brickDescriptor' => $brickDescriptor,
];
}

return [];
}
}

0 comments on commit d79d72d

Please sign in to comment.