How to setup Custom Data Source?

Hey, right now I’m trying to get in touch with custom data sources in Neos. I tried to implement the example from the docs but it won’t work.

Configuration:

    'category':
  type: array
  ui:
    label: 'Kategorie'
    inspector:
      group: 'image'
      editor: Neos.Neos/Inspector/Editors/SelectBoxEditor
      editorOptions:
        placeholder: Choose
        dataSourceIdentifier: website-derweinmacher-wineselector

Class:

<?php
namespace Website\DerWeinmacher\DataSource;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Neos\Domain\Service\UserService;
use Neos\Neos\Service\DataSource\AbstractDataSource;
use Neos\ContentRepository\Domain\Model\NodeInterface;

class WineSelectorDataSource extends AbstractDataSource
{

  /**
  * @var string
  */
  static protected $identifier = 'website-derweinmacher-wineselector';

  /**
  * @Flow\Inject
  * @var UserService
  */

  protected $userService;

  /**
  * @Flow\Inject
  * @var PersistenceManagerInterface
  */
  protected $persistenceManager;

  /**
  * @param NodeInterface $node The node that is currently edited (optional)
  * @param array $arguments Additional arguments (key / value)
  * @return array
  */

  public function getData(NodeInterface $node = null, array $arguments)
  {
    $options = [];
    foreach ($this->userService->getUsers() as $user) {
      $options[$this->persistenceManager->getIdentifierByObject($user)] = ['label' => $user->getLabel()];
    }
    return $options;
  }
}

Error:

Fatal error: Declaration of Website\DerWeinmacher\DataSource\WineSelectorDataSource::getData(?Neos\ContentRepository\Domain\Model\NodeInterface $node, array $arguments) must be compatible with Neos\Neos\Service\DataSource\DataSourceInterface::getData(?Neos\ContentRepository\Domain\Model\NodeInterface $node = NULL, array $arguments = Array) in /html/neos/DistributionPackages/Website.DerWeinmacher/Classes/DataSources/WineSelectorDataSource.php on line 43

Exception Code 1355480641
Exception Type Neos\Flow\Core\Booting\Exception\SubProcessException
Log Reference 202010042337596506ef
Thrown in File Packages/Framework/Neos.Flow/Classes/Core/Booting/Scripts.php
Line 699

Did I missed something in the setup?

David

The PHP error tells you, that yor getData method is not completely the same as from the interface. You must follow the interface 100% or else PHP will throw this error.

It tells you that it looks like
Neos\Neos\Service\DataSource\DataSourceInterface::getData(?Neos\ContentRepository\Domain\Model\NodeInterface $node = NULL, array $arguments = Array)

so you might be missing a [] as default value as defined in the source

I think we had this question before. Probably one of our tutorials or something is incorrect with the latest Neos versions.

@DavidLang which docs did you use?

Ah, just copied the code from docs without checking it again.

@sebobo I got the code from here:
https://docs.neos.io/cms/manual/extending-neos-with-php-flow/custom-data-sources

There is missing “=[]” after “$arguments”. But when I add this there must be an other error:

# Required class "Website\DerWeinmacher\DataSources\WineSelectorDataSource" could not be loaded properly for reflection.

Possible reasons are:

* Requiring non-existent classes
* Using non-supported annotations
* Class-/filename missmatch.

The "Neos.Flow.object.excludeClasses" setting can be used to skip classes
from being reflected.

Type: Neos\Flow\Reflection\Exception\ClassLoadingForReflectionFailedException
File: Packages/Framework/Neos.Flow/Classes/Reflection/ClassReflection.php
Line: 31

Open Data/Logs/Exceptions/202010051857317d111d.txt for a full stack trace.

|Exception Code|1355480641|
| --- | --- |
|Exception Type|Neos\Flow\Core\Booting\Exception\SubProcessException|
|Log Reference|20201005205731d5611e|
|Thrown in File|Packages/Framework/Neos.Flow/Classes/Core/Booting/Scripts.php|
|Line|699|

Here you can check the whole message from the frontend:
http://p559898.mittwaldserver.info

I’m not sure where something is missing/wrong.

Your namespace in the file is Website\DerWeinmacher\DataSource but the folder is of the name DtaSources - or opposite way around. This messes with the autoloader - see the error message

Possible reasons are:

* Requiring non-existent classes
* Using non-supported annotations
* Class-/filename missmatch

Correct the file/folder name to match the class and namespace

I update the docs with the added [] to the arguments argument :+1:

Hey, that worked great. Thank you @sorenmalling ! I have one last point. I’m looking for a tutorial how to get values from a page node into the dropdown from the editor. Do you know something like that? Or can someone give me some hints how I can solve that?

If you have NodeData you can access properties like $node->getProperty('properyName') - is that what you want?