Skip to content

7.4. Creating a custom data provider

Steve Karsch edited this page Jun 15, 2017 · 3 revisions

Firstly, copy an existing data provider class file (e.g. restful/src/Plugin/resource/DataProvider/DataProviderDbQuery.php) to your custom module, in a similar file path.

Rename this file to reflect what your class will do, then edit the file:

  • change the various class references from the original to your new one e.g. * Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderDbQuery. etc

  • change the namespace to your module e.g.

    namespace Drupal\my_module\Plugin\resource\DataProvider;
  • replace all the use statements with just those that your new class actually uses. It will need at least one, pointing at the original data provider class you're extending:

    use Drupal\restful\Plugin\resource\DataProvider\DataProviderDbQuery;
  • replace the class definition with something like:

    class DataProviderDbQueryFancy extends DataProviderDbQuery {
    }

    and then add your custom code.

  • finally, be sure to tell your custom resource to use your new data provider. See Assigning a different data provider for more.

Clone this wiki locally