For Drupal 7, if you provide a super advanced formatter for a field type, and your field is named 'advanced_field_formatter', because your module is listed alphabetically first, this means your formatter is listed first as the default formatter in select boxes for the field type. It is interesting that hook_field_widget_info() supports a 'weight' parameter, but not hook_field_formatter_info(). I think it should. It's an easy one-line add for Drupal 7.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Status: Active » Needs work
FileSize
1.19 KB

Patch attached for D7, needs to be re-rolled for D8.

Dave Reid’s picture

Status: Needs work » Needs review
FileSize
2.68 KB

Patch against D8.

Status: Needs review » Needs work

The last submitted patch, 1982776-field-formatter-weight.patch, failed testing.

Dave Reid’s picture

Status: Needs work » Needs review
FileSize
898 bytes

Blargh.

Dave Reid’s picture

And revised patch for D7.

haydeniv’s picture

Yeah I ran into this problem with Select (or other) #1953284: Increase module weight with random ordering of the widget list. I ended up having to increase my whole module weight got get it to drop below the other formatters. I thought it was a bit strange that I could change weight on widget but not formatter.

swentel’s picture

Status: Needs review » Reviewed & tested by the community

Makes sense, patch in #4 still applies fine.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

I think we should adds tests for this...

Dave Reid’s picture

Well there were no tests for widget weights, but I'll try to add some.

swentel’s picture

Issue tags: +Field API

Tagging

netsensei’s picture

Assigned: Unassigned » netsensei

Going to take a stab at this one.

netsensei’s picture

Status: Needs work » Needs review
FileSize
4.39 KB
2.62 KB

Okay. First attempt.

A bit new-ish at this: per Swentels' instructions:

a/ Patch with a test which should fail
b/ Patch with a test and a fix which should pass.

netsensei’s picture

Suggested by Swentel: fix comment which exceeded the 80 character line.

haydeniv’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Looks good. Patch was RTBC before. Now has passing tests. I gave it one more look so RTBC. Excited to have this so I can fix module weight issue over at Select (or Other)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php
    @@ -173,9 +173,11 @@ public function prepareConfiguration($field_type, array $configuration) {
    +      uasort($formatter_types, 'drupal_sort_weight');
    

    We should use the Drupal\Component\Utility\SortArray::sortByWeightElement() here as this is OO code and we should call into common.inc unless absolutely necessary.

  2. +++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php
    @@ -57,6 +57,13 @@ function testFormatterUI() {
    +    // Check whether formatter weights are respected
    +    $result = $this->xpath('//select[@id=:id]/option[1]', array(':id' => 'edit-fields-field-test-type'));
    +    $options = array_map(function($item) {
    +      return (string) $item->attributes()->value[0];
    +    }, $result);
    +    $this->assertEqual($options, array('field_weight_test'), 'The expected formatter ordering is respected.');
    +
         // Change the formatter and check that the summary is updated.
    

    Testing sort order by only asserting on an array of one formatter seems odd.

netsensei’s picture

Status: Needs work » Needs review
FileSize
943 bytes
4.44 KB

1.

Changed the uasort compare callback.

2.

Yes. Tests for this should be fleshed out some more. I'm new at writing good tests, so not sure how this should practically look like. This test only works on an explicit formatter case (field_weight_test) with the weight parameter set. I could test a normal default case where no weight properties are set and we assume a default ordering.

The last submitted patch, issue-1982776-ordering-weights-16.patch, failed testing.

plopesc’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
9.24 KB
10.57 KB

Re-rolling.

Instead of adding a new formatter, adding weight to existing ones to test the ordering. Also adding same tests for widgets.

Regards.

The last submitted patch, 18: formatter_weight-1982776-18-test-only.patch, failed testing.

swentel’s picture

+++ b/core/lib/Drupal/Core/Field/Annotation/FieldFormatter.php
@@ -74,4 +74,13 @@ class FieldFormatter extends Plugin {
+   * An integer to determine the weight of this formatter relative to other
+   * formatter in the Field UI when selecting a widget for a given field
+   * instance.
+   *

Hmm, this comment mixes formatters with widgets.

Other than that, this looks good to me.

plopesc’s picture

Ups, I didn't remove all the widget references... :(

swentel’s picture

swentel’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 21: formatter_weight-1982776-21.patch, failed testing.

swentel’s picture

swentel’s picture

Status: Needs work » Reviewed & tested by the community

Let's hope this one doesn't run 14 days either.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

Dave Reid’s picture

Version: 8.x-dev » 7.x-dev
Status: Fixed » Patch (to be ported)
Issue tags: +Needs backport to 7.x

Time for D7 then!

plopesc’s picture

Status: Patch (to be ported) » Needs review
Issue tags: -Needs backport to 7.x
FileSize
4.75 KB
5.78 KB

Backporting patch. Also adding tests for both widget and formatter weight.

The last submitted patch, 29: formatter_weight-1982776-29-test-only.patch, failed testing.

  • webchick committed 0990c7e on 8.3.x
    Issue #1982776 by netsensei, Dave Reid, plopesc: Formatters do not...

  • webchick committed 0990c7e on 8.3.x
    Issue #1982776 by netsensei, Dave Reid, plopesc: Formatters do not...

  • webchick committed 0990c7e on 8.4.x
    Issue #1982776 by netsensei, Dave Reid, plopesc: Formatters do not...

  • webchick committed 0990c7e on 8.4.x
    Issue #1982776 by netsensei, Dave Reid, plopesc: Formatters do not...