/** * Set the default locale for the hydration request. * * @param HydrateEvent $event */ public function handleDefaultLocale(HydrateEvent $event) { // set the default locale if (null === $event->getLocale()) { $event->setLocale($this->documentRegistry->getDefaultLocale()); } }
/** * It should set the default locale. */ public function testDefaultLocale() { $this->hydrateEvent->getLocale()->willReturn(null); $this->registry->getDefaultLocale()->willReturn('de'); $this->hydrateEvent->setLocale('de')->shouldBeCalled(); $this->subscriber->handleDefaultLocale($this->hydrateEvent->reveal()); }
/** * @param PersistEvent $event * * @throws DocumentManagerException */ public function handlePersist(PersistEvent $event) { $document = $event->getDocument(); if (!$document instanceof AutoNameBehavior) { return; } $title = $document->getTitle(); if (!$title) { throw new DocumentManagerException(sprintf('Document has no title (title is required for auto name behavior): %s)', DocumentHelper::getDebugTitle($document))); } $name = $this->slugifier->slugify($title); $parentNode = $event->getParentNode(); $node = $event->hasNode() ? $event->getNode() : null; $name = $this->resolver->resolveName($parentNode, $name, $node); if (null === $node) { $node = $this->documentStrategy->createNodeForDocument($document, $parentNode, $name); $event->setNode($node); return; } if ($name === $node->getName()) { return; } $node = $event->getNode(); $defaultLocale = $this->registry->getDefaultLocale(); if ($defaultLocale != $event->getLocale()) { return; } $this->rename($node, $name); }
public function setUp() { parent::setUp(); $this->webspaceManager = $this->prophesize(WebspaceManagerInterface::class); $this->inspector = $this->prophesize(DocumentInspector::class); $this->registry = $this->prophesize(DocumentRegistry::class); $this->document = $this->prophesize(StructureBehavior::class)->willImplement(WebspaceBehavior::class); $this->webspace = $this->prophesize(Webspace::class); $this->localization1 = $this->prophesize(Localization::class); $this->localization2 = $this->prophesize(Localization::class); $this->subscriber = new FallbackLocalizationSubscriber($this->encoder->reveal(), $this->inspector->reveal(), $this->registry->reveal(), new LocalizationFinder($this->webspaceManager->reveal())); $this->hydrateEvent->getNode()->willReturn($this->node->reveal()); $this->hydrateEvent->getDocument()->willReturn($this->document->reveal()); $this->hydrateEvent->getLocale()->willReturn(self::FIX_LOCALE); $this->webspaceManager->findWebspaceByKey(self::FIX_WEBSPACE)->willReturn($this->webspace); $this->registry->getDefaultLocale()->willReturn('de'); }
/** * Return available localizations. * * @param StructureBehavior $document * @param string $locale * * @return string */ public function getAvailableLocalization(StructureBehavior $document, $locale) { $availableLocales = $this->inspector->getLocales($document); if (in_array($locale, $availableLocales)) { return $locale; } $fallbackLocale = null; if ($document instanceof WebspaceBehavior) { $fallbackLocale = $this->localizationFinder->findAvailableLocale($this->inspector->getWebspace($document), $availableLocales, $locale); } if (!$fallbackLocale) { $fallbackLocale = reset($availableLocales); } if (!$fallbackLocale) { $fallbackLocale = $this->documentRegistry->getDefaultLocale(); } return $fallbackLocale; }