diff --git a/Form/Type/CampaignConditionFieldValueType.php b/Form/Type/CampaignConditionFieldValueType.php index 93f18ffb7..06d02d4cb 100644 --- a/Form/Type/CampaignConditionFieldValueType.php +++ b/Form/Type/CampaignConditionFieldValueType.php @@ -46,10 +46,17 @@ public function __construct( */ public function buildForm(FormBuilderInterface $builder, array $options): void { - $fields = $this->customFieldModel->fetchCustomFieldsForObject($options['customObject']); - $choices = []; + $fields = $this->customFieldModel->fetchCustomFieldsForObject($options['customObject']); + $choices = []; + $optionAttr = []; + foreach ($fields as $field) { - $choices[$field->getLabel()] = $field->getId(); + $choices[$field->getLabel()] = $field->getId(); + $optionAttr[$field->getLabel()] = [ + 'data-operators' => json_encode($field->getTypeObject()->getOperatorOptions()), + 'data-options' => json_encode($field->getChoices()), + 'data-field-type' => $field->getType(), + ]; } $builder->add( @@ -62,16 +69,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'attr' => [ 'class' => 'form-control', ], - 'choice_attr' => array_map( - function ($field) { - return [ - 'data-operators' => json_encode($field->getTypeObject()->getOperatorOptions()), - 'data-options' => json_encode($field->getChoices()), - 'data-field-type' => $field->getType(), - ]; - }, - $fields - ), + 'choice_attr' => $optionAttr, ] ); diff --git a/Tests/Functional/EventListener/CampaignConditionTest.php b/Tests/Functional/EventListener/CampaignConditionTest.php index 39ae8e511..3db5f5d36 100644 --- a/Tests/Functional/EventListener/CampaignConditionTest.php +++ b/Tests/Functional/EventListener/CampaignConditionTest.php @@ -67,4 +67,35 @@ public function testConditionForm(): void Assert::assertSame('=', $body['event']['properties']['properties']['operator']); Assert::assertSame('unicorn', $body['event']['properties']['properties']['value']); } + + public function testVerifyDataOperatorAttrIsAvailableForFields(): void + { + $customObject = $this->createCustomObjectWithAllFields(self::$container, 'Campaign test object'); + $crawler = $this->client->request( + Request::METHOD_GET, + 's/campaigns/events/new', + [ + 'campaignId' => 'mautic_041b2a401f680fb0b644654af5ba0892f31f0697', + 'type' => "custom_item.{$customObject->getId()}.fieldvalue", + 'eventType' => 'condition', + 'anchor' => 'leadsource', + 'anchorEventType' => 'source', + ], + [], + $this->createAjaxHeaders() + ); + + Assert::assertTrue($this->client->getResponse()->isOk(), $this->client->getResponse()->getContent()); + + $html = json_decode($this->client->getResponse()->getContent(), true)['newContent']; + + $crawler->addHtmlContent($html); + + $options = $crawler->filter('#campaignevent_properties_field')->filter('option'); // ->attr('data-operators'); + + /** @var \DOMElement $option */ + foreach ($options as $option) { + Assert::assertNotEmpty($option->getAttribute('data-operators')); + } + } } diff --git a/Tests/Unit/Form/Type/CampaignConditionFieldValueTypeTest.php b/Tests/Unit/Form/Type/CampaignConditionFieldValueTypeTest.php index c8df0e30e..d45d81ac0 100644 --- a/Tests/Unit/Form/Type/CampaignConditionFieldValueTypeTest.php +++ b/Tests/Unit/Form/Type/CampaignConditionFieldValueTypeTest.php @@ -102,7 +102,7 @@ public function testBuildForm(): void 'class' => 'form-control', ], 'choice_attr' => [ - 42 => [ + 'Cheese' => [ 'data-operators' => '{"=":"a","!=":"b"}', 'data-options' => '[]', 'data-field-type' => 'int',