createTranslatedProperty() public method

Create a new "translated" property.
public createTranslatedProperty ( object $property, string $locale, Sulu\Component\Content\Compat\StructureInterface $structure = null ) : Sulu\Component\Content\Compat\PropertyInterface
$property object
$locale string
$structure Sulu\Component\Content\Compat\StructureInterface
return Sulu\Component\Content\Compat\PropertyInterface
Beispiel #1
0
 /**
  * {@inheritDoc}
  */
 public function getProperty($name)
 {
     $this->init();
     if (isset($this->properties[$name])) {
         return $this->properties[$name];
     }
     if (!$this->node) {
         $this->node = $this->inspector->getNode($this->document);
     }
     $structureProperty = $this->structureMetadata->getProperty($name);
     $contentTypeName = $structureProperty->getType();
     $bridge = new StructureBridge($this->structureMetadata, $this->inspector, $this->legacyPropertyFactory, $this->document);
     if ($structureProperty->isLocalized()) {
         $locale = $this->inspector->getLocale($this->document);
         $property = $this->legacyPropertyFactory->createTranslatedProperty($structureProperty, $locale, $bridge);
     } else {
         $property = $this->legacyPropertyFactory->createProperty($structureProperty);
     }
     $this->legacyProperties[$name] = $property;
     $property->setStructure($bridge);
     $contentType = $this->contentTypeManager->get($contentTypeName);
     $contentType->read($this->node, $property, null, null, null);
     $valueProperty = new PropertyValue($name, $property->getValue());
     $this->properties[$name] = $valueProperty;
     return $valueProperty;
 }
 /**
  * It should create a translated property.
  *
  * @depends testCreateProperty
  */
 public function testCreateTranslated()
 {
     $this->setUpProperty($this->property1);
     $this->namespaceRegistry->getPrefix('content_localized')->willReturn('i18n');
     $translatedProperty = $this->factory->createTranslatedProperty($this->property1->reveal(), 'de');
     $this->assertEquals('de', $translatedProperty->getLocalization());
     $this->assertEquals('i18n:de-name', $translatedProperty->getName());
 }
Beispiel #3
0
 /**
  * Prepare document-property and import them.
  *
  * @param PropertyInterface $property
  * @param NodeInterface $node
  * @param string $value
  * @param string $webspaceKey
  * @param string $locale
  * @param string $format
  */
 protected function importProperty(PropertyInterface $property, NodeInterface $node, StructureInterface $structure, $value, $webspaceKey, $locale, $format)
 {
     $contentType = $property->getContentTypeName();
     if (!$this->contentImportManager->hasImport($contentType, $format)) {
         return;
     }
     $translateProperty = $this->legacyPropertyFactory->createTranslatedProperty($property, $locale, $structure);
     $this->contentImportManager->import($contentType, $node, $translateProperty, $value, null, $webspaceKey, $locale);
 }
 private function doGetProperty($name, $contentTypeName, $locale)
 {
     $this->propertyMetadata->getType()->willReturn($contentTypeName);
     $this->structureMetadata->getProperty($name)->willReturn($this->propertyMetadata);
     $this->contentTypeManager->get($contentTypeName)->willReturn($this->contentType->reveal());
     if ($locale) {
         $this->propertyFactory->createTranslatedProperty($this->propertyMetadata->reveal(), $locale, Argument::type(StructureBridge::class))->willReturn($this->legacyProperty->reveal());
     } else {
         $this->propertyFactory->createProperty($this->propertyMetadata->reveal(), $locale)->willReturn($this->legacyProperty->reveal());
     }
     $this->contentType->read($this->node->reveal(), $this->legacyProperty->reveal(), null, null, null)->shouldBeCalledTimes(1);
     $property = $this->structure->getProperty($name);
     $this->assertInstanceOf(PropertyValue::class, $property);
     $this->assertEquals($name, $property->getName());
 }
Beispiel #5
0
 /**
  * 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);
     }
 }
 /**
  * It should set the structure type and map the content to thethe node.
  */
 public function testPersist()
 {
     $document = new TestContentDocument($this->structure->reveal());
     $document->setStructureType('foobar');
     $this->persistEvent->getDocument()->willReturn($document);
     // map the structure type
     $this->persistEvent->getLocale()->willReturn('fr');
     $this->encoder->contentName('template')->willReturn('i18n:fr-template');
     $this->node->setProperty('i18n:fr-template', 'foobar')->shouldBeCalled();
     // 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->structureProperty->getContentTypeName()->willReturn('content_type');
     $this->contentTypeManager->get('content_type')->willReturn($this->contentType->reveal());
     $this->propertyFactory->createTranslatedProperty($this->structureProperty->reveal(), 'fr')->willReturn($this->legacyProperty->reveal());
     $this->structure->getProperty('prop1')->willReturn($this->propertyValue->reveal());
     $this->propertyValue->getValue()->willReturn('test');
     $this->contentType->remove($this->node->reveal(), $this->legacyProperty->reveal(), 'webspace', 'fr', null)->shouldBeCalled();
     $this->contentType->write($this->node->reveal(), $this->legacyProperty->reveal(), null, 'webspace', 'fr', null)->shouldBeCalled();
     $this->subscriber->handlePersist($this->persistEvent->reveal());
 }