Skip to content

Commit

Permalink
Add dynamic field type Hidden (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler authored Apr 19, 2024
1 parent b3c3bd4 commit ed1520a
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
46 changes: 46 additions & 0 deletions Dynamic/Types/HiddenType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Dynamic\Types;

use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeConfiguration;
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeInterface;
use Sulu\Bundle\FormBundle\Entity\FormField;
use Symfony\Component\Form\Extension\Core\Type\HiddenType as TypeHiddenType;
use Symfony\Component\Form\FormBuilderInterface;

/**
* The Hidden form field type.
*/
class HiddenType implements FormFieldTypeInterface
{
use SimpleTypeTrait;

/**
* {@inheritdoc}
*/
public function getConfiguration(): FormFieldTypeConfiguration
{
return new FormFieldTypeConfiguration(
'sulu_form.type.hidden',
__DIR__ . '/../../Resources/config/form-fields/field_hidden.xml'
);
}

/**
* {@inheritdoc}
*/
public function build(FormBuilderInterface $builder, FormField $field, string $locale, array $options): void
{
$type = TypeHiddenType::class;
$builder->add($field->getKey(), $type, $options);
}
}
1 change: 1 addition & 0 deletions Entity/Dynamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Dynamic implements AuditableInterface
'headline',
'freeText',
'recaptcha',
'hidden',
];

/**
Expand Down
23 changes: 23 additions & 0 deletions Resources/config/form-fields/field_hidden.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" ?>
<properties xmlns="http://schemas.sulu.io/template/template"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/properties-1.0.xsd">
<property name="title" type="text_editor" mandatory="true">
<meta>
<title>sulu_form.title</title>
</meta>
</property>

<property name="shortTitle" type="text_line" colspan="6">
<meta>
<title>sulu_form.short_title</title>
<info_text>sulu_form.short_title_description</info_text>
</meta>
</property>

<property name="defaultValue" type="text_line" colspan="6">
<meta>
<title>sulu_form.default</title>
</meta>
</property>
</properties>
4 changes: 4 additions & 0 deletions Resources/config/types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,9 @@
<service id="sulu_form.dynamic.type_attachment" class="Sulu\Bundle\FormBundle\Dynamic\Types\AttachmentType">
<tag name="sulu_form.dynamic.type" alias="attachment"/>
</service>

<service id="sulu_form.dynamic.type_hidden" class="Sulu\Bundle\FormBundle\Dynamic\Types\HiddenType">
<tag name="sulu_form.dynamic.type" alias="hidden"/>
</service>
</services>
</container>
1 change: 1 addition & 0 deletions Resources/translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"sulu_form.type.radiobuttons": "Radio Buttons",
"sulu_form.type.recaptcha": "Captcha",
"sulu_form.type.mailchimp": "Mailchimp",
"sulu_form.type.hidden": "Hidden",
"sulu_form.type.sendinblue": "Sendinblue",
"sulu_form.width.full": "Voll",
"sulu_form.width.half": "Halb",
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"sulu_form.type.radiobuttons": "Radio Buttons",
"sulu_form.type.recaptcha": "Captcha",
"sulu_form.type.mailchimp": "Mailchimp",
"sulu_form.type.hidden": "Hidden",
"sulu_form.type.sendinblue": "Sendinblue",
"sulu_form.width.full": "Full",
"sulu_form.width.half": "Half",
Expand Down
10 changes: 6 additions & 4 deletions Tests/Functional/Metadata/DynamicFormMetadataLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testGetMetadataEnglish(): void

$fields = $formFields->getItems()['fields'];
$this->assertInstanceOf(FieldMetadata::class, $fields);
$this->assertCount(27, $fields->getTypes());
$this->assertCount(28, $fields->getTypes());
$this->assertEquals('fields', $fields->getName());
$this->assertEquals('block', $fields->getType());
$this->assertEquals('attachment', $fields->getDefaultType());
Expand All @@ -67,6 +67,7 @@ public function testGetMetadataEnglish(): void
'freeText',
'function',
'headline',
'hidden',
'lastName',
'textarea',
'phone',
Expand Down Expand Up @@ -106,7 +107,7 @@ public function testGetMetadataGerman(): void

$fields = $formFields->getItems()['fields'];
$this->assertInstanceOf(FieldMetadata::class, $fields);
$this->assertCount(27, $fields->getTypes());
$this->assertCount(28, $fields->getTypes());
$this->assertEquals('fields', $fields->getName());
$this->assertEquals('block', $fields->getType());
$this->assertEquals('attachment', $fields->getDefaultType());
Expand All @@ -126,6 +127,7 @@ public function testGetMetadataGerman(): void
'company',
'freeText',
'function',
'hidden',
'country',
'spacer',
'textarea',
Expand Down Expand Up @@ -184,7 +186,7 @@ public function testGetMetadataAttachmentEnglish(): void

$fields = $formFields->getItems()['fields'];
$this->assertInstanceOf(FieldMetadata::class, $fields);
$this->assertCount(27, $fields->getTypes());
$this->assertCount(28, $fields->getTypes());

$attachment = $fields->getTypes()['attachment'];
$this->assertInstanceOf(FormMetadata::class, $attachment);
Expand Down Expand Up @@ -253,7 +255,7 @@ public function testGetMetadataAttachmentGerman(): void

$fields = $formFields->getItems()['fields'];
$this->assertInstanceOf(FieldMetadata::class, $fields);
$this->assertCount(27, $fields->getTypes());
$this->assertCount(28, $fields->getTypes());

$attachment = $fields->getTypes()['attachment'];
$this->assertInstanceOf(FormMetadata::class, $attachment);
Expand Down

0 comments on commit ed1520a

Please sign in to comment.