Sulu\Bundle\ContentBundle\Content\Types\InternalLinks::write PHP Method

write() public method

public write ( PHPCR\NodeInterface $node, Sulu\Component\Content\Compat\PropertyInterface $property, $userId, $webspaceKey, $languageCode, $segmentKey )
$node PHPCR\NodeInterface
$property Sulu\Component\Content\Compat\PropertyInterface
    public function write(NodeInterface $node, PropertyInterface $property, $userId, $webspaceKey, $languageCode, $segmentKey)
    {
        $value = $property->getValue();
        if ($value instanceof ArrayableInterface) {
            $value = $value->toArray();
        }
        if (isset($value)) {
            // remove not existing ids
            $session = $node->getSession();
            $selectedNodes = $session->getNodesByIdentifier($value);
            $ids = [];
            foreach ($selectedNodes as $selectedNode) {
                if ($selectedNode->getIdentifier() === $node->getIdentifier()) {
                    throw new \InvalidArgumentException('You are not allowed to link a page to itself!');
                }
                $ids[] = $selectedNode->getIdentifier();
            }
            $value = $ids;
        }
        // set value to node
        $node->setProperty($property->getName(), $value, PropertyType::REFERENCE);
    }

Usage Example

Exemplo n.º 1
0
 public function testWriteWithNoneExistingUUID()
 {
     $subNode1 = $this->getMockForAbstractClass('Sulu\\Bundle\\ContentBundle\\Tests\\Unit\\Content\\Types\\InternalLink\\NodeInterface', [], '', true, true, true);
     $subNode1->expects($this->any())->method('getIdentifier')->will($this->returnValue('123-123-123'));
     $subNode2 = $this->getMockForAbstractClass('Sulu\\Bundle\\ContentBundle\\Tests\\Unit\\Content\\Types\\InternalLink\\NodeInterface', [], '', true, true, true);
     $subNode2->expects($this->any())->method('getIdentifier')->will($this->returnValue('123-456-789'));
     $node = $this->getMockForAbstractClass('Sulu\\Bundle\\ContentBundle\\Tests\\Unit\\Content\\Types\\InternalLink\\NodeInterface', [], '', true, true, true);
     $session = $this->getMockForAbstractClass('PHPCR\\SessionInterface', [], '', true, true, true);
     $node->expects($this->any())->method('getSession')->will($this->returnValue($session));
     $session->expects($this->any())->method('getNodesByIdentifier')->will($this->returnValue([$subNode1, $subNode2]));
     $property = $this->getMockForAbstractClass('Sulu\\Component\\Content\\Compat\\PropertyInterface', [], '', true, true, true);
     $property->expects($this->any())->method('getName')->will($this->returnValue('property'));
     $property->expects($this->any())->method('getValue')->will($this->returnValue(['123-123-123', '123-456-789', 'not existing']));
     $node->expects($this->once())->method('setProperty')->with('property', ['123-123-123', '123-456-789']);
     $this->internalLinks->write($node, $property, 1, 'test', 'de', null);
 }