/** * @throws InvalidConfigurationException */ public function configureTabMenu(AdminInterface $admin, MenuItemInterface $menu, $action, AdminInterface $childAdmin = null) { if (!($subject = $admin->getSubject())) { return; } if (!$subject instanceof RouteReferrersReadInterface && !$subject instanceof Route) { throw new InvalidConfigurationException(sprintf('%s can only be used on subjects which implement Symfony\\Cmf\\Component\\Routing\\RouteReferrersReadInterface or Symfony\\Component\\Routing\\Route.', __CLASS__)); } if ($subject instanceof PrefixInterface && !is_string($subject->getId())) { // we have an unpersisted dynamic route return; } $defaults = array(); if ($subject instanceof TranslatableInterface) { if ($locale = $subject->getLocale()) { $defaults['_locale'] = $locale; } } try { $uri = $this->router->generate($subject, $defaults); } catch (RoutingExceptionInterface $e) { // we have no valid route return; } $menu->addChild($this->translator->trans('admin.menu_frontend_link_caption', array(), 'CmfRoutingBundle'), array('uri' => $uri, 'attributes' => array('class' => 'sonata-admin-menu-item', 'role' => 'menuitem'), 'linkAttributes' => array('class' => 'sonata-admin-frontend-link', 'role' => 'button', 'target' => '_blank', 'title' => $this->translator->trans('admin.menu_frontend_link_title', array(), 'CmfRoutingBundle')))); }
/** * Add a new instance * * @param AdminInterface $admin * @param string $elementId * * @throws \Exception */ public function addNewInstance(AdminInterface $admin, $elementId) { $entity = $admin->getSubject(); $propertyAccessor = new PropertyAccessor(); $collection = $propertyAccessor->getValue($entity, $elementId); if ($collection instanceof \Doctrine\Common\Collections\Collection || $collection instanceof \Doctrine\ORM\PersistentCollection || $collection instanceof \Doctrine\ODM\MongoDB\PersistentCollection) { $instance = $this->getClassInstance($admin, explode('.', preg_replace('#\\[\\d*?\\]#', '', $elementId))); } else { return; } if (!method_exists($collection, 'add')) { return; } $collection->add($instance); $propertyAccessor->setValue($entity, $elementId, $collection); }
/** * Note: * This code is ugly, but there is no better way of doing it. * For now the append form element action used to add a new row works * only for direct FieldDescription (not nested one). * * @throws \RuntimeException * * @param AdminInterface $admin * @param object $subject * @param string $elementId * * @return array */ public function appendFormFieldElement(AdminInterface $admin, $subject, $elementId) { // retrieve the subject $formBuilder = $admin->getFormBuilder(); $form = $formBuilder->getForm(); $form->setData($subject); $form->handleRequest($admin->getRequest()); // get the field element $childFormBuilder = $this->getChildFormBuilder($formBuilder, $elementId); //Child form not found (probably nested one) //if childFormBuilder was not found resulted in fatal error getName() method call on non object if (!$childFormBuilder) { $propertyAccessor = $this->pool->getPropertyAccessor(); $entity = $admin->getSubject(); $path = $this->getElementAccessPath($elementId, $entity); $collection = $propertyAccessor->getValue($entity, $path); if ($collection instanceof \Doctrine\ORM\PersistentCollection || $collection instanceof \Doctrine\ODM\MongoDB\PersistentCollection) { //since doctrine 2.4 $entityClassName = $collection->getTypeClass()->getName(); } elseif ($collection instanceof \Doctrine\Common\Collections\Collection) { $entityClassName = $this->getEntityClassName($admin, explode('.', preg_replace('#\\[\\d*?\\]#', '', $path))); } else { throw new \Exception('unknown collection class'); } $collection->add(new $entityClassName()); $propertyAccessor->setValue($entity, $path, $collection); $fieldDescription = null; } else { // retrieve the FieldDescription $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName()); try { $value = $fieldDescription->getValue($form->getData()); } catch (NoValueException $e) { $value = null; } // retrieve the posted data $data = $admin->getRequest()->get($formBuilder->getName()); if (!isset($data[$childFormBuilder->getName()])) { $data[$childFormBuilder->getName()] = array(); } $objectCount = count($value); $postCount = count($data[$childFormBuilder->getName()]); $fields = array_keys($fieldDescription->getAssociationAdmin()->getFormFieldDescriptions()); // for now, not sure how to do that $value = array(); foreach ($fields as $name) { $value[$name] = ''; } // add new elements to the subject while ($objectCount < $postCount) { // append a new instance into the object $this->addNewInstance($form->getData(), $fieldDescription); ++$objectCount; } $this->addNewInstance($form->getData(), $fieldDescription); } $finalForm = $admin->getFormBuilder()->getForm(); $finalForm->setData($subject); // bind the data $finalForm->setData($form->getData()); return array($fieldDescription, $finalForm); }
/** * returns the locale of the current admin user * * @param \Sonata\AdminBundle\Admin\AdminInterface $admin * @return mixed|string */ public function getCurrentAdminLocale(\Sonata\AdminBundle\Admin\AdminInterface $admin) { $locale = ''; if (!$admin->hasRequest()) { $admin->setRequest($this->getService('request')); } if ($subject = $admin->getSubject()) { return $this->getFieldValue($subject, 'locale'); } elseif ($filter = $admin->getDatagrid()->getFilter('locale')) { /** @var \Sonata\AdminBundle\Filter\Filter $filter */ $data = $filter->getValue(); if (!$data || !is_array($data) || !array_key_exists('value', $data)) { $locale = $this->getCurrentLocale(); } $data['value'] = trim($data['value']); if (strlen($data['value']) > 0) { $locale = $data['value']; } if (!$locale && method_exists($admin, 'getDefaultLocale')) { $locale = $admin->getDefaultLocale(); } } return $locale; }