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

tstoeckler’s picture

Copying 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

Undefined index 'foo' on line 123

is quite easily debuggable.

@Berdir's response:

Well, yes and no, the thing is that we're talking about objects, so you wouldn't see that notice.. you'd see a fatal error method call on non-object on the next line, which is already a bit harder to debug, especially when you combine it with dynamic field names and so on.

Re @Berdir: Can you elaborate? AFAIK EntityManger::getFieldDefinitions() returns a simple array. So:

// If this contains 'title' and 'description' ...
$definitions = $entity_manager->getFieldDefinitions($entity_type, $bundle);

// ... this will result in an e.g. "Undefined index 'body'" error.
$definition = $entity_manager->getFieldDefinitions($entity_type, $bundle)[$field_name];

// What am I missing?
berdir’s picture

getFieldDefinitions() 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:

if ($definition->getType() == 'string') {
  // do something.
}

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.

tstoeckler’s picture

Ahh, I see. Thanks for that explanation! Yes, that's actually a very fair and good argument for the dedicated methods.

fago’s picture

#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,

if ($definition->getType() == 'string') {

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?

berdir’s picture

Yeah. 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).

yched’s picture

Yeah, 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)

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

Title: Add methods to get a single field/base field/field storage definition to the entity manager » Add methods to get a single field/base field/field storage definition to the entity field manager
Version: 9.5.x-dev » 10.1.x-dev
Issue summary: View changes

Still 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.