In Drupal the default field storage is in an SQL database. I am creating a custom field type and want it to have non-SQL storage.

In Drupal 7 there was the Field Storage API, which you could use to define your own storage back ends by implementing a module similar to the field_sql_storage module.

How does one do such a thing in Drupal 8?

To give an example usage, I am interested in implementing something similar to Drupal 7's hook_field_storage_load() so I can have fields that load their data from files in the file system.

If it is too complex a thing to give a code example for, a point in the direction of the relevant API and/or default SQL implementation that I can reference should suffice.

  • I think you're asking how to create a field type with a custom storage, but I'm not sure. Is that correct? Or are you asking how to swap out field storage for existing field types? – mradcliffe Mar 2 '16 at 13:07
  • Yes that is correct, I am creating a custom field type and want it to have non-sql storage. – rooby Mar 2 '16 at 13:18
  • With D8, as @moshe_weitzman implied, some value needs to be stored with the entity. The trick, as exemplified by computed_field, is that you can manipulate that value on preSave() (to save it to NoSQL and keep a DBRef, for instance, in the entity storage), and on display with a custom formatter. The limitation being that you don't get the actual data until you perform an entity view, and cannot use a formatter of your choice. Possible workaround for the latter involves a standard formatter and an entity_prepare_view hook to side-load the data from NoSQL. – FGM Jul 16 '16 at 20:47

Field types implement FieldStorageDefinitionInterface. In Drupal core, there is FieldStorageConfig and BaseFieldDefinition.

So if the desire is to change field storage to an arbitrary file store, then you should create an abstract class similar to BaseFieldDefinition, and have your Field Type classes extend it.

  • So that allows me to tell my field it has no schema and uses custom storage right? Then for the storage handler itself would I implement a class similar to SqlContentEntityStorage that extends ContentEntityStorageBase? After that, how/where to I tell the field to use my storage class? – rooby Mar 3 '16 at 1:03

Fields don't control own storage anymore with Drupal 8. The entity is in charge. See http://www.drupal.org/project/external_entities/ for an example.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.