Follow-up of #2116363: Unified repository of field definitions (cache + API).
Problem/Motivation
The mentioned issue changes a number of calls that need a single field definition from getInstance($entity_type_id, $bundle, $field_name) to getField*Definitions(), which returns an array and then needs to get the field from that.
That needs more code and could in some cases be harder to debug, when you attempt to use a field definition that doesn't exist and you get a fatal error.
Proposed resolution
Having getFieldDefinition() and so on methods would allow to directly fetch a single field definition and it would be possible to throw an exception if it doesn't exist.
Remaining tasks
User interface changes
API changes
Three new methods on the interface, very unlikely that someone would implement that directly and would have to provide them.
Comments
Comment #1
tstoecklerCopying my comment from over there here:
I think we will get more used to seeing method()['foo'] over time. I personally think it is much more preferable over introducing a separate method or an optional parameter to the existing method. Such a method needs to be documented, unit tested and it needs to copy paste a trivial implementation for each implementation of the respective interface. And while the corresponding PHP construct might still seem unusual it is 100% clear what is happening.
A typed exception would certainly be preferable, I agree on that part, but a
is quite easily debuggable.
@Berdir's response:
Re @Berdir: Can you elaborate? AFAIK EntityManger::getFieldDefinitions() returns a simple array. So:
Comment #2
berdirgetFieldDefinitions() returns an array of objects. If you call it, then you're going to do something with it, so what you're missing is that the next line will then be something like:
And then you get a fatal error and while the notice will be logged, your application will die instead of throwing an exception that can be handled and at least allows you to display a nice message about something going wrong instead of the fatal error WSOD.
Comment #3
tstoecklerAhh, I see. Thanks for that explanation! Yes, that's actually a very fair and good argument for the dedicated methods.
Comment #4
fago#2 is a good reason, I agree.
Also, I must say that
$definition = $entity_manager->getFieldDefinitions($entity_type, $bundle)[$field_name];is worse to read compared to
$definition = $entity_manager->getFieldDefinition($entity_type, $bundle, $field_name);But then, if the field is not found our typical return value would be NULL, e.g. as field_info_field() does now. But then,
would still die? But the other way round, if we make it throw an exception the field existence check becomes unnecessarily complex as you'd have to catch the exception?
Comment #5
berdirYeah. The exception is supposed to be the last resort, if you try to access something that doesn't exist. Related: #2178795: Allow DiscoveryInterface::getDefinition() to throw an exception for an invalid plugin.
If you want to check if a field exists, you still have to get them all and then do an isset/empty on the array... that's the case anyway I think, unless you want to do an inline !empty($entity_manager->getFieldDefinitions($entity_type, $bundle)[$field_name]), no idea if that even works ;)
That was why I sometimes used the function()[] syntax and sometimes not, when I had to check if it exists, I fetched them all and then did an isset(), and if I was reasonably sure that it does (for example, if we already checked above, are within some code that could only be reached if it does exist), I used the shorthand version.
Not sure how often you need to check if a field exists *without having access to the $entity object* (for those cases, we have $entity->hasField($field_name).
Comment #6
yched commentedYeah, I'd definitely find it weird / DX painful if our various "repository" APIs only had "get all" methods - e.g PluginDiscovery only had getDefinitions() and no getDefinition(), or ParameterBag only had all() and no get() - PHP 5.4 or not.
field_info_field() was much more used than field_info_fields() - granted, partially because of the shape of the API in D7, which is different in D8; But having an API for "things" that doesn't give you access to "things" directly but only to "lists of things" that you then need to dereference yourself is cumbersome.
Gives either long lines like :
$field = $EM->getFieldDefinitions($x, $y)[$name];
or useless temp vars like
$fields = $EM->getFieldDefinitions($x, $y);
$field = $fields[$name];
(what the latest patch in #2116363: Unified repository of field definitions (cache + API) ends up doing)
Comment #19
joachim commentedStill relevant, though it's the entity field manager now.
Which also means this downside (which I've removed from the IS) is no longer a problem:
> Downside is that this would add three more methods to the already convoluted EntityManagerIntrerface.
Also:
> single field/base field/field storage
Do we need all 3? A base field is BOTH a field AND a field storage. So getFieldDefinition() and getFieldStorageDefinition() would suffice. Which one you load when you're working with a base field would depend on the context.