コード例 #1
0
 /**
  * Exchange default admin controller by custom entity admin controller.
  *
  * @param FilterControllerEvent $event
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     $request = $event->getRequest();
     if ('easyadmin' !== $request->attributes->get('_route')) {
         return;
     }
     $currentController = $event->getController();
     // if the controller is defined in a class, $currentController is an array
     // otherwise do nothing because it's a Closure (rare but possible in Symfony)
     if (!is_array($currentController)) {
         return;
     }
     // this condition happens when accessing the backend homepage, which
     // then redirects to the 'list' action of the first configured entity.
     if (null === ($entityName = $request->query->get('entity'))) {
         return;
     }
     $entity = $this->configManager->getEntityConfig($entityName);
     // if the entity doesn't define a custom controller, do nothing
     if (!isset($entity['controller'])) {
         return;
     }
     $customController = $entity['controller'];
     $controllerMethod = $currentController[1];
     // build the full controller name depending on its type
     if (class_exists($customController)) {
         // 'class::method' syntax for normal controllers
         $customController .= '::' . $controllerMethod;
     } else {
         // 'service:method' syntax for controllers as services
         $customController .= ':' . $controllerMethod;
     }
     $request->attributes->set('_controller', $customController);
     $newController = $this->resolver->getController($request);
     if (false === $newController) {
         throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". Check the "controller" configuration of the "%s" entity in your EasyAdmin backend.', $request->getPathInfo(), $entityName));
     }
     $event->setController($newController);
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $entity = $options['entity'];
     $view = $options['view'];
     $entityConfig = $this->configManager->getEntityConfig($entity);
     $entityProperties = $entityConfig[$view]['fields'];
     $formGroups = array();
     $currentFormGroup = null;
     foreach ($entityProperties as $name => $metadata) {
         $formFieldOptions = $metadata['type_options'];
         // Configure options using the list of registered type configurators:
         foreach ($this->configurators as $configurator) {
             if ($configurator->supports($metadata['fieldType'], $formFieldOptions, $metadata)) {
                 $formFieldOptions = $configurator->configure($name, $formFieldOptions, $metadata, $builder);
             }
         }
         $formFieldType = LegacyFormHelper::getType($metadata['fieldType']);
         // if the form field is a special 'group' design element, don't add it
         // to the form. Instead, consider it the current form group (this is
         // applied to the form fields defined after it) and store its details
         // in a property to get them in form template
         if (in_array($formFieldType, array('easyadmin_group', 'JavierEguiluz\\Bundle\\EasyAdminBundle\\Form\\Type\\EasyAdminGroupType'))) {
             $currentFormGroup = $metadata['fieldName'];
             $formGroups[$currentFormGroup] = $metadata;
             continue;
         }
         // 'divider' and 'section' are 'fake' form fields used to create the design
         // elements of the complex form layouts: define them as unmapped and non-required
         if (0 === strpos($metadata['property'], '_easyadmin_form_design_element_')) {
             $formFieldOptions['mapped'] = false;
             $formFieldOptions['required'] = false;
         }
         $formField = $builder->getFormFactory()->createNamedBuilder($name, $formFieldType, null, $formFieldOptions);
         $formField->setAttribute('easyadmin_form_group', $currentFormGroup);
         $builder->add($formField);
     }
     $builder->setAttribute('easyadmin_form_groups', $formGroups);
 }