[SOLVED] Convert Datasource object into its object instance

Hello,

I have a datasource to link users to a node. I got it working, but it’s not perfect yet, because the property is saved as the node identifier and it doesn’t convert to the User model automatically. Here’s my configuration:

yaml

author:
  #type: Neos\Neos\Domain\Model\User
  type: string
  ui:
    label: Author
    reloadPageIfChanged: true
    inspector:
      group: document
      editor: 'Neos.Neos/Inspector/Editors/SelectBoxEditor'
      editorOptions:
        placeholder: Choose author
        dataSourceIdentifier: my-datasource-identifier

UserDataSouce.php

/**
 * 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)
{
    $options = [];
    foreach ($this->userRepository->findAll() as $user) {
        /** @var $user User */
        $options[] = [
            'label' => $user->getLabel(),
            'value' => $this->persistenceManager->getIdentifierByObject($user),
        ];
    }

    return $options;
}

Then in fusion, author will be a string, but I need it to be an instance of Neos\Neos\Domain\Model\User:

author = ${q(node).property('author')}

if I switch the author type to Neos\Neos\Domain\Model\User, the inspector doesn’t save the data in the backend.

You commented out the above part and assigned it as a string - I suppose that was because it didn’t work?

Yeap, the selector reloads as empty every time I Save, that’s why I switched it to string.

@dimitriadisg,

Are you using the new UI?

There was an issue https://github.com/neos/neos-ui/issues/1667 wich has been solved already. Maybe you need to update you packages.

Hi Peter,

thanks for the reference. I am using the old UI (Neos 3.2), but from this link on the issue post I saw an example of definition and found what I was doing wrong.

I didn’t have a definition in the settings for the Neos\Neos\Domain\Model\User dataType. And weirdly enough, defining the typeConverter inside the properties definition doesn’t work. I also tried with ObjectConverter class before, but didn’t work either.

So, for my case, I had to add the following configuration in my settings.yaml:

Neos:
  Neos:
    inspector:
        dataTypes:
          'Neos\Neos\Domain\Model\User':
            typeConverter: 'Neos\Neos\TypeConverter\EntityToIdentityConverter'
            editor: 'Neos.Neos/Inspector/Editors/SelectBoxEditor'
            editorOptions:
              dataSourceIdentifier: 'my-datasource-identifier'

and then the property can be defined like so:

author:
  type: Neos\Neos\Domain\Model\User
  ui:
    label: Author
    reloadPageIfChanged: true
    inspector:
      group: document
      editorOptions:
        placeholder: Choose author