/** * {@inheritdoc} */ public function enhance(Description $description) { $metadata = $this->metadataFactory->getMetadataFor($description->getResource()->getPayloadType()); $childClasses = $metadata->getChildClasses(); $childTypes = []; // explode the allowed types into concrete classes foreach ($this->metadataFactory->getAllMetadata() as $childMetadata) { foreach ($childClasses as $childClass) { if ($childClass == $childMetadata->name || $childMetadata->getReflectionClass()->isSubclassOf($childClass)) { $childTypes[] = $childMetadata->name; } } } $description->set(Descriptor::CHILDREN_ALLOW, !$metadata->isLeaf()); $description->set(Descriptor::CHILDREN_TYPES, $childTypes); }
/** * {@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()); }
private function processChildrenTypes(Description $description, Metadata $metadata, Request $request, $payload) { $childClasses = $description->get(Descriptor::CHILDREN_TYPES); $childLinks = []; foreach ($childClasses as $childClass) { try { $metadata = $this->registry->getByClass($childClass); } catch (\InvalidArgumentException $e) { continue; } $configuration = $this->requestConfigurationFactory->create($metadata, $request); $url = $this->urlGenerator->generate($configuration->getRouteName('create')); $childLinks[$metadata->getAlias()] = $url . '?parent=' . $payload->getId(); } $description->set(Descriptor::LINKS_CREATE_CHILD_HTML, $childLinks); }
/** * It should add child type metadata if the CHILDREN_TYPE descriptor has previously been set. */ public function testChildrenType() { $this->initEnhance(); $this->urlGenerator->generate('create')->will(function ($args) { return '/create'; }); $this->metadata->getAlias()->willReturn('std.class'); $description = new Description($this->cmfResource->reveal()); $description->set(Descriptor::CHILDREN_TYPES, [\stdClass::class]); $this->enhancer->enhance($description); $this->assertEquals([Descriptor::LINK_SHOW_HTML => 'show/5', Descriptor::LINK_LIST_HTML => 'index/5', Descriptor::LINK_EDIT_HTML => 'update/5', Descriptor::LINK_CREATE_HTML => 'create/5', Descriptor::LINK_REMOVE_HTML => 'delete/5', Descriptor::CHILDREN_TYPES => [\stdClass::class], Descriptor::LINKS_CREATE_CHILD_HTML => ['std.class' => '/create?parent=5']], $description->all()); }
/** * It should return the resource that it describes. */ public function testGetResource() { $resource = $this->description->getResource(); $this->assertSame($this->resource->reveal(), $resource); }