Custom Data Node Builder
Learn how to create custom data node builder.
Element Field Definition Definition
TBD
Configuration
# config/packages/opendxp_content_crafter.yaml
services:
App\ContentCrafter\DataType\NodeBuilder\SpecialObjectFieldDefinitionNode:
tags:
- { name: content_crafter.data_type.node_builder }
Service
TBD
Standalone Definition
TBD
Configuration
# config/packages/opendxp_content_crafter.yaml
services:
App\ContentCrafter\DataType\NodeBuilder\MyCustomNode:
tags:
- { name: content_crafter.data_type.node_builder }
Service
<?php
namespace App\ContentCrafter\DataType\NodeBuilder;
use OpenDxp\Bundle\ContentCrafterBundle\Model\Contract;
use OpenDxp\Bundle\ContentCrafterBundle\Model\DataTypeNode;
use OpenDxp\Bundle\ContentCrafterBundle\Service\AIEngine\Response\Image;
use OpenDxp\Bundle\ContentCrafterBundle\Service\AIEngine\Response\ResponseTypeInterface;
use OpenDxp\Bundle\ContentCrafterBundle\Service\DataType\NodeBuilder\StandaloneAwareInterface;
use OpenDxp\Model\DataObject;
use OpenDxp\Model\Element\ElementInterface;
class MyCustomNode implements StandaloneAwareInterface
{
public function supportsDataType(string $dataTypeProvider, mixed $dataTypeSource): bool
{
return $dataTypeSource instanceof DataObject\ClassDefinition;
}
public function buildStandaloneDefinitionNode(string $dataTypeProvider, mixed $dataTypeSource): DataTypeNode
{
return new DataTypeNode(
'app_my_custom_node',
'App: My Custom Node',
'input',
true,
null
);
}
public function supportsWithResponseType(DataTypeNode $dataTypeNode, ResponseTypeInterface $responseType): bool
{
return !$responseType instanceof Image;
}
public function hasOutdatedData(mixed $element, string $dataWriteBehavior, ?string $locale, DataTypeNode $dataTypeNode, array $context): bool
{
if (!$element instanceof ElementInterface) {
return true;
}
if ($dataWriteBehavior === Contract::DATA_WRITE_BEHAVIOR_OVERWRITE) {
return true;
}
$arguments = [];
$getter = 'setMyCustomValue';
if ($locale !== null) {
$arguments[] = $locale;
}
if (!method_exists($element, $getter)) {
throw new \InvalidArgumentException(
sprintf(
'Cannot check for outdated data via "%s" to %s. Method "%s" does not exist',
$dataTypeNode->getIdentifier(),
get_class($element),
$getter
)
);
}
$existingValue = $element->$getter(...$arguments);
return in_array($existingValue, [null, ''], true);
}
public function assignResponseTypeData(mixed $element, ?string $locale, DataTypeNode $dataTypeNode, ResponseTypeInterface $responseType, array $context): void
{
if (!$element instanceof ElementInterface) {
return;
}
$arguments = [];
$getter = 'setMyCustomValue';
$value = $responseType->getResponse();
$arguments[] = $value;
if ($locale !== null) {
$arguments[] = $locale;
}
if (!method_exists($element, $setter)) {
throw new \InvalidArgumentException(
sprintf(
'Cannot assign "%s" to %s. Method "%s" does not exist',
$dataTypeNode->getIdentifier(),
get_class($element),
$setter
)
);
}
// @todo: set text node
$element->$setter(...$arguments);
}
}