Skip to content

Commit

Permalink
feature #225 Improve field configuration parsing (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch (closes #225).

Discussion
----------

Improve field configuration parsing

This fixes #220.

The main two changes introduced:

  * Previously, the nice field configuration options were only applied when you configured the view fields explicitly. Now we apply those defaults also when you don't configure anything. An example of the issues that this change solves: if you don't configure the boolean fields, they are displayed as dull badges; if you configure them, they are displayed as cool flip switches. Now the behavior is the same in both cases: flip switches (unless you configure them as booleans).
  * Previously the documentation said that you could use Doctrine types as the value of the `type` field option. This is wrong. The `type` option only admits Symfony Form types (and the two custom `toggle` and `image` types defined by EasyAdmin).

Commits
-------

daa73d3 Improve field configuration parsing
  • Loading branch information
javiereguiluz committed Apr 15, 2015
2 parents 3c280e1 + daa73d3 commit e76dc74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 58 deletions.
44 changes: 20 additions & 24 deletions Configuration/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,11 @@ private function processEntityPropertiesMetadata(ClassMetadata $entityMetadata)
*/
private function getFieldsForListView(array $entityConfiguration)
{
// there is a custom configuration for 'list' fields
if (count($entityConfiguration['list']['fields']) > 0) {
return $this->normalizeFieldsConfiguration('list', $entityConfiguration);
if (0 === count($entityConfiguration['list']['fields'])) {
$entityConfiguration['list']['fields'] = $this->filterListFieldsBasedOnSmartGuesses($this->defaultEntityFields);
}

return $this->filterListFieldsBasedOnSmartGuesses($this->defaultEntityFields);
return $this->normalizeFieldsConfiguration('list', $entityConfiguration);
}

/**
Expand All @@ -179,12 +178,11 @@ private function getFieldsForListView(array $entityConfiguration)
*/
private function getFieldsForShowView(array $entityConfiguration)
{
// there is a custom configuration for 'show' fields
if (count($entityConfiguration['show']['fields']) > 0) {
return $this->normalizeFieldsConfiguration('show', $entityConfiguration);
if (0 === count($entityConfiguration['show']['fields'])) {
$entityConfiguration['show']['fields'] = $this->defaultEntityFields;
}

return $this->defaultEntityFields;
return $this->normalizeFieldsConfiguration('show', $entityConfiguration);
}

/**
Expand All @@ -198,18 +196,13 @@ private function getFieldsForShowView(array $entityConfiguration)
*/
protected function getFieldsForFormBasedViews($view, array $entityConfiguration)
{
$entityFields = array();

// there is a custom field configuration for this view
if (count($entityConfiguration[$view]['fields']) > 0) {
$entityFields = $this->normalizeFieldsConfiguration($view, $entityConfiguration);
} else {
if (0 === count($entityConfiguration[$view]['fields'])) {
$excludedFieldNames = array($entityConfiguration['primary_key_field_name']);
$excludedFieldTypes = array('binary', 'blob', 'json_array', 'object');
$entityFields = $this->filterFieldsByNameAndType($this->defaultEntityFields, $excludedFieldNames, $excludedFieldTypes);
$entityConfiguration[$view]['fields'] = $this->filterFieldsByNameAndType($this->defaultEntityFields, $excludedFieldNames, $excludedFieldTypes);
}

return $entityFields;
return $this->normalizeFieldsConfiguration($view, $entityConfiguration);
}

/**
Expand Down Expand Up @@ -323,8 +316,11 @@ private function normalizeFieldsConfiguration($view, $entityConfiguration)
{
$configuration = array();
$fieldsConfiguration = $entityConfiguration[$view]['fields'];
$originalViewConfiguration = $this->backendConfig['entities'][$entityConfiguration['name']][$view];

foreach ($fieldsConfiguration as $fieldName => $fieldConfiguration) {
$originalFieldConfiguration = isset($originalViewConfiguration['fields'][$fieldName]) ? $originalViewConfiguration['fields'][$fieldName] : null;

if (!array_key_exists($fieldName, $entityConfiguration['properties'])) {
// treat this field as 'virtual' because it doesn't exist as a
// property of the related Doctrine entity
Expand Down Expand Up @@ -361,19 +357,19 @@ private function normalizeFieldsConfiguration($view, $entityConfiguration)
// for the field, use it as 'fieldType'. Otherwise, infer the best field
// type using the property data type.
if (in_array($view, array('edit', 'new'))) {
if (isset($fieldConfiguration['type'])) {
$normalizedConfiguration['fieldType'] = $fieldConfiguration['type'];
} else {
$normalizedConfiguration['fieldType'] = $this->getFormTypeFromDoctrineType($normalizedConfiguration['type']);
}
$normalizedConfiguration['fieldType'] = isset($originalFieldConfiguration['type'])
? $originalFieldConfiguration['type']
: $this->getFormTypeFromDoctrineType($normalizedConfiguration['type']);
}

// special case for the 'list' view: 'boolean' properties are displayed
// as toggleable flip switches when certain conditions are met
if ('list' === $view && 'boolean' === $normalizedConfiguration['dataType']) {
// conditions: 1) the end-user hasn't configured the field type explicitly
// 2) the 'edit' action is allowed for the 'list' view of this entity
if (!isset($fieldConfiguration['type']) && array_key_exists('edit', $entityConfiguration['list']['actions'])) {
// conditions:
// 1) the end-user hasn't configured the field type explicitly
// 2) the 'edit' action is enabled for the 'list' view of this entity
$isEditActionEnabled = array_key_exists('edit', $entityConfiguration['list']['actions']);
if (!isset($originalFieldConfiguration['type']) && $isEditActionEnabled) {
$normalizedConfiguration['dataType'] = 'toggle';
}
}
Expand Down
26 changes: 0 additions & 26 deletions Resources/doc/4-customizing-list-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,34 +481,8 @@ easy_admin:
These are the supported types:

* All the [Symfony Form types](http://symfony.com/doc/current/reference/forms/types.html)
* All the [Doctrine data types](http://doctrine-dbal.readthedocs.org/en/latest/reference/types.html)
* Custom EasyAdmin types:
* `image`, displays images inlined in the entity listings. Read the
previous sections for more details.
* `toggle`, displays a boolean value as a flip switch. Read the previous
sections for more details.

If you use any of the types defined by Doctrine, the backend automatically
transforms into the appropriate Symfony Form type using the following
conversion table:

| Doctrine Type | Symfony form type
| -------------- | -----------------
| `array` | `collection`
| `bigint` | `text`
| `blob` | `textarea`
| `boolean` | `checkbox`
| `date` | `date`
| `datetime` | `datetime`
| `datetimetz` | `datetime`
| `decimal` | `number`
| `float` | `number`
| `guid` | `text`
| `integer` | `integer`
| `json_array` | `textarea`
| `object` | `textarea`
| `simple_array` | `collection`
| `smallint` | `integer`
| `string` | `text`
| `text` | `textarea`
| `time` | `time`
12 changes: 4 additions & 8 deletions Resources/doc/5-customizing-show-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ easy_admin:

These are the supported types:

* All the Doctrine data types:
* Dates: `date`, `datetime`, `datetimetz`, `time`
* Logical: `boolean`
* Arrays: `array`, `simple_array`
* Text: `string`, `text`
* Numeric: `bigint`, `integer`, `smallint`, `decimal`, `float`
* `image`, custom type defined by EasyAdmin which displays images inlined in
the entity show page. Read the previous sections for more details.
* All the [Symfony Form types](http://symfony.com/doc/current/reference/forms/types.html)
* Custom EasyAdmin types:
* `image`, displays images inlined in the entity listings. Read the
previous sections for more details.

0 comments on commit e76dc74

Please sign in to comment.