/** * Finds the values of the given entity which match the query provided. * * @param string $entity * @param string $query * @param int $page * * @return array */ public function find($entity, $query, $page = 1) { if (empty($entity) || empty($query)) { return array('results' => array()); } $backendConfig = $this->configManager->getBackendConfig(); if (!isset($backendConfig['entities'][$entity])) { throw new \InvalidArgumentException(sprintf('The "entity" argument must contain the name of an entity managed by EasyAdmin ("%s" given).', $entity)); } $paginator = $this->finder->findByAllProperties($backendConfig['entities'][$entity], $query, $page, $backendConfig['show']['max_results']); return array('results' => $this->processResults($paginator->getCurrentPageResults(), $backendConfig['entities'][$entity])); }
public function isActionEnabled($entityName, $view, $action) { if (!$this->container->get('security')->hasRole(strtoupper("ROLE_{$entityName}_{$action}"))) { return false; } return parent::isActionEnabled($entityName, $view, $action); }
/** * 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); }
/** * {@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); }
/** * Finds the values of the given entity which match the query provided. * * @param string $entity * @param string $property * @param string $view * @param string $query * @param int $page * * @return array */ public function find($entity, $property, $view, $query, $page = 1) { if (empty($entity) || empty($property) || empty($view) || empty($query)) { return array('results' => array()); } $backendConfig = $this->configManager->getBackendConfig(); if (!isset($backendConfig['entities'][$entity])) { throw new \InvalidArgumentException(sprintf('The "entity" argument must contain the name of an entity managed by EasyAdmin ("%s" given).', $entity)); } if (!isset($backendConfig['entities'][$entity][$view]['fields'][$property])) { throw new \InvalidArgumentException(sprintf('The "property" argument must contain the name of a property configured in the "%s" view of the "%s" entity ("%s" given).', $view, $entity, $property)); } if (!isset($backendConfig['entities'][$entity][$view]['fields'][$property]['targetEntity'])) { throw new \InvalidArgumentException(sprintf('The "%s" property configured in the "%s" view of the "%s" entity can\'t be of type "easyadmin_autocomplete" because it\'s not related to another entity.', $property, $view, $entity)); } $targetEntityClass = $backendConfig['entities'][$entity][$view]['fields'][$property]['targetEntity']; $targetEntityConfig = $this->configManager->getEntityConfigByClass($targetEntityClass); if (null === $targetEntityConfig) { throw new \InvalidArgumentException(sprintf('The configuration of the "%s" entity is not available (this entity is used as the target of the "%s" autocomplete field in the "%s" view of the "%s" entity).', $targetEntityClass, $property, $view, $entity)); } $paginator = $this->finder->findByAllProperties($targetEntityConfig, $query, $page, $backendConfig['show']['max_results']); return array('results' => $this->processResults($paginator->getCurrentPageResults(), $targetEntityConfig)); }