Skip to content

Commit

Permalink
[Bug]: GraphQl - query for asset meta data returns an exception (#816)
Browse files Browse the repository at this point in the history
* Fix: Add preResolveAssetMetadataEvents

* Apply php-cs-fixer changes

* Update src/GraphQL/Resolver/AssetType.php

Co-authored-by: JiaJia Ji <[email protected]>

---------

Co-authored-by: robertSt7 <[email protected]>
Co-authored-by: JiaJia Ji <[email protected]>
  • Loading branch information
3 people authored Nov 13, 2023
1 parent ea10a08 commit 2530a31
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
26 changes: 26 additions & 0 deletions src/Event/GraphQL/AssetMetadataEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\DataHubBundle\Event\GraphQL;

final class AssetMetadataEvents
{
/**
* @Event("Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\AssetEvent")
*
* @var string
*/
const PRE_RESOLVE = 'pimcore.datahub.graphql.asset.metadata.preResolve';
}
64 changes: 38 additions & 26 deletions src/GraphQL/Resolver/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

use Exception;
use GraphQL\Type\Definition\ResolveInfo;
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\AssetMetadataEvents;
use Pimcore\Bundle\DataHubBundle\GraphQL\ElementDescriptor;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ElementTagTrait;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
use Pimcore\Bundle\DataHubBundle\WorkspaceHelper;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Model\Asset;
use Symfony\Component\EventDispatcher\EventDispatcher;

class AssetType
{
Expand Down Expand Up @@ -64,39 +67,48 @@ public function resolveTag($value = null, $args = [], $context = [], ResolveInfo
public function resolveMetadata($value = null, $args = [], $context = [], ResolveInfo $resolveInfo = null)
{
$asset = $this->getAssetFromValue($value, $context);
$metadata = $asset?->getMetadata(raw: true);
if (!$metadata) {
return null;
}

if ($asset) {
$metadata = $asset->getObjectVar('metadata');
if ($metadata) {
if (isset($args['ignore_language']) && $args['ignore_language']) {
return $metadata;
}
//Relational data would return as a comma separated list. each element type and id by `_`, eg. "object_154,asset_489"
$event = new AssetEvent($asset, [
'metadata' => $metadata,
'context' => $context
]);
/** @var EventDispatcher $eventDispatcher */
$eventDispatcher = \Pimcore::getContainer()->get('event_dispatcher');
$eventDispatcher->dispatch($event, AssetMetadataEvents::PRE_RESOLVE);
$metadata = $event->getArgument('metadata');

if (isset($args['ignore_language']) && $args['ignore_language']) {
return $metadata;
}

$map = [];
$keys = [];
$language = isset($args['language']) ? $args['language'] : $this->getGraphQlService()->getLocaleService()->findLocale();
$map = [];
$keys = [];
$language = $args['language'] ?? $this->getGraphQlService()->getLocaleService()->findLocale();

foreach ($metadata as $item) {
$keys[$item['name']] = 1;
$l = $item['language'] ? $item['language'] : 'default';
$map[$l][$item['name']] = $item;
}
$result = [];

foreach ($keys as $key => $found) {
if (isset($map[$language][$key])) {
$result[] = $map[$language][$key];
} elseif (isset($map['default'][$key])) {
$result[] = $map['default'][$key];
}
}
foreach ($metadata as $item) {
$keys[$item['name']] = 1;
$l = $item['language'] ?: 'default';
$map[$l][$item['name']] = $item;
}
$result = [];

if ($result) {
return $result;
}
foreach ($keys as $key => $found) {
if (isset($map[$language][$key])) {
$result[] = $map[$language][$key];
} elseif (isset($map['default'][$key])) {
$result[] = $map['default'][$key];
}
}

if ($result) {
return $result;
}

return null;
}

Expand Down

0 comments on commit 2530a31

Please sign in to comment.