configuration keys. utm_term has no native field and is left alone. */ public const FIELDS = [ 'utmSource' => 'utm_source', 'utmMedium' => 'utm_medium', 'utmCampaign' => 'utm_campaign', 'utmContent' => 'utm_content', ]; /** Native options carried over to the select. */ private const KEPT_OPTIONS = ['label_attr', 'constraints', 'help', 'help_attr', 'help_html', 'help_translation_parameters']; public function __construct( private readonly UtmConfigurationProvider $configurationProvider, private readonly UtmParameterCatalog $catalog, private readonly TranslatorInterface $translator, ) { } public function apply(FormBuilderInterface $builder): void { // Fields are rebuilt once the stored values are known, to offer legacy values as choices. $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { $this->buildFields($event->getForm(), $event->getData()); }); // A value posted for a removed field would raise an "extra fields" error: drop it. $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void { $data = $event->getData(); if (!is_array($data)) { return; } foreach ($this->getDisabledFields() as $field) { unset($data[$field]); } $event->setData($data); }); // A removed field leaves its key untouched in the stored array: clear it so no stale value survives. $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event): void { $data = $event->getData(); if (null !== $data && !is_array($data)) { return; } $data ??= []; foreach ($this->getDisabledFields() as $field) { // null, as the native empty field: an empty string would add "utm_x=" to every link. $data[$field] = null; } $event->setData($data); }); } /** * @param FormInterface $form */ private function buildFields(FormInterface $form, mixed $storedTags): void { $storedTags = is_array($storedTags) ? $storedTags : []; $nativeOrder = array_keys($form->all()); foreach (self::FIELDS as $field => $key) { if (!$form->has($field)) { continue; } $parameter = $this->configurationProvider->getParameter($key); if (null === $parameter) { continue; } $mode = $this->getMode($parameter); if (UtmParameterMode::DISABLED === $mode) { $form->remove($field); continue; } $label = $this->getCustomLabel($key, $parameter); if (UtmParameterMode::DROPDOWN === $mode) { $this->addDropdown($form, $field, $key, $label, $storedTags[$field] ?? null); } elseif (null !== $label) { $this->replaceField($form, $field, null, ['label' => $label]); } } // A replaced field is appended last: re-append every field in core's order. foreach ($nativeOrder as $name) { if ($form->has($name)) { $child = $form->get($name); $form->remove($name); $form->add($child); } } } /** * @param FormInterface $form */ private function addDropdown(FormInterface $form, string $field, string $key, ?string $label, mixed $stored): void { // A non-scalar value can't be shown in a select: keep the native field rather than lose it. if (null !== $stored && !is_scalar($stored)) { return; } // value => label, in configured order. Numeric values become int keys: compare as strings. $labels = []; foreach ($this->configurationProvider->getDropdownValues($key) as $row) { $labels[(string) $row['value']] = (string) $row['label']; } $options = []; $storedValue = null === $stored ? '' : (string) $stored; if ('' !== $storedValue && !array_key_exists($storedValue, $labels)) { // Legacy value: kept as the last choice so it displays and re-saves without data loss. $labels[$storedValue] = $this->translator->trans('mt3s.smart_links.form.legacy_choice', ['%value%' => $storedValue]); $options['help'] = 'mt3s.smart_links.form.legacy_warning'; $options['help_attr'] = ['class' => 'text-warning']; } elseif ([] === $labels) { $options['help'] = 'mt3s.smart_links.form.no_values'; $options['help_attr'] = ['class' => 'text-warning']; } $options += [ 'choices' => array_map('strval', array_keys($labels)), 'choice_label' => static fn (string $value): string => $labels[$value], // Configured labels are shown as typed. The placeholder is translated here for the same reason. 'choice_translation_domain' => false, 'placeholder' => $this->translator->trans('mt3s.smart_links.form.choose'), 'required' => false, 'multiple' => false, 'expanded' => false, 'attr' => ['class' => 'form-control'], 'invalid_message' => 'mt3s.smart_links.form.error.not_in_taxonomy', ]; if (null !== $label) { $options['label'] = $label; } $this->replaceField($form, $field, ChoiceType::class, $options); } /** * Re-adds a field with its native options (label, constraints...) overridden by $options. * * @param FormInterface $form * @param class-string|null $type null keeps the native type and all native options * @param array $options */ private function replaceField(FormInterface $form, string $field, ?string $type, array $options): void { $config = $form->get($field)->getConfig(); $native = $config->getOptions(); if (null === $type) { $type = $config->getType()->getInnerType()::class; $options = array_replace($this->getOverridableOptions($native), $options); } else { $kept = array_intersect_key($native, array_flip(self::KEPT_OPTIONS)); $kept += ['label' => $native['label'] ?? null]; // Our help (legacy warning, empty list) wins over the native one; otherwise keep the native help. $options = array_replace($kept, $options); } $form->remove($field); $form->add($field, $type, $options); } /** * Native options a type accepts again when re-added (resolved-only options are excluded). * * @param array $native * * @return array */ private function getOverridableOptions(array $native): array { return array_intersect_key($native, array_flip(['label', 'label_attr', 'attr', 'required', 'constraints', 'help', 'help_attr', 'help_html'])); } /** * @param array $parameter */ private function getMode(array $parameter): string { return $parameter['enabled'] ? (string) $parameter['mode'] : UtmParameterMode::DISABLED; } /** * The configured label, or null when it is the catalog default (the native label is kept then). * * @param array $parameter */ private function getCustomLabel(string $key, array $parameter): ?string { $label = (string) ($parameter['label'] ?? ''); $definition = $this->catalog->get($key); if ('' === $label || (null !== $definition && $label === $this->translator->trans($definition->getLabelKey()))) { return null; } return $label; } /** * @return list */ private function getDisabledFields(): array { $disabled = []; foreach (self::FIELDS as $field => $key) { $parameter = $this->configurationProvider->getParameter($key); if (null !== $parameter && UtmParameterMode::DISABLED === $this->getMode($parameter)) { $disabled[] = $field; } } return $disabled; } }