/** * @throws \RuntimeException * @param \Symfony\Component\Form\FormBuilder $formBuider * @param $elementId * @return \Symfony\Component\Form\FormBuilder */ public function getChildFormBuilder(FormBuilder $formBuider, $elementId) { // todo : warning this introduce a bug if the field name = 'field_name', // add a check to field will always be 'fieldName' $elements = explode('_', $elementId); // always remove the first element : form's name array_shift($elements); while ($elementName = array_shift($elements)) { if (!$formBuider->has($elementName)) { throw new \RuntimeException(sprintf('The element `%s` does not exists', $elementName)); } $formBuider = $formBuider->get($elementName); } return $formBuider; }
protected function contentFormClb($action, \NyroDev\NyroCmsBundle\Model\Content $row, \Symfony\Component\Form\FormBuilder $form) { $langs = $this->get('nyrocms')->getLocaleNames($row); $defaultLocale = $this->get('nyrocms')->getDefaultLocale($row); unset($langs[$defaultLocale]); $this->translations = array(); foreach ($row->getTranslations() as $tr) { if (!isset($this->translations[$tr->getLocale()])) { $this->translations[$tr->getLocale()] = array(); } $this->translations[$tr->getLocale()][$tr->getField()] = $tr; } /* @var $form \Ivory\OrderedForm\Builder\OrderedFormBuilder */ $propertyAccess = PropertyAccess::createPropertyAccessor(); foreach ($langs as $lg => $lang) { foreach ($this->contentTranslationFields as $field => $options) { if ($form->has($field)) { $type = $options['type']; unset($options['type']); $fieldName = 'lang_' . $lg . '_' . $field; if (isset($options['required']) && $options['required']) { $options['constraints'] = array(new Constraints\NotBlank()); } $form->add($fieldName, $type, array_merge($options, array('label' => $this->trans('admin.content.' . $field) . ' ' . strtoupper($lg), 'mapped' => false, 'data' => isset($this->translations[$lg]) && isset($this->translations[$lg][$field]) ? $this->translations[$lg][$field]->getContent() : $propertyAccess->getValue($row, $field), 'position' => array('after' => $field)))); } } } $adminFormEvent = new AdminFormEvent($action, $row, $form); $this->get('event_dispatcher')->dispatch(AdminFormEvent::UPDATE_CONTENT, $adminFormEvent); }