Skip to content

Commit

Permalink
!!! TASK: Replace Node::getLabel with NodeLabelRenderer and `Neos…
Browse files Browse the repository at this point in the history
….Node.renderNodeLabel(node)`
  • Loading branch information
mhsdesign committed Apr 30, 2024
1 parent 15034ab commit 3f51825
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,6 @@ public function hasProperty(string $propertyName): bool
return $this->properties->offsetExists($propertyName);
}

/**
* Returns the node label as generated by the configured node label generator
*
* @return string
*/
public function getLabel(): string
{
// todo REMOVE ME!!!!!!!!!!!!
return $this->nodeType?->getNodeLabelGenerator()->getLabel($this) ?: $this->nodeTypeName->value;
}

public function equals(Node $other): bool
{
return $this->subgraphIdentity->equals($other->subgraphIdentity)
Expand Down
26 changes: 23 additions & 3 deletions Neos.Neos/Classes/Domain/NodeLabel/NodeLabelRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,36 @@
namespace Neos\Neos\Domain\NodeLabel;

use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Neos\Fusion\Helper\NodeHelper;
use Neos\Neos\Utility\NodeTypeWithFallbackProvider;

class NodeLabelRenderer
/**
* @api For PHP, in Fusion one can use ${Neos.Node.renderNodeLabel(node)} {@see NodeHelper::renderNodeLabel()}
*/
final readonly class NodeLabelRenderer
{
use NodeTypeWithFallbackProvider;

public function __construct(
private readonly ObjectManagerInterface $objectManager
private ContentRepositoryRegistry $contentRepositoryRegistry,
private ObjectManagerInterface $objectManager
) {
}

public function create(NodeType $nodeType): NodeLabelGeneratorInterface
public function renderNodeLabel(Node $node): string
{
$nodeType = $this->getNodeType($node);
$generator = $this->getNodeLabelGeneratorForNodeType($nodeType);
return $generator->getLabel($node);
}

/**
* @internal
*/
public function getNodeLabelGeneratorForNodeType(NodeType $nodeType): NodeLabelGeneratorInterface
{
if ($nodeType->hasConfiguration('label.generatorClass')) {
$nodeLabelGeneratorClassName = $nodeType->getConfiguration('label.generatorClass');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Domain\NodeLabel\NodeLabelRenderer;

/**
* Fusion implementation for a dimensions menu.
Expand All @@ -27,6 +29,9 @@
*/
class DimensionsMenuItemsImplementation extends AbstractMenuItemsImplementation
{
#[Flow\Inject()]
protected NodeLabelRenderer $nodeLabelRenderer;

/**
* @return array<mixed>
*/
Expand Down Expand Up @@ -209,7 +214,7 @@ protected function determineLabel(?Node $variant = null, array $metadata = []):
if ($this->getContentDimensionIdentifierToLimitTo()) {
return $metadata[$this->getContentDimensionIdentifierToLimitTo()->value]['label'] ?: '';
} elseif ($variant) {
return $variant->getLabel() ?: '';
return $this->nodeLabelRenderer->renderNodeLabel($variant) ?: '';
} else {
return array_reduce($metadata, function ($carry, $item) {
return $carry . (empty($carry) ? '' : '-') . $item['label'];
Expand Down
18 changes: 17 additions & 1 deletion Neos.Neos/Classes/Fusion/Helper/NodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Domain\Exception;
use Neos\Neos\Domain\NodeLabel\NodeLabelRenderer;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\Neos\Presentation\VisualNodePath;
Expand All @@ -42,6 +43,9 @@ class NodeHelper implements ProtectedContextAwareInterface
#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;

#[Flow\Inject]
protected NodeLabelRenderer $nodeLabelRenderer;

/**
* Check if the given node is already a collection, find collection by nodePath otherwise, throw exception
* if no content collection could be found
Expand Down Expand Up @@ -96,13 +100,25 @@ public function nearestContentCollection(Node $node, string $nodePath): Node
}

/**
* Generate a label for a node with a chaining mechanism. To be used in nodetype definitions.
* Generate a label for a node with a chaining mechanism. To be used in NodeType definition:
*
* 'Vendor.Site:MyContent':
* label: "${Neos.Node.labelForNode(node).prefix('foo')}"
*
*/
public function labelForNode(Node $node): NodeLabelToken
{
return new NodeLabelToken($node);
}

/**
* Renders the actual node label based on the NodeType definition in Fusion.
*/
public function renderNodeLabel(Node $node): string
{
return $this->nodeLabelRenderer->renderNodeLabel($node);
}

/**
* @param Node $node
* @return int
Expand Down
9 changes: 7 additions & 2 deletions Neos.Neos/Classes/Fusion/MenuItemsImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\Subtree;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds;
use Neos\Flow\Annotations as Flow;
use Neos\Fusion\Exception as FusionException;
use Neos\Neos\Domain\NodeLabel\NodeLabelRenderer;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;

/**
Expand Down Expand Up @@ -62,6 +64,9 @@ class MenuItemsImplementation extends AbstractMenuItemsImplementation
*/
protected ?NodeTypeCriteria $nodeTypeCriteria = null;

#[Flow\Inject()]
protected NodeLabelRenderer $nodeLabelRenderer;

/**
* The last navigation level which should be rendered.
*
Expand Down Expand Up @@ -215,7 +220,7 @@ protected function buildMenuItemFromNode(Node $node): MenuItem
return new MenuItem(
$node,
$this->isCalculateItemStatesEnabled() ? $this->calculateItemState($node) : null,
$node->getLabel(),
$this->nodeLabelRenderer->renderNodeLabel($node),
0,
[],
$this->buildUri($node)
Expand All @@ -239,7 +244,7 @@ protected function buildMenuItemFromSubtree(Subtree $subtree, int $startLevel =
return new MenuItem(
$node,
$this->isCalculateItemStatesEnabled() ? $this->calculateItemState($node) : null,
$node->getLabel(),
$this->nodeLabelRenderer->renderNodeLabel($node),
$subtree->level + $startLevel,
$children,
$this->buildUri($node)
Expand Down
6 changes: 5 additions & 1 deletion Neos.Neos/Classes/Utility/NodeUriPathSegmentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n\Locale;
use Neos\Neos\Domain\NodeLabel\NodeLabelRenderer;
use Neos\Neos\Exception;
use Neos\Neos\Service\TransliterationService;

Expand All @@ -30,6 +31,9 @@ class NodeUriPathSegmentGenerator
#[Flow\Inject]
protected TransliterationService $transliterationService;

#[Flow\Inject]
protected NodeLabelRenderer $nodeLabelRenderer;

/**
* Generates a URI path segment for a given node taking its language dimension value into account
*
Expand All @@ -40,7 +44,7 @@ public function generateUriPathSegment(?Node $node = null, ?string $text = null)
{
$language = null;
if ($node) {
$text = $text ?: $node->getLabel() ?: ($node->nodeName?->value ?? '');
$text = $text ?: $this->nodeLabelRenderer->renderNodeLabel($node) ?: ($node->nodeName?->value ?? '');
$languageDimensionValue = $node->originDimensionSpacePoint->coordinates['language'] ?? null;
if (!is_null($languageDimensionValue)) {
$locale = new Locale($languageDimensionValue);
Expand Down
9 changes: 8 additions & 1 deletion Neos.Neos/Classes/ViewHelpers/Link/NodeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\Projection\ContentGraph\NodePath;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Neos\Domain\NodeLabel\NodeLabelRenderer;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\FrontendRouting\NodeAddress;
use Neos\Neos\FrontendRouting\NodeAddressFactory;
Expand Down Expand Up @@ -149,6 +150,12 @@ class NodeViewHelper extends AbstractTagBasedViewHelper
*/
protected $throwableStorage;

/**
* @Flow\Inject
* @var NodeLabelRenderer
*/
protected $nodeLabelRenderer;

/**
* Initialize arguments
*
Expand Down Expand Up @@ -341,7 +348,7 @@ public function render(): string
$this->templateVariableContainer->remove($this->arguments['nodeVariableName']);

if ($content === null && $resolvedNode !== null) {
$content = $resolvedNode->getLabel();
$content = $this->nodeLabelRenderer->renderNodeLabel($resolvedNode);
}

$this->tag->setContent($content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ public function executeCommand(string $contentRepository = 'default', bool $quie
$this->output->outputLine(sprintf('Enabled %d nodes with exceeded timed dates.', $handlingResult->countByType(ChangedVisibilityType::NODE_WAS_ENABLED)));
foreach ($handlingResult->getByType(ChangedVisibilityType::NODE_WAS_ENABLED) as $result) {
$this->output->outputLine(sprintf(
'- NodeAggregateId: %s, DimensionSpacePoint: %s, Label: %s',
'- NodeAggregateId: %s, DimensionSpacePoint: %s',
$result->node->nodeAggregateId->value,
join(',', $result->node->originDimensionSpacePoint->coordinates),
$result->node->getLabel()
join(',', $result->node->originDimensionSpacePoint->coordinates)
)
);
}

$this->output->outputLine(sprintf('Disabled %d nodes with exceeded timed dates.', $handlingResult->countByType(ChangedVisibilityType::NODE_WAS_DISABLED)));
foreach ($handlingResult->getByType(ChangedVisibilityType::NODE_WAS_DISABLED) as $result) {
$this->output->outputLine(sprintf(
'- NodeAggregateId: %s, DimensionSpacePoint: %s, Label: %s',
'- NodeAggregateId: %s, DimensionSpacePoint: %s',
$result->node->nodeAggregateId->value,
join(',', $result->node->originDimensionSpacePoint->coordinates),
$result->node->getLabel()
join(',', $result->node->originDimensionSpacePoint->coordinates)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ private function needsDisabling(Node $node, \DateTimeImmutable $now): bool
private function logResult(ChangedVisibility $result): void
{
$this->logger->info(
sprintf('Timed node visibility: %s node [NodeAggregateId: %s, DimensionSpacePoints: %s]: %s',
sprintf('Timed node visibility: %s node [NodeAggregateId: %s, DimensionSpacePoints: %s]',
$result->type->value,
$result->node->nodeAggregateId->value,
join(',', $result->node->originDimensionSpacePoint->coordinates),
$result->node->getLabel())
join(',', $result->node->originDimensionSpacePoint->coordinates)
)
);
}
}

0 comments on commit 3f51825

Please sign in to comment.