drupalextension icon indicating copy to clipboard operation
drupalextension copied to clipboard

Adding a custom field handler

Open jludwig opened this issue 8 years ago • 3 comments
trafficstars

From what I can tell in \Drupal\Driver\Cores\AbstractCore::getFieldHandler(), the only way to add a custom field handler is to put it in the \Drupal\Driver\Fields{core} namespace.

We are using a Composer-based build process, so when we require custom field handlers, I'm just adding patches to drupalextension to place a new field handler into the proper directory.

Is there another way to do this currently? If not, should there be? It seems like something a custom module that defines a custom field should be able to add.

jludwig avatar Aug 01 '17 21:08 jludwig

I think we need a proper plugin system for field handlers, not the half-baked home-baked plugins we currently use.

My idea is use D8's core-plugins component as a composer dependency and build Field and Entity plugin systems using that. Would be sweet.

jonathanjfshaw avatar Aug 02 '17 11:08 jonathanjfshaw

Has there been any more work done on implementing a plugin system to extend the field handlers allowed? We are looking at supporting the GeofieldItem field type from the Geofield module, and would like to keep it mostly forward compatible.

To get the handler class loaded in a composer managed codebase, with the current implementation of getFieldHandler(), I think you could autoload it in the composer.json autoload-dev key, with the full PHP class namespace \Drupal\Driver\Cores\Fields\Drupal8\ (for example) specified alongside the path to your class.

I haven't tried this yet, but it seems like it should work.

johnhelmuth avatar Jun 08 '18 23:06 johnhelmuth

See https://github.com/jhedstrom/DrupalDriver/pull/157

I figure we're 80% of the way there. Reviews and help very much appreciated.

It works fine already for D8, and you could use it as a patch, but not in a way that would give you perfect stable forward compatibility. If your needs are complex (multiple inter-related entities, interventions at different points in entity lifecycle), it might be worth it. If they are simple (e.g. just one field) and you don't want to have to make adjustments down the road, then it's not for you right now.

I think I've seen someone make a custom field handler work by slipping the class into their FeatureContext file.

jonathanjfshaw avatar Jun 09 '18 09:06 jonathanjfshaw