public function buildForm(AdminInterface $admin, FormMapper $formMapper) { /** * Bind data to the mapped fields */ $formMapper->getFormBuilder()->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $formEvent) { $data = $formEvent->getData(); $form = $formEvent->getForm(); if ($data instanceof Route) { $form->get('source')->setData($data->getPath()); $form->get('target')->setData($data->getDefault('path')); } }); /** * Bind the mapped fields to the entity */ $formMapper->getFormBuilder()->addEventListener(FormEvents::SUBMIT, function (FormEvent $formEvent) { $data = $formEvent->getData(); $form = $formEvent->getForm(); if ($data instanceof Route) { $data->setStaticPrefix($form->get('source')->getData()); $data->setDefault('path', $form->get('target')->getData()); $data->setDefault('_controller', $this->routeController); $data->setDefault('permanent', true); } }); $formMapper->add('source', 'text', ['mapped' => false]); $formMapper->add('target', 'text', ['mapped' => false]); $formMapper->remove('staticPrefix'); $formMapper->remove('variablePattern'); }
public function testRemoveCascadeRemoveFieldFromFormGroup() { $this->formMapper->with('foo'); $this->formMapper->remove('foo'); }
/** * @param FormMapper $formMapper */ protected function configureFormFields(FormMapper $formMapper) { // Verify if document is not child $isParent = $this->getParentFieldDescription() === null; $hasRouteChild = method_exists($this->getClass(), 'getRouteChild'); $adminClassname = $this->getClassName(); $notMenuAdmin = $adminClassname != 'MenuAdmin'; if ($isParent && $hasRouteChild && $notMenuAdmin) { $formMapper->tab('Configuration')->with('Add to Menu', array('collapsed' => true, 'class' => 'col-md-4', 'box_class' => 'box box-solid box-danger', 'description' => 'Add to Menu'))->end()->with('Document Father', array('collapsed' => true, 'class' => 'col-md-4', 'box_class' => 'box box-solid box-danger', 'description' => 'Parent/Name'))->add('parentDocument', 'doctrine_phpcr_odm_tree', array('root_node' => $this->documentRootPath, 'choice_list' => array(), 'select_root_node' => true, 'required' => false))->add('name', 'text', array('required' => false))->end()->with('Route Father', array('collapsed' => true, 'class' => 'col-md-4', 'box_class' => 'box box-solid box-danger', 'description' => 'Route/URL'))->add('routeChild', 'doctrine_phpcr_odm_tree', array('root_node' => $this->routesRootPath, 'choice_list' => array(), 'select_root_node' => true, 'label' => 'Select Route', 'required' => false))->end()->end(); } elseif ($isParent && $notMenuAdmin) { $formMapper->tab('Configuration')->with('Document Father', array('collapsed' => true, 'class' => 'col-md-4', 'box_class' => 'box box-solid box-danger', 'description' => 'Parent/Name'))->add('parentDocument', 'doctrine_phpcr_odm_tree', array('root_node' => $this->documentRootPath, 'choice_list' => array(), 'select_root_node' => true, 'required' => false))->add('name', 'text', array('required' => false))->end()->end(); } // Configuration, Content and Helpers properties $formMapper->tab('Content')->with('Content')->add('title', 'text', array('required' => true))->add('subtitle', 'text', array('required' => false))->add('resume', 'text', array('required' => false))->add('body', 'ckeditor', $this->getCkeditorOptions())->end()->end()->setHelps(array('title' => 'seven_manager.admin.fields.title.helper', 'subtitle' => 'seven_manager.admin.fields.subtitle.helper', 'name' => 'seven_manager.admin.fields.name.helper', 'content' => 'seven_manager.admin.fields.content.helper')); // "Globalize" this $smEventListener = function (FormEvent $event) use($formMapper) { $isParent = $this->getParentFieldDescription() === null; // Get form $formData = $event->getForm(); $classImplements = class_implements($this->getClass()); // To be use to check blockInterface, or not // Verify if is a Block or a Parent Document if (!$isParent) { // Load groups and tabs $formTabs = $this->getFormTabs(); if (!empty($formTabs['default']['groups'])) { // Find tabs and groups $hasPublishable = array_search('form.group_publish_workflow', $formTabs['default']['groups']); $hasLocale = array_search('form.group_general', $formTabs['default']['groups']); // TODO: Create handler remove by form tab or form field all components !!! DRY if (true) { if ($hasPublishable !== false) { $formGroup = $this->getFormGroups(); $formTabs = $this->getFormTabs(); unset($formGroup['form.group_publish_workflow']); unset($formTabs['default']['groups'][$hasPublishable]); $this->setFormGroups($formGroup); $this->setFormTabs($formTabs); } if ($hasLocale !== false) { $formGroup = $this->getFormGroups(); $formTabs = $this->getFormTabs(); unset($formGroup['form.group_general']); unset($formTabs['default']['groups'][$hasLocale]); $this->setFormGroups($formGroup); $this->setFormTabs($formTabs); } // Remove locale only if this admin is child if ($formData->has('locale')) { $formData->remove('locale'); $formMapper->remove('locale'); } // Remove undesired fields if ($formData->has('publish_start_date')) { $formData->remove('publish_start_date')->remove('publish_end_date')->remove('publishable'); $formMapper->remove('publish_start_date')->remove('publish_end_date')->remove('publishable'); } // If used as child remove locale, locale is set by default // no need extra field in this case } } else { // At the end of the future handler remove empty groups to avoid empty tabs // Using get array_values and verify each group $formTabs = $this->getFormTabs(); if (empty($formTabs['default']['groups'])) { unset($formTabs['default']); $this->setFormTabs($formTabs); } } } }; // Add event listener $formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SET_DATA, $smEventListener); }