Return the structure for the given StructureBehavior implementing document.
public getStructureMetadata ( Sulu\Component\Content\Document\Behavior\StructureBehavior $document ) : |
||
$document | Sulu\Component\Content\Document\Behavior\StructureBehavior | |
return |
/** * Return a structure bridge corresponding to the given document. * * @param BasePageDocument $document * * @return PageBridge */ protected function documentToStructure(BasePageDocument $document) { $structure = $this->inspector->getStructureMetadata($document); $documentAlias = $this->inspector->getMetadata($document)->getAlias(); $structureBridge = $this->structureManager->wrapStructure($documentAlias, $structure); $structureBridge->setDocument($document); return $structureBridge; }
private function getResourceSegmentProperty($document) { $structure = $this->inspector->getStructureMetadata($document); $property = $structure->getPropertyByTagName('sulu.rlp'); if (!$property) { throw new \RuntimeException(sprintf('Structure "%s" does not have a "sulu.rlp" tag which is required for documents implementing the ' . 'ResourceSegmentBehavior. In "%s"', $structure->name, $structure->resource)); } return $property; }
/** * Return a structure bridge corresponding to the given document. * * @param StructureBehavior $document * * @return StructureBridge * * @deprecated */ private function documentToStructure(StructureBehavior $document) { if (null === $document) { return; } $structure = $this->documentInspector->getStructureMetadata($document); $documentAlias = $this->documentInspector->getMetadata($document)->getAlias(); $structureBridge = $this->structureManager->wrapStructure($documentAlias, $structure); $structureBridge->setDocument($document); return $structureBridge; }
/** * Adds all the structure specific data (template, structure properties and breadcrumb) to the serialization. * * @param ObjectEvent $event */ public function onPostSerialize(ObjectEvent $event) { $document = $event->getObject(); $context = $event->getContext(); if (!$document instanceof StructureBehavior) { return; } $visitor = $event->getVisitor(); $structureMetadata = $this->inspector->getStructureMetadata($document); $visitor->addData('template', $document->getStructureType()); $visitor->addData('originTemplate', $document->getStructureType()); $visitor->addData('internal', false); $visitor->addData('localizedTemplate', $structureMetadata->getTitle($this->inspector->getLocale($document))); if (array_search('defaultPage', $context->attributes->get('groups')->getOrElse([])) !== false) { $this->addStructureProperties($structureMetadata, $document, $visitor); } // create bread crumbs if (array_search('breadcrumbPage', $context->attributes->get('groups')->getOrElse([])) !== false) { $this->addBreadcrumb($document, $visitor); } }
public function setUp() { $this->contentTypeManager = $this->prophesize(ContentTypeManagerInterface::class); $this->node = $this->prophesize(NodeInterface::class); $this->structureMetadata = $this->prophesize(StructureMetadata::class); $this->document = $this->prophesize(StructureBehavior::class); $this->contentType = $this->prophesize(ContentTypeInterface::class); $this->encoder = $this->prophesize(PropertyEncoder::class); $this->propertyMetadata = $this->prophesize(PropertyMetadata::class); $this->propertyFactory = $this->prophesize(LegacyPropertyFactory::class); $this->inspector = $this->prophesize(DocumentInspector::class); $this->legacyProperty = $this->prophesize(PropertyInterface::class); $this->structure = new ManagedStructure($this->contentTypeManager->reveal(), $this->propertyFactory->reveal(), $this->inspector->reveal(), $this->document->reveal()); $this->inspector->getNode($this->document->reveal())->willReturn($this->node->reveal()); $this->inspector->getStructureMetadata($this->document->reveal())->willReturn($this->structureMetadata->reveal()); }
/** * It should throw an exception if the property is required but the value is null. * * @expectedException Sulu\Component\Content\Exception\MandatoryPropertyException */ public function testThrowExceptionPropertyRequired() { $document = new TestContentDocument($this->structure->reveal()); $document->setStructureType('foobar'); $this->persistEvent->getDocument()->willReturn($document); // map the structure type $this->persistEvent->getLocale()->willReturn('fr'); // map the content $this->inspector->getStructureMetadata($document)->willReturn($this->structureMetadata->reveal()); $this->inspector->getWebspace($document)->willReturn('webspace'); $this->structureMetadata->getProperties()->willReturn(['prop1' => $this->structureProperty->reveal()]); $this->structureProperty->isRequired()->willReturn(true); $this->structure->getProperty('prop1')->willReturn($this->propertyValue->reveal()); $this->propertyValue->getValue()->willReturn(null); $this->structureMetadata->getName()->willReturn('test'); $this->structureMetadata->getResource()->willReturn('/path/to/resource.xml'); $this->subscriber->handlePersist($this->persistEvent->reveal()); }
/** * Map to the content properties to the node using the content types. * * @param mixed $document * @param NodeInterface $node * @param string $locale * * @throws MandatoryPropertyException */ private function mapContentToNode($document, NodeInterface $node, $locale) { $structure = $document->getStructure(); $webspaceName = $this->inspector->getWebspace($document); $metadata = $this->inspector->getStructureMetadata($document); foreach ($metadata->getProperties() as $propertyName => $structureProperty) { $realProperty = $structure->getProperty($propertyName); $value = $realProperty->getValue(); if ($structureProperty->isRequired() && null === $value) { throw new MandatoryPropertyException(sprintf('Property "%s" in structure "%s" is required but no value was given. Loaded from "%s"', $propertyName, $metadata->getName(), $metadata->resource)); } $contentTypeName = $structureProperty->getContentTypeName(); $contentType = $this->contentTypeManager->get($contentTypeName); // TODO: Only write if the property has been modified. $legacyProperty = $this->legacyPropertyFactory->createTranslatedProperty($structureProperty, $locale); $legacyProperty->setValue($value); $contentType->remove($node, $legacyProperty, $webspaceName, $locale, null); $contentType->write($node, $legacyProperty, null, $webspaceName, $locale, null); } }
/** * If this is a shadow document, update the URL to that of the shadowed document. * * TODO: This is about caching and should be handled somewhere else. * * @param PersistEvent $event */ public function handlePersistUpdateUrl(PersistEvent $event) { $document = $event->getDocument(); if (!$document instanceof ShadowLocaleBehavior) { return; } if (!$document->isShadowLocaleEnabled()) { return; } $node = $event->getNode(); $structure = $this->inspector->getStructureMetadata($document); if (false === $structure->hasPropertyWithTagName('sulu.rlp')) { return; } $locatorProperty = $structure->getPropertyByTagName('sulu.rlp'); if ($node->getPropertyValueWithDefault($this->encoder->localizedSystemName($locatorProperty->getName(), $document->getLocale()), null)) { return; } $shadowLocator = $node->getPropertyValueWithDefault($this->encoder->localizedSystemName($locatorProperty->getName(), $document->getShadowLocale()), null); if (!$shadowLocator) { return; } $event->getAccessor()->set('resourceSegment', $shadowLocator); }
private function init() { if (!$this->structureMetadata) { $this->structureMetadata = $this->inspector->getStructureMetadata($this->document); } }
protected function documentToStructure(StructureBehavior $document) { return new $this($this->inspector->getStructureMetadata($document), $this->inspector, $this->propertyFactory, $document); }
/** * Invalidates the structure of the given document. * * @param $document */ private function invalidateDocumentStructure($document) { $structureBridge = $this->structureManager->wrapStructure($this->documentInspector->getMetadata($document)->getAlias(), $this->documentInspector->getStructureMetadata($document)); $structureBridge->setDocument($document); $this->structureHandler->invalidateStructure($structureBridge); }
/** * Finds the correct route for the current request. * It loads the correct data with the content mapper. * * @param Request $request A request against which to match * * @return \Symfony\Component\Routing\RouteCollection with all Routes that * could potentially match $request. Empty collection if nothing can * match */ public function getRouteCollectionForRequest(Request $request) { $collection = new RouteCollection(); // no portal information without localization supported if ($this->requestAnalyzer->getCurrentLocalization() === null && $this->requestAnalyzer->getMatchType() !== RequestAnalyzerInterface::MATCH_TYPE_PARTIAL && $this->requestAnalyzer->getMatchType() !== RequestAnalyzerInterface::MATCH_TYPE_REDIRECT) { return $collection; } $htmlExtension = '.html'; $resourceLocator = $this->requestAnalyzer->getResourceLocator(); if ($this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_REDIRECT || $this->requestAnalyzer->getMatchType() == RequestAnalyzerInterface::MATCH_TYPE_PARTIAL) { // redirect webspace correctly with locale $collection->add('redirect_' . uniqid(), $this->getRedirectWebSpaceRoute()); } elseif ($request->getRequestFormat() === 'html' && substr($request->getPathInfo(), -strlen($htmlExtension)) === $htmlExtension) { // redirect *.html to * (without url) $collection->add('redirect_' . uniqid(), $this->getRedirectRoute($request, $this->getUrlWithoutEndingTrailingSlash($resourceLocator))); } else { // just show the page $portal = $this->requestAnalyzer->getPortal(); $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocalization(); $resourceLocatorStrategy = $this->resourceLocatorStrategyPool->getStrategyByWebspaceKey($portal->getWebspace()->getKey()); try { // load content by url ignore ending trailing slash /** @var PageDocument $document */ $document = $this->documentManager->find($resourceLocatorStrategy->loadByResourceLocator(rtrim($resourceLocator, '/'), $portal->getWebspace()->getKey(), $locale), $locale, ['load_ghost_content' => false]); if (!$document->getTitle()) { // If the title is empty the document does not exist in this locale // Necessary because of https://github.com/sulu/sulu/issues/2724, otherwise locale could be checked return $collection; } if (preg_match('/\\/$/', $resourceLocator) && $this->requestAnalyzer->getResourceLocatorPrefix()) { // redirect page to page without slash at the end $collection->add('redirect_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . rtrim($resourceLocator, '/'))); } elseif ($document->getRedirectType() === RedirectType::INTERNAL) { // redirect internal link $redirectUrl = $this->requestAnalyzer->getResourceLocatorPrefix() . $document->getRedirectTarget()->getResourceSegment(); if ($request->getQueryString()) { $redirectUrl .= '?' . $request->getQueryString(); } $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getRedirectRoute($request, $redirectUrl)); } elseif ($document->getRedirectType() === RedirectType::EXTERNAL) { $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getRedirectRoute($request, $document->getRedirectExternal())); } elseif (!$this->checkResourceLocator()) { return $collection; } else { // convert the page to a StructureBridge because of BC /** @var PageBridge $structure */ $structure = $this->structureManager->wrapStructure($this->documentInspector->getMetadata($document)->getAlias(), $this->documentInspector->getStructureMetadata($document)); $structure->setDocument($document); // show the page $collection->add($document->getStructureType() . '_' . $document->getUuid(), $this->getStructureRoute($request, $structure)); } } catch (ResourceLocatorNotFoundException $exc) { // just do not add any routes to the collection } catch (ResourceLocatorMovedException $exc) { // old url resource was moved $collection->add($exc->getNewResourceLocatorUuid() . '_' . uniqid(), $this->getRedirectRoute($request, $this->requestAnalyzer->getResourceLocatorPrefix() . $exc->getNewResourceLocator())); } catch (RepositoryException $exc) { // just do not add any routes to the collection } } return $collection; }