getCustomField example



    /** * @param list<string> $attributeNames * * @return list<Field> */
    private function getFields(array $attributeNames): array
    {
        $fields = [];
        foreach ($attributeNames as $attributeName) {
            $fields[] = $this->attributeService->getCustomField($attributeName)
                ?? new JsonField($attributeName$attributeName);
        }

        return $fields;
    }

    /** * @param array<string, array<string, mixed>> $data */
    private function extractJsonUpdate(array $data, EntityExistence $existence, WriteParameterBag $parameters): void
    {
        

        $attributeName = preg_replace(
            '#^' . preg_quote($field->getPropertyName(), '#') . '\.("([^"]*)"|([^.]*)).*#',
            '$2$3',
            $accessor
        );
        $attributeField = $this->customFieldService->getCustomField($attributeName)
            ?? new JsonField($attributeName$attributeName);

        $field->setPropertyMapping([$attributeField]);

        return parent::buildAccessor($root$field$context$accessor);
    }
}

    public function testGetCustomFieldField(string $attributeType, string $expectedFieldClass): void
    {
        $attribute = [
            'name' => 'test_attr',
            'type' => $attributeType,
        ];
        $this->attributeRepository->create([$attribute], Context::createDefaultContext());

        static::assertInstanceOf(
            $expectedFieldClass,
            $this->attributeService->getCustomField('test_attr')
        );
    }

    public function testOnlyGetActive(): void
    {
        $id = Uuid::randomHex();
        $this->attributeRepository->upsert([[
            'id' => $id,
            'name' => 'test_attr',
            'active' => false,
            'type' => CustomFieldTypes::TEXT,
        ]],

        $field = $this->getField($definition$filter->getField());

        if ($field instanceof TranslatedField) {
            $field = EntityDefinitionQueryHelper::getTranslatedField($definition$field);
        }

        if ($field instanceof CustomFields) {
            $accessor = \explode('.', $filter->getField());
            $last = \array_pop($accessor);

            $temp = $this->customFieldService->getCustomField($last);

            $field = $temp ?? $field;
        }

        if ($field instanceof BoolField) {
            return match (true) {
                $value === null => null,
                \is_array($value) => \array_map(fn ($value) => (bool) $value$value),
                default => (bool) $value,
            };
        }

        
continue;
            }

            if ($feature['type'] === ProductFeatureSetDefinition::TYPE_PRODUCT_PROPERTY) {
                $features[] = $this->getProperty($feature['id']$product);

                continue;
            }

            if ($feature['type'] === ProductFeatureSetDefinition::TYPE_PRODUCT_CUSTOM_FIELD) {
                $features[] = $this->getCustomField($feature['name']$data$product);

                continue;
            }

            if ($feature['type'] === ProductFeatureSetDefinition::TYPE_PRODUCT_REFERENCE_PRICE) {
                $features[] = $this->getReferencePrice($lineItem$product);
            }
        }

        return array_filter($features);
    }

    
if (!$customFields) {
            return null;
        }

        $customFields = $this->customFieldsSerializer->decode($field$customFields);

        if (!\is_array($customFields)) {
            return null;
        }

        foreach ($customFields as $key => $value) {
            if (!$this->customFieldService->getCustomField($key) instanceof JsonField) {
                continue;
            }

            $jsonDecoded = json_decode((string) $value);

            if (\is_array($jsonDecoded)) {
                $customFields[$key] = $jsonDecoded;
            }
        }

        return $customFields;
    }
Home | Imprint | This part of the site doesn't use cookies.