Skip to content

Commit

Permalink
chore: Add support for mockup objects in relations.
Browse files Browse the repository at this point in the history
  • Loading branch information
das-peter committed Aug 13, 2024
1 parent 945842d commit 0ef5076
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/GraphQL/DataObjectQueryFieldConfigGenerator/Helper/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
use Pimcore\Bundle\DataHubBundle\WorkspaceHelper;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\Element\AbstractElement;
Expand Down Expand Up @@ -76,9 +77,16 @@ public function resolve($value = null, $args = [], $context = [], ResolveInfo $r
$relations = Service::resolveValue($value, $this->fieldDefinition, $this->attribute, $args);
if ($relations) {
$result = [];
/** @var $relation AbstractElement */
foreach ($relations as $relation) {
if (!WorkspaceHelper::checkPermission($relation, 'read')) {
/** @var $relation AbstractElement */
// Explicitly set the type of the mockup object because
// these don't have the matching class instance for
// auto-detect.
$type = null;
if ($relation instanceof DefaultMockup) {
$type = $relation->getType();
}
if (!WorkspaceHelper::checkPermission($relation, 'read', $type)) {
continue;
}

Expand Down
9 changes: 7 additions & 2 deletions src/GraphQL/ElementDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Pimcore\Bundle\DataHubBundle\GraphQL;

use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Document;
Expand All @@ -25,15 +26,19 @@ class ElementDescriptor extends BaseDescriptor
/**
* @param ElementInterface|null $element
*/
public function __construct(ElementInterface $element = null)
public function __construct(ElementInterface|DefaultMockup $element = null)
{
parent::__construct();
if ($element) {
$this->offsetSet('id', $element->getId());
$this->offsetSet('__elementType', \Pimcore\Model\Element\Service::getElementType($element));
$this->offsetSet('__elementSubtype', $element instanceof Concrete ? $element->getClass()->getName() : $element->getType());

if ($element instanceof Concrete) {
if ($element instanceof DefaultMockup) {
$subtype = $element->getClass()->getName();
$this->offsetSet('__elementType', 'object');
$this->offsetSet('__elementSubtype', $subtype);
} elseif ($element instanceof Concrete) {
$subtype = $element->getClass()->getName();

$this->offsetSet('__elementType', 'object');
Expand Down
9 changes: 7 additions & 2 deletions src/GraphQL/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
use Pimcore\Bundle\DataHubBundle\GraphQL\FieldHelper\DocumentFieldHelper;
use Pimcore\Bundle\DataHubBundle\GraphQL\Query\Operator\Factory\OperatorFactoryInterface;
use Pimcore\Bundle\DataHubBundle\GraphQL\Query\Value\DefaultValue;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementLoaderTrait;
use Pimcore\Bundle\DataHubBundle\PimcoreDataHubBundle;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
use Pimcore\Cache\RuntimeCache;
use Pimcore\DataObject\GridColumnConfig\ConfigElementInterface;
use Pimcore\Localization\LocaleServiceInterface;
Expand All @@ -57,6 +59,8 @@

class Service
{
use ElementLoaderTrait;

/***
* @var ContainerInterface
*/
Expand Down Expand Up @@ -944,8 +948,7 @@ public static function setValue($object, $attribute, $callback)
public static function resolveValue(BaseDescriptor $descriptor, Data $fieldDefinition, $attribute, $args = [])
{
$getter = 'get' . ucfirst($fieldDefinition->getName());
$objectId = $descriptor['id'];
$object = Concrete::getById($objectId);
$object = self::staticLoadDataElement($descriptor, 'object');
if (!$object) {
return null;
}
Expand Down Expand Up @@ -1183,6 +1186,8 @@ public function extractData($data, $target, $args = [], $context = [], ResolveIn
$fieldHelper = $this->getAssetFieldHelper();
} elseif ($target instanceof AbstractObject) {
$fieldHelper = $this->getObjectFieldHelper();
} elseif ($target instanceof DefaultMockup) {
$fieldHelper = $this->getObjectFieldHelper();
}

if ($fieldHelper) {
Expand Down
9 changes: 9 additions & 0 deletions src/GraphQL/Traits/ElementLoaderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ protected function setDataElement($data, ElementInterface | DefaultMockup $eleme
* @return ElementInterface
*/
protected function loadDataElement(&$data, $type)
{
return self::staticLoadDataElement($data, $type);
}

/**
*
* @return ElementInterface
*/
protected static function staticLoadDataElement(&$data, $type)
{
if (!isset($data[ElementInterface::class . '_instance'])) {
$data[ElementInterface::class . '_type'] = $type;
Expand Down

0 comments on commit 0ef5076

Please sign in to comment.