/** * Overrides the base getFormSpecification() to additionally iterate through each * field/association in the metadata and trigger the associated event. * * This allows building of a form from metadata instead of requiring annotations. * Annotations are still allowed through the ElementAnnotationsListener. * * {@inheritDoc} */ public function getFormSpecification($entity) { $formSpec = parent::getFormSpecification($entity); $metadata = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity); $inputFilter = $formSpec['input_filter']; $formElements = array('DoctrineModule\\Form\\Element\\ObjectSelect', 'DoctrineModule\\Form\\Element\\ObjectMultiCheckbox', 'DoctrineModule\\Form\\Element\\ObjectRadio'); foreach ($formSpec['elements'] as $key => $elementSpec) { $name = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null; $isFormElement = isset($elementSpec['spec']['type']) && in_array($elementSpec['spec']['type'], $formElements); if (!$name) { continue; } if (!isset($inputFilter[$name])) { $inputFilter[$name] = new \ArrayObject(); } $params = array('metadata' => $metadata, 'name' => $name, 'elementSpec' => $elementSpec, 'inputSpec' => $inputFilter[$name]); if ($this->checkForExcludeElementFromMetadata($metadata, $name)) { $elementSpec = $formSpec['elements']; unset($elementSpec[$key]); $formSpec['elements'] = $elementSpec; if (isset($inputFilter[$name])) { unset($inputFilter[$name]); } $formSpec['input_filter'] = $inputFilter; continue; } if ($metadata->hasField($name) || !$metadata->hasAssociation($name) && $isFormElement) { $this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params); } elseif ($metadata->hasAssociation($name)) { $this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params); } } $formSpec['options'] = array('prefer_form_input_filter' => true); return $formSpec; }
/** * Overrides the base getFormSpecification() to additionally iterate through each * field/association in the metadata and trigger the associated event. * * This allows building of a form from metadata instead of requiring annotations. * Annotations are still allowed through the ElementAnnotationsListener. * * {@inheritDoc} */ public function getFormSpecification($entity) { $formSpec = parent::getFormSpecification($entity); $metadata = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity); $inputFilter = $formSpec['input_filter']; foreach ($formSpec['elements'] as $key => $elementSpec) { $name = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null; if (!$name) { continue; } if (!isset($inputFilter[$name])) { $inputFilter[$name] = new \ArrayObject(); } $params = array('metadata' => $metadata, 'name' => $name, 'elementSpec' => $elementSpec, 'inputSpec' => $inputFilter[$name]); if ($this->checkForExcludeElementFromMetadata($metadata, $name)) { $elementSpec = $formSpec['elements']; unset($elementSpec[$key]); $formSpec['elements'] = $elementSpec; if (isset($inputFilter[$name])) { unset($inputFilter[$name]); } $formSpec['input_filter'] = $inputFilter; continue; } if ($metadata->hasField($name)) { $this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params); } elseif ($metadata->hasAssociation($name)) { $this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params); } } return $formSpec; }
/** * Note: * Only save to / load from cache if it is the specified entity. * Otherwise strange stuff will happen when using fieldsets and collections. * * {@inheritDoc} */ public function getFormSpecification($entity) { $config = $this->getConfiguration(); $cache = $config->getCache(); $cacheKey = $config->getCacheKey(); // Pre event $this->triggerEvent(FormEvent::EVENT_FORM_SPECIFICATIONS_PRE, $this, array('cacheKey' => $cacheKey)); // Load form specs from cache: if ($config->isCacheableEntity($entity) && $cache->hasItem($cacheKey)) { $formSpec = $cache->getItem($cacheKey); // Parse form specs: } else { $formSpec = parent::getFormSpecification($entity); // Save cache: if ($config->isCacheableEntity($entity)) { try { $cache->setItem($cacheKey, $formSpec); } catch (\Exception $e) { // Silent fail } } } // Post event $this->triggerEvent(FormEvent::EVENT_FORM_SPECIFICATIONS_POST, $this, array('cacheKey' => $cacheKey, 'formSpec' => $formSpec)); return $formSpec; }
/** * Primerio oter todos os atributos da classe de forma automática; * Depois, obter todos atributos que possuem annotações que serão utilizadas; * Estes atributos serão gerados na grid. * * @param \Dataware\Entity\Grid $grid */ private function makeGridColumnsByEntity(Grid $grid) { if (strlen($grid->getEntity()) > 0) { $annotationBuilder = new AnnotationBuilder(); $formEspecification = $annotationBuilder->getFormSpecification($grid->getEntity()); foreach ($formEspecification['elements'] as $element) { if (strlen($element['spec']['options']['label']) > 0) { // É possível a partir do tipo, conseguir descobrir o alinhamento dos registros. $gridColumn = new GridColumn($element['spec']['name'], $element['spec']['options']['label']); $grid->addColumn($gridColumn); } } } }
/** * {@inheritDoc} */ public function getFormSpecification($entity) { $formSpec = null; if ($cache = $this->getCacheStorage()) { // getting cache key from entity name $cacheKey = $this->getCacheKey($entity); // get the cached form, try cache first $formSpec = $cache->getItem($cacheKey); } if (!$formSpec) { $formSpec = AnnotationBuilder::getFormSpecification($entity); // save form to cache if ($cache) { $cache->addItem($cacheKey, $formSpec); } } return $formSpec; }
public function testCanRetrieveOnlyFormSpecification() { $entity = new TestAsset\Annotation\ComplexEntity(); $builder = new Annotation\AnnotationBuilder(); $spec = $builder->getFormSpecification($entity); $this->assertInstanceOf('ArrayObject', $spec); }