Neos\Neos\Service\Mapping\NodePropertyConverterService::getProperty PHP Method

getProperty() public method

Get a single property reduced to a simple type (no objects) representation
public getProperty ( Neos\ContentRepository\Domain\Model\NodeInterface $node, string $propertyName ) : mixed
$node Neos\ContentRepository\Domain\Model\NodeInterface
$propertyName string
return mixed
    public function getProperty(NodeInterface $node, $propertyName)
    {
        if ($propertyName[0] === '_') {
            $propertyValue = ObjectAccess::getProperty($node, ltrim($propertyName, '_'));
        } else {
            $propertyValue = $node->getProperty($propertyName);
        }
        $dataType = $node->getNodeType()->getPropertyType($propertyName);
        try {
            $convertedValue = $this->convertValue($propertyValue, $dataType);
        } catch (PropertyException $exception) {
            $this->systemLogger->logException($exception);
            $convertedValue = null;
        }
        if ($convertedValue === null) {
            $convertedValue = $this->getDefaultValueForProperty($node->getNodeType(), $propertyName);
        }
        return $convertedValue;
    }

Usage Example

 /**
  * Renders data attributes needed for the given node property.
  *
  * @param NodeInterface $node
  * @param string $propertyName
  * @return array
  */
 protected function renderNodePropertyAttribute(NodeInterface $node, $propertyName)
 {
     $attributes = [];
     /** @var $contentContext ContentContext */
     $contentContext = $node->getContext();
     // skip the node name of the site node - TODO: Why do we need this?
     if ($propertyName === '_name' && $node === $contentContext->getCurrentSiteNode()) {
         return $attributes;
     }
     $dataType = $node->getNodeType()->getPropertyType($propertyName);
     $dasherizedPropertyName = $this->dasherize($propertyName);
     $propertyValue = $this->nodePropertyConverterService->getProperty($node, $propertyName);
     $propertyValue = $propertyValue === null ? '' : $propertyValue;
     $propertyValue = !is_string($propertyValue) ? json_encode($propertyValue) : $propertyValue;
     if ($dataType !== 'string') {
         $attributes['data-nodedatatype-' . $dasherizedPropertyName] = 'xsd:' . $dataType;
     }
     $attributes['data-node-' . $dasherizedPropertyName] = $propertyValue;
     return $attributes;
 }