Problem/Motivation

While adding a UUID field in a view, no field formatter available, but the input element of selecting formatter is there still. It might make users like me wonder is it something wrong with this field if without a formatter available for it.

By checking code

/**
 * Defines the 'uuid' entity field type.
 *
 * The field uses a newly generated UUID as default value.
 *
 * @FieldType(
 *   id = "uuid",
 *   label = @Translation("UUID"),
 *   description = @Translation("An entity field containing a UUID."),
 *   no_ui = TRUE,
 *   default_formatter = "string"
 * )
 */

Found the string formatter is its default formatter. Further check the string formatter.

/**
 * Plugin implementation of the 'string' formatter.
 *
 * @FieldFormatter(
 *   id = "string",
 *   label = @Translation("Plain text"),
 *   field_types = {
 *     "string",
 *     "uri",
 *   },
 *   quickedit = {
 *     "editor" = "plain_text"
 *   }
 * )
 */

It does not support uuid field type explicitly.

Proposed resolution

There is a uuid_extra contributed module which provides a uuid field formatter. But IMO, it would be better to do it in core by adjusting the annotation of string formatter a little bit, to support uuid field type.

 *   field_types = {
 *     "string",
 *     "uri",
 *   },

to

 *   field_types = {
 *     "string",
 *     "uri",
 *     "uuid",
 *   },
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jungle created an issue. See original summary.

jungle’s picture

Assigned: jungle » Unassigned
Status: Active » Needs review
FileSize
513 bytes
jungle’s picture

Version: 8.8.x-dev » 8.9.x-dev
alexpott’s picture

Title: Let StringFormatter support UUID filed type explicitly » Let StringFormatter support UUID field type explicitly
Category: Feature request » Task
Status: Needs review » Needs work

I think this change makes sense. It also makes me wonder what is the use case of having a FieldType with a default_formatter where the formatter chosen doesn't support said field type.

I think

  • it's worth adding a test to prove we don;t break this in the future.
  • it's worth considering triggering an error when a FieldType defines a default formatter that it can't use.
jungle’s picture

Submit a patch and look for further amending instructions

jungle’s picture

jungle’s picture

jungle’s picture

jungle’s picture

Status: Needs work » Needs review

Addressed @alexpott's concerns in #4

alexpott’s picture

+++ b/core/lib/Drupal/Core/Field/FormatterPluginManager.php
@@ -183,7 +183,11 @@ public function getOptions($field_type = NULL) {
+      if (!empty($this->formatterOptions[$field_type])) {
+        return $this->formatterOptions[$field_type];
+      }
+      @trigger_error("No default field formatter available to field type: $field_type or claimed default formatter does not support it. See https://www.drupal.org/project/drupal/issues/3112256.");
+      return [];

It's great that this was the only case of this in core. Nice proof. Let's move this code to a follow-up so we can decide the best thing to do here. It might be to silently fix this or trigger an exception at some point in the future.

alexpott’s picture

Note this problem is also fixed in contrib - see https://www.drupal.org/project/uuid_extra

wengerk’s picture

Everything from #4 (test to prove we don't break this in the future & triggering an error when a FieldType defines a default formatter).

IMO it worth moving the code triggering the error into a sub-issues instead of here - to prevent muti-scoop issues.

PS: I just spotted 2 code quality flaws with extra spaces at the beginning and end of the ::testUuidField method - but that's pretty trivial.

Edit: just saw the message from @alexpott right now ... sorry for the same idea/message twice.

jungle’s picture

  • Re #12 Extra blank lines removed (Confired with @wengerk over slack, he meant blank lines, not spaces ), but I think it's fine to allow them.
  • Removed the code triggering the error.

Re #11 I do not think uuid_extra fixed this problem. With the UUID formatter provided by the module, this problem is hidden, it's still unable to select the default formatter - the string formater.

Thanks @alexpott and @wengerk!

jungle’s picture

jungle’s picture

+++ b/core/modules/views_ui/tests/src/FunctionalJavascript/DisplayTest.php
@@ -152,4 +152,29 @@ public function testAjaxRebuild() {
+   * Test UUID field's default field formatter.

Test ... should be Tests ... probably

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

gaele’s picture

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.

gilmord’s picture

Patch works for me on 9.4.2

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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.

fskreuz’s picture

Re-roll against 9.5.x.

_utsavsharma’s picture

Tried to fix CCF for #24.
Please review.

Status: Needs review » Needs work

The last submitted patch, 25: 3112256-25.patch, failed testing. View results

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.