forked from pimcore/data-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Generalize the element loading / sharing by introducing a trait.
- Loading branch information
Showing
6 changed files
with
76 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* 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\GraphQL\Traits; | ||
|
||
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup; | ||
use Pimcore\Model\Element\ElementInterface; | ||
use Pimcore\Model\Element\Service as ElementService; | ||
|
||
trait ElementLoaderTrait | ||
{ | ||
/** | ||
* Stores the "metadata" of a related element into the data storage which | ||
* then is passed around. | ||
* | ||
* It can be used with loadDataElement() which if possible will use the | ||
* already loaded element but will fallback to load the element using | ||
* the id. | ||
* | ||
* @return array | ||
*/ | ||
protected function setDataElement(\ArrayAccess $data, ElementInterface | DefaultMockup $element): \ArrayAccess | ||
{ | ||
$data['id'] = $element->getId(); | ||
$data[ElementInterface::class . '_type'] = $element->getType(); | ||
$data[ElementInterface::class . '_instance'] = $element->getId(); | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* | ||
* @return ElementInterface | ||
*/ | ||
protected function loadDataElement(&$data, $type) | ||
{ | ||
if (!isset($data[ElementInterface::class . '_instance'])) { | ||
$data[ElementInterface::class] = ElementService::getElementById($data['id'], $type); | ||
} | ||
|
||
return $data[ElementInterface::class] ?? null; | ||
} | ||
} |