I have a custom relate field mapper and an entirely custom field mapper that are conflicting somehow and I would like some input on making sure different mappers only run on specific fields. My custom field save mapper and my custom relate save mapper are below:
class CustomTagsSaveApiMapper implements ApiRecordFieldTypeMapperInterface
{
use CustomTagsApiMapperTrait;
public const FIELD_TYPE = 'Customtags';
protected LinkedRecordsProviderInterface $linkedRecordsProvider;
public function __construct(LinkedRecordsProviderInterface $linkedRecordsProvider)
{
$this->linkedRecordsProvider = $linkedRecordsProvider;
}
public function getFieldType(): string
{
return self::FIELD_TYPE;
}
public function getModule(): string
{
return 'default';
}
public function getKey(): string
{
return 'custom-tags-save-api-mapper';
}
public function getModes(): array
{
return ['save'];
}
public function getOrder(): int
{
return 0;
}
public function toInternal(Record $record, FieldDefinition $fieldDefinitions, string $field): void
{
}
public function toExternal(Record $record, FieldDefinition $fieldDefinitions, string $field): void
{
$this->injectRelatedRecords($record, $field, $this->linkedRecordsProvider);
}
}
the custom relate save mapper is
namespace App\Extension\defaultExt\backend\Module\Service\Fields\CustomRelate\SaveHandlers;
use App\Data\Entity\Record;
use App\Data\Service\Record\RecordSaveHandlers\RecordFieldTypeSaveHandlerInterface;
use App\FieldDefinitions\Entity\FieldDefinition;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
#[Autoconfigure(lazy: true)]
class CustomRelateSaveHandler implements RecordFieldTypeSaveHandlerInterface
{
public const FIELD_TYPE = 'relate';
public function getFieldType(): string
{
return self::FIELD_TYPE;
}
public function getModule(): string
{
return 'default';
}
public function getKey(): string
{
return 'custom-relate-save-handler';
}
public function getOrder(): int
{
return 10;
}
public function getModes(): array
{
return ['after-save'];
}
public function getHandlerKey(): string
{
return $this->getKey();
}
public function run(?Record $previousVersion, Record $inputRecord, ?Record $savedRecord, FieldDefinition $fieldDefinitions, string $field): void
{
if ($savedRecord === null) {
return;
}
$vardefs = $fieldDefinitions->getVardef();
$definition = $vardefs[$field] ?? null;
if (!$definition) {
return;
}
$idFieldName = $definition['id_name'] ?? '';
if (empty($idFieldName)) {
return;
}
$attributes = $inputRecord->getAttributes() ?? [];
$relateField = $attributes[$field] ?? null;
if (is_array($relateField) && isset($relateField['id'])) {
// Set the ID field value from the relate field
$savedAttributes = $savedRecord->getAttributes() ?? [];
$savedAttributes[$idFieldName] = $relateField['id'];
$savedRecord->setAttributes($savedAttributes);
} elseif (is_string($relateField) && !empty($relateField)) {
// If the field value is a string (ID), set it directly
$savedAttributes = $savedRecord->getAttributes() ?? [];
$savedAttributes[$idFieldName] = $relateField;
$savedRecord->setAttributes($savedAttributes);
}
}
}
initially I thought maybe key and priority would resolve the issue but the relate save handler is working and the customtags save handler is not. They work when either is not in the system.