Access node property in DataSource

Hallo,

how is it possible to access node properties in DataSources? I want to check, if a property(“vehicleid”) is set already at the node, which is currently edited in the inspector. My aim is to set a unique id by a DataSouce, only if this is not set already.

I’m thinking about something like this:

ExampleDataSource.php


$currentNode = $this->getCurrentNode();
$vehicleid = $currentNode->getProperty(‘vehicleid’);

if(empty($vehicleid)) {
  $newDate = new \DateTime();
  $uid = $newDate->format('YmdHis');

  $vehicleIdArr[] = array(
	'value' => $uid,
	'label' => $uid,
	'group' => '',
	'icon'  => '',
   );
  return $vehicleArr;
}

You would need to give the current node as argument to the data source. Don’t think I have an example at hand. Not even sure if we can easily do that.

Good morning Christian,

thank you.
I thought setting a unique id(which is shown in frontend, too) for a node is a widespread use case. In example a product-id or something like that.
It’s not necessary to be able to edit this id in the inspector for my usecase, but I want to have influence on the format of the id and it should be a property of the node.

Maybe any other idea to realize that?

Maybe I just don’t understand yet what you want to do?

So what is your plan?

I’m sorry for my ambiguous description. I’ll try it again:

The owner of the website is a car seller, so the editors are dealing with cars.
So I created a custom nodetype named “car”, which shall have a property named “vehicleid”. This vehicleid shall be a unique id, which shall be “set automatically” at creation of every car and has not necessarily to be editable in the inspector.
My idea was to set this id by datasource. Of course only at creation of each car, so I wanted to check in the datasource, if the id is already set.

can’t you just use the node identifier for that?

I think I can.
It shall not sound ungrateful, but I’m wondering how other developers solve this issue - I think these long node identifiers don’t look very nice in frontends and it would be fine to “adjust the format of an unique id” to each use case.
On the other hand I preferably don’t want to generate effort for my special use case (except for me :wink:).

I just wanted to suggest an easy solution. You could probably use a custom node class and directly generate some identifier on your own on creation for example. Or use AOP. There would be ways to accomplish that, I just don’t see the reason.

See TYPO3\Neos\Service\DataSource\DataSourceInterface

/**
 * Get data
 *
 * The return value must be JSON serializable data structure.
 *
 * @param NodeInterface $node The node that is currently edited (optional)
 * @param array $arguments Additional arguments (key / value)
 * @return mixed JSON serializable data
 * @api
 */
public function getData(NodeInterface $node = null, array $arguments);

The getData method contains NodeInterface as an optional argument. Why don’t use $node for getting “vehicle” property in your data source?

1 Like

Good point, I totally forgot that you get the node!

The getData method contains NodeInterface as an optional argument. Why
don’t use $node for getting “vehicle” property in your data source?

:flushed: Sometimes I sleep with open eyes I think :smirk:

Thank you, @christianm and @pamegli so much for your kind help and have a nice, sunny weekend!