/** * @param ComponentInterface $component * * @return AdminInterface */ protected function getAdmin(ComponentInterface $component, ActionInterface $action = null) { if ($action && ($adminComponent = $action->getComponent('admin_code'))) { return $this->pool->getAdminByAdminCode($adminComponent); } try { return $this->pool->getAdminByClass($component->getModel()); } catch (\RuntimeException $e) { } return false; }
/** * Get the identifiers as a string that is save to use in an url. * * @param object $model * @param AdminInterface $admin * * @return string string representation of the id that is save to use in an url */ public function getUrlsafeIdentifier($model, AdminInterface $admin = null) { if (is_null($admin)) { $admin = $this->pool->getAdminByClass(ClassUtils::getClass($model)); } return $admin->getUrlsafeIdentifier($model); }
public function testGetAdminForClassWhenAdminClassIsSet() { $this->pool->setAdminServiceIds(array('sonata.user.admin.group1')); $this->pool->setAdminClasses(array('someclass' => array('sonata.user.admin.group1'))); $this->assertTrue($this->pool->hasAdminByClass('someclass')); $this->assertSame('adminUserClass', $this->pool->getAdminByClass('someclass')); }
/** * @param string $className * * @return AdminInterface */ private function getAdminByClass($className) { if (!isset($this->admins[$className])) { // will return null if not defined $this->admins[$className] = $this->pool->getAdminByClass($className); } return $this->admins[$className]; }
/** * {@inheritdoc} */ public function enhance(array $data, Resource $resource) { $object = $resource->getPayload(); // sonata has dependency on ClassUtils so this is fine. $class = ClassUtils::getClass($object); if (false === $this->pool->hasAdminByClass($class)) { return $data; } $admin = $this->pool->getAdminByClass($class); $links = array(); $routeCollection = $admin->getRoutes(); foreach ($routeCollection->getElements() as $code => $route) { $routeName = $route->getDefault('_sonata_name'); $url = $this->urlGenerator->generate($routeName, array($admin->getIdParameter() => $admin->getUrlsafeIdentifier($object)), true); $routeRole = substr($code, strlen($admin->getCode()) + 1); $links[$routeRole] = $url; } $data['label'] = $admin->toString($object); $data['sonata_label'] = $admin->getLabel(); $data['sonata_links'] = $links; return $data; }
/** * {@inheritdoc} */ public function enhance(Description $description) { $object = $description->getResource()->getPayload(); // sonata has dependency on ClassUtils so this is fine. $class = ClassUtils::getClass($object); $admin = $this->pool->getAdminByClass($class); $links = array(); $routeCollection = $admin->getRoutes(); foreach ($routeCollection->getElements() as $code => $route) { $routeName = $route->getDefault('_sonata_name'); $url = $this->urlGenerator->generate($routeName, array($admin->getIdParameter() => $admin->getUrlsafeIdentifier($object)), true); $routeRole = substr($code, strlen($admin->getCode()) + 1); $links[$routeRole] = $url; } if (isset($links['list'])) { $description->set('list', $links['list']); unset($links['list']); } if (isset($links['create'])) { $description->set(Descriptor::LINK_CREATE_HTML, $links['create']); unset($links['create']); } if (isset($links['edit'])) { $description->set(Descriptor::LINK_EDIT_HTML, $links['edit']); unset($links['edit']); } if (isset($links['delete'])) { $description->set(Descriptor::LINK_REMOVE_HTML, $links['delete']); unset($links['delete']); } if (isset($links['show'])) { $description->set(Descriptor::LINK_SHOW_HTML, $links['show']); unset($links['show']); } $description->set(Descriptor::PAYLOAD_TITLE, $admin->toString($object)); $description->set(Descriptor::TYPE_ALIAS, $admin->getLabel()); }
/** * Get the identifiers as a string that is save to use in an url. * * @param object $model * * @return string string representation of the id that is save to use in an url */ public function getUrlsafeIdentifier($model) { $admin = $this->pool->getAdminByClass(ClassUtils::getClass($model)); return $admin->getUrlsafeIdentifier($model); }