Skip to content

Commit

Permalink
Expose field type constant
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed May 30, 2020
1 parent 960a2ab commit 146df61
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 121 deletions.
31 changes: 31 additions & 0 deletions UPGRADE-3.x.md
@@ -1,6 +1,28 @@
UPGRADE 3.x
===========

## Added constant for show/list field types and deprecated some field types.

You can now `TemplateRegistry` constants, like
```
$showMapper->add('foo', TemplateRegistry::TYPE_STRING)
```
Instead of using directly a string value
```
$showMapper->add('foo', 'string')
```

The list of available types can be found in [the documentation](docs/reference/field_types.rst).

In the same way, we deprecated the following types:
- `text`: prefer using `string` or `TemplateRegistry::TYPE_STRING`
- `decimal`: prefer using `float` or `TemplateRegistry::TYPE_FLOAT`
- `smallint`: prefer using `integer` or `TemplateRegistry::TYPE_INTEGER`
- `bigint`: prefer using `integer` or `TemplateRegistry::TYPE_INTEGER`

UPGRADE FROM 3.66 to 3.67
=========================

## Deprecated accessing to a non existing value when adding field to `showMapper` and `listMapper`.

Before:
Expand All @@ -20,10 +42,16 @@ In the next major an exception will be thrown if no getter/isser/hasser is found
of the time the error is coming from a typo, this will allow the developer to catch it as fast as possible.
Currently this will only trigger a deprecation if the field value is not found.

UPGRADE FROM 3.65 to 3.66
=========================

## Deprecated not passing a `Sonata\AdminBundle\Admin\AdminHelper` instance to `Sonata\AdminBundle\Form\Type\AdminType::__construct()`

When instantiating a `Sonata\AdminBundle\Form\Type\AdminType` object, please use the 1 parameter signature `($adminHelper)`.

UPGRADE FROM 3.63 to 3.64
=========================

## Deprecated not setting as `false` the configuration option `sonata_admin.options.legacy_twig_text_extension`

This option controls which Twig text extension will be used to provide filters like
Expand Down Expand Up @@ -69,6 +97,9 @@ $showMapper
;
```

UPGRADE FROM 3.59 to 3.60
=========================

## Deprecated not setting "sonata.admin.manager" tag in model manager services

If you are using [autoconfiguration](https://symfony.com/doc/4.4/service_container.html#the-autoconfigure-option),
Expand Down
12 changes: 6 additions & 6 deletions docs/reference/action_list.rst
Expand Up @@ -51,15 +51,15 @@ Here is an example::

// you may specify the field type directly as the
// second argument instead of in the options
->add('isVariation', 'boolean')
->add('isVariation', TemplateRegistry::TYPE_BOOLEAN)

// if null, the type will be guessed
->add('enabled', null, [
'editable' => true
])

// editable association field
->add('status', 'choice', [
->add('status', TemplateRegistry::TYPE_CHOICE, [
'editable' => true,
'class' => 'Vendor\ExampleBundle\Entity\ExampleStatus',
'choices' => [
Expand All @@ -70,7 +70,7 @@ Here is an example::
])

// editable multiple field
->add('winner', 'choice', [
->add('winner', TemplateRegistry::TYPE_CHOICE, [
'editable' => true,
'multiple' => true,
'choices' => [
Expand All @@ -81,7 +81,7 @@ Here is an example::
])

// we can add options to the field depending on the type
->add('price', 'currency', [
->add('price', TemplateRegistry::TYPE_CURRENCY, [
'currency' => $this->currencyDetector->getCurrency()->getLabel()
])

Expand Down Expand Up @@ -555,10 +555,10 @@ Example::
'header_style' => 'width: 5%; text-align: center',
'row_align' => 'center'
])
->add('name', 'text', [
->add('name', TemplateRegistry::TYPE_STRING, [
'header_style' => 'width: 35%'
])
->add('description', 'text', [
->add('description', TemplateRegistry::TYPE_STRING, [
'header_style' => 'width: 35%',
'collapse' => true
])
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/action_show.rst
Expand Up @@ -116,9 +116,9 @@ The following is a working example of a ShowAction::
// The boolean option is actually very cool
// true shows a check mark and the 'yes' label
// false shows a check mark and the 'no' label
->add('dateCafe', 'boolean')
->add('datePub', 'boolean')
->add('dateClub', 'boolean')
->add('dateCafe', TemplateRegistry::TYPE_BOOLEAN)
->add('datePub', TemplateRegistry::TYPE_BOOLEAN)
->add('dateClub', TemplateRegistry::TYPE_BOOLEAN)
;

}
Expand Down
100 changes: 51 additions & 49 deletions docs/reference/field_types.rst
Expand Up @@ -6,24 +6,29 @@ List and Show Actions

There are many field types that can be used in the list and show action :

============ =============================================
Fieldtype Description
============ =============================================
array display value from an array
boolean display a green or red picture dependant on the boolean value
date display a formatted date. Accepts an optional ``format`` parameter
datetime display a formatted date and time. Accepts an optional ``format`` and ``timezone`` parameter
text display a text
textarea display a textarea
trans translate the value with a provided ``catalogue`` (translation domain) and ``format`` (sprintf format) option
string display a text
number display a number
currency display a number with a provided ``currency`` option
percent display a percentage
choice uses the given value as index for the ``choices`` array and displays (and optionally translates) the matching value
url display a link
html display (and optionally truncate or strip tags from) raw html
============ =============================================
======================================= =============================================
Fieldtype Description
======================================= =============================================
``TemplateRegistry::TYPE_ARRAY`` display value from an array
``TemplateRegistry::TYPE_BOOLEAN`` display a green or red picture dependant on the boolean value
``TemplateRegistry::TYPE_DATE`` display a formatted date. Accepts an optional ``format`` parameter
``TemplateRegistry::TYPE_TIME`` display a formatted time. Accepts an optional ``format`` and ``timezone`` parameter
``TemplateRegistry::TYPE_DATETIME`` display a formatted date and time. Accepts an optional ``format`` and ``timezone`` parameter
``TemplateRegistry::TYPE_STRING`` display a text
``TemplateRegistry::TYPE_EMAIL`` display a mailto link
``TemplateRegistry::TYPE_TEXTAREA`` display a textarea
``TemplateRegistry::TYPE_TRANS`` translate the value with a provided ``catalogue`` (translation domain) and ``format`` (sprintf format) option
``TemplateRegistry::TYPE_FLOAT`` display a number
``TemplateRegistry::TYPE_CURRENCY`` display a number with a provided ``currency`` option
``TemplateRegistry::TYPE_PERCENT`` display a percentage
``TemplateRegistry::TYPE_CHOICE`` uses the given value as index for the ``choices`` array and displays (and optionally translates) the matching value
``TemplateRegistry::TYPE_URL`` display a link
``TemplateRegistry::TYPE_HTML`` display (and optionally truncate or strip tags from) raw html
``TemplateRegistry::TYPE_MANY_TO_MANY`` used for relational tables
``TemplateRegistry::TYPE_MANY_TO_ONE`` used for relational tables
``TemplateRegistry::TYPE_ONE_TO_MANY`` used for relational tables
``TemplateRegistry::TYPE_ONE_TO_ONE`` used for relational tables
======================================= =============================================

Theses types accept an ``editable`` parameter to edit the value from within the list action.
This is currently limited to scalar types (text, integer, url...) and choice types with association field.
Expand All @@ -36,10 +41,10 @@ This is currently limited to scalar types (text, integer, url...) and choice typ
Option for currency type must be an official ISO code, example : EUR for "euros".
List of ISO codes : `http://en.wikipedia.org/wiki/List_of_circulating_currencies <http://en.wikipedia.org/wiki/List_of_circulating_currencies>`_

In ``date`` and ``datetime`` field types, ``format`` pattern must match twig's
In ``TemplateRegistry::TYPE_DATE``, ``TemplateRegistry::TYPE_TIME`` and ``TemplateRegistry::TYPE_DATETIME`` field types, ``format`` pattern must match twig's
``date`` filter specification, available at: `http://twig.sensiolabs.org/doc/filters/date.html <http://twig.sensiolabs.org/doc/filters/date.html>`_

In ``datetime`` field types, ``timezone`` syntax must match twig's
In ``TemplateRegistry::TYPE_TIME`` and ``TemplateRegistry::TYPE_DATETIME`` field types, ``timezone`` syntax must match twig's
``date`` filter specification, available at: `http://twig.sensiolabs.org/doc/filters/date.html <http://twig.sensiolabs.org/doc/filters/date.html>`_
and php timezone list: `https://php.net/manual/en/timezones.php <https://php.net/manual/en/timezones.php>`_
You can use in lists what `view-timezone <http://symfony.com/doc/current/reference/forms/types/datetime.html#view-timezone>`_ allows on forms,
Expand All @@ -57,11 +62,8 @@ This is currently limited to scalar types (text, integer, url...) and choice typ
;
}

More types might be provided based on the persistency layer defined. Please refer to their
related documentations.

Array
^^^^^^
``TemplateRegistry::TYPE_ARRAY``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can use the following parameters:

Expand Down Expand Up @@ -97,7 +99,7 @@ Parameter Description
protected function configureListFields(ListMapper $listMapper): void
{
$listMapper
->add('options', 'array', [
->add('options', TemplateRegistry::TYPE_ARRAY, [
'inline' => true,
'display' => 'both',
'key_translation_domain' => true,
Expand All @@ -106,8 +108,8 @@ Parameter Description
;
}
Choice
^^^^^^
``TemplateRegistry::TYPE_CHOICE``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can use the following parameters:

Expand All @@ -126,7 +128,7 @@ Parameter Description
{
// For the value `prog`, the displayed text is `In progress`. The `App` catalogue will be used to translate `In progress` message.
$listMapper
->add('status', 'choice', [
->add('status', TemplateRegistry::TYPE_CHOICE, [
'choices' => [
'prep' => 'Prepared',
'prog' => 'In progress',
Expand All @@ -137,13 +139,13 @@ Parameter Description
;
}
The ``choice`` field type also supports multiple values that can be separated by a ``delimiter``::
The ``TemplateRegistry::TYPE_CHOICE`` field type also supports multiple values that can be separated by a ``delimiter``::

protected function configureListFields(ListMapper $listMapper)
{
// For the value `['r', 'b']`, the displayed text ist `red | blue`.
$listMapper
->add('colors', 'choice', [
->add('colors', TemplateRegistry::TYPE_CHOICE, [
'multiple' => true,
'delimiter' => ' | ',
'choices' => [
Expand All @@ -159,8 +161,8 @@ The ``choice`` field type also supports multiple values that can be separated by

The default delimiter is a comma ``,``.

URL
^^^
``TemplateRegistry::TYPE_URL``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Display URL link to external website or controller action.

Expand All @@ -185,29 +187,29 @@ Parameter Description
$listMapper
// Output for value `http://example.com`:
// `<a href="http://example.com">http://example.com</a>`
->add('targetUrl', 'url')
->add('targetUrl', TemplateRegistry::TYPE_URL)
// Output for value `http://example.com`:
// `<a href="http://example.com" target="_blank">example.com</a>`
->add('targetUrl', 'url', [
->add('targetUrl', TemplateRegistry::TYPE_URL, [
'attributes' => ['target' => '_blank']
])
// Output for value `http://example.com`:
// `<a href="http://example.com">example.com</a>`
->add('targetUrl', 'url', [
->add('targetUrl', TemplateRegistry::TYPE_URL, [
'hide_protocol' => true
])
// Output for value `Homepage of example.com` :
// `<a href="http://example.com">Homepage of example.com</a>`
->add('title', 'url', [
->add('title', TemplateRegistry::TYPE_URL, [
'url' => 'http://example.com'
])
// Output for value `Acme Blog Homepage`:
// `<a href="http://blog.example.com">Acme Blog Homepage</a>`
->add('title', 'url', [
->add('title', TemplateRegistry::TYPE_URL, [
'route' => [
'name' => 'acme_blog_homepage',
'absolute' => true
Expand All @@ -216,7 +218,7 @@ Parameter Description
// Output for value `Sonata is great!` (related object has identifier `123`):
// `<a href="http://blog.example.com/xml/123">Sonata is great!</a>`
->add('title', 'url', [
->add('title', TemplateRegistry::TYPE_URL, [
'route' => [
'name' => 'acme_blog_article',
'absolute' => true,
Expand All @@ -229,10 +231,10 @@ Parameter Description
.. note::

Do not use ``url`` type with ``addIdentifier()`` method, because it will create invalid nested URLs.
Do not use ``TemplateRegistry::TYPE_URL`` type with ``addIdentifier()`` method, because it will create invalid nested URLs.

HTML
^^^^
``TemplateRegistry::TYPE_HTML``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Display (and optionally truncate or strip tags from) raw html.

Expand All @@ -256,47 +258,47 @@ Parameter Description
// Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
// `<p><strong>Creating a Template for the Field</strong> and form</p>` (no escaping is done)
->add('content', 'html')
->add('content', TemplateRegistry::TYPE_HTML)
// Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
// `Creating a Template for the Fi...`
->add('content', 'html', [
->add('content', TemplateRegistry::TYPE_HTML, [
'strip' => true
])
// Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
// `Creating a Template for...`
->add('content', 'html', [
->add('content', TemplateRegistry::TYPE_HTML, [
'truncate' => true
])
// Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
// `Creating a...`
->add('content', 'html', [
->add('content', TemplateRegistry::TYPE_HTML, [
'truncate' => [
'length' => 10
]
])
// Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
// `Creating a Template for the Field...`
->add('content', 'html', [
->add('content', TemplateRegistry::TYPE_HTML, [
'truncate' => [
'cut' => false
]
])
// Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
// `Creating a Template for the Fi, etc.`
->add('content', 'html', [
->add('content', TemplateRegistry::TYPE_HTML, [
'truncate' => [
'ellipsis' => ', etc.'
]
])
// Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
// `Creating a Template for***`
->add('content', 'html', [
->add('content', TemplateRegistry::TYPE_HTML, [
'truncate' => [
'length' => 20,
'cut' => false,
Expand Down

0 comments on commit 146df61

Please sign in to comment.