|
3 | 3 | namespace Codete\FormGeneratorBundle; |
4 | 4 |
|
5 | 5 | use Codete\FormGeneratorBundle\Annotations\Display; |
| 6 | +use Codete\FormGeneratorBundle\Annotations\Field; |
6 | 7 | use Codete\FormGeneratorBundle\Annotations\Form; |
7 | 8 | use Codete\FormGeneratorBundle\Form\Type\EmbedType; |
8 | 9 | use Doctrine\Common\Annotations\AnnotationReader; |
@@ -80,31 +81,60 @@ private function getFieldsConfiguration($model, $fields = []) |
80 | 81 | $properties = $ro->getProperties(); |
81 | 82 | } else { |
82 | 83 | foreach (array_keys($fields) as $field) { |
| 84 | + // setting the configuration to null guarantees the order of elements in there |
| 85 | + // to match the order specified in $fields |
| 86 | + $configuration[$field] = null; |
| 87 | + if (! $ro->hasProperty($field)) { |
| 88 | + continue; // most prob a class-level field, order will be maintained due to trick above |
| 89 | + } |
83 | 90 | $properties[] = $ro->getProperty($field); |
84 | 91 | } |
85 | 92 | } |
| 93 | + $fieldConfigurations = []; |
| 94 | + // first are coming properties |
86 | 95 | foreach ($properties as $property) { |
87 | | - $propertyIsListed = array_key_exists($property->getName(), $fields); |
| 96 | + $propertyName = $property->getName(); |
| 97 | + $propertyIsListed = array_key_exists($propertyName, $fields); |
88 | 98 | if (!empty($fields) && !$propertyIsListed) { |
89 | | - continue; |
| 99 | + continue; // list of fields was specified and current one is not there |
90 | 100 | } |
91 | 101 | $fieldConfiguration = $this->annotationReader->getPropertyAnnotation($property, Display::class); |
92 | 102 | if ($fieldConfiguration === null && !$propertyIsListed) { |
93 | 103 | continue; |
94 | 104 | } |
95 | | - $configuration[$property->getName()] = (array)$fieldConfiguration; |
96 | | - if (isset($fields[$property->getName()])) { |
97 | | - $configuration[$property->getName()] = array_replace_recursive($configuration[$property->getName()], $fields[$property->getName()]); |
| 105 | + $fieldConfigurations[$propertyName] = $fieldConfiguration; |
| 106 | + } |
| 107 | + // later are coming class-level fields. We need to iterate through all annotations as there's no method |
| 108 | + // to get *all* occurrences of chosen annotation. |
| 109 | + foreach ($this->annotationReader->getClassAnnotations($ro) as $annotation) { |
| 110 | + if (! $annotation instanceof Display) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + $propertyName = $annotation->value; |
| 114 | + if (!empty($fields) && !array_key_exists($propertyName, $fields)) { |
| 115 | + continue; // list of fields was specified and current one is not there |
| 116 | + } |
| 117 | + // @todo this was a mistake originally, need to drop default required at all in 2.0 |
| 118 | + unset($annotation->required); |
| 119 | + $fieldConfigurations[$propertyName] = $annotation; |
| 120 | + } |
| 121 | + foreach ($fieldConfigurations as $propertyName => $fieldConfiguration) { |
| 122 | + if ($fieldConfiguration instanceof Display && ! $fieldConfiguration instanceof Field) { |
| 123 | + @trigger_error("Display annotation has been deprecated in 1.3 and will be removed in 2.0 - please use Field instead.", E_USER_DEPRECATED); |
| 124 | + } |
| 125 | + $configuration[$propertyName] = (array)$fieldConfiguration; |
| 126 | + if (isset($fields[$propertyName])) { |
| 127 | + $configuration[$propertyName] = array_replace_recursive($configuration[$propertyName], $fields[$propertyName]); |
98 | 128 | } |
99 | | - if ($configuration[$property->getName()]['type'] === EmbedType::TYPE) { |
100 | | - if (($value = $property->getValue($model)) === null) { |
101 | | - $value = $this->instantiator->instantiate($configuration[$property->getName()]['class']); |
| 129 | + if ($configuration[$propertyName]['type'] === EmbedType::TYPE) { |
| 130 | + if (! $ro->hasProperty($propertyName) || ($value = $ro->getProperty($propertyName)->getValue($model)) === null) { |
| 131 | + $value = $this->instantiator->instantiate($configuration[$propertyName]['class']); |
102 | 132 | } |
103 | | - $configuration[$property->getName()]['data_class'] = $configuration[$property->getName()]['class']; |
104 | | - $configuration[$property->getName()]['model'] = $value; |
| 133 | + $configuration[$propertyName]['data_class'] = $configuration[$propertyName]['class']; |
| 134 | + $configuration[$propertyName]['model'] = $value; |
105 | 135 | } |
106 | 136 | // this variable comes from Doctrine\Common\Annotations\Annotation |
107 | | - unset($configuration[$property->getName()]['value']); |
| 137 | + unset($configuration[$propertyName]['value']); |
108 | 138 | } |
109 | 139 | return $configuration; |
110 | 140 | } |
|
0 commit comments