summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2014-03-07 21:59:19 (GMT)
committerwebchick2014-03-07 21:59:19 (GMT)
commit0990c7eee901eac14b3e37285c66fb8eaee305f4 (patch)
treee0fd8c38b184f81463ae7a102c5ba665ec251a48
parent82bb0f33fff1464c3b4ce69a9f2ef3465126a9ae (diff)
Issue #1982776 by netsensei, Dave Reid, plopesc: Formatters do not support weights like widgets do.
-rw-r--r--core/lib/Drupal/Core/Field/Annotation/FieldFormatter.php9
-rw-r--r--core/lib/Drupal/Core/Field/FormatterPluginManager.php10
-rw-r--r--core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldDefaultFormatter.php3
-rw-r--r--core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptyFormatter.php3
-rw-r--r--core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptySettingFormatter.php3
-rw-r--r--core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php3
-rw-r--r--core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldNoSettingsFormatter.php1
-rw-r--r--core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldPrepareViewFormatter.php3
-rw-r--r--core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidget.php3
-rw-r--r--core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidgetMultiple.php3
-rw-r--r--core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php32
11 files changed, 60 insertions, 13 deletions
diff --git a/core/lib/Drupal/Core/Field/Annotation/FieldFormatter.php b/core/lib/Drupal/Core/Field/Annotation/FieldFormatter.php
index 11f2246..fc4a4c4 100644
--- a/core/lib/Drupal/Core/Field/Annotation/FieldFormatter.php
+++ b/core/lib/Drupal/Core/Field/Annotation/FieldFormatter.php
@@ -74,4 +74,13 @@ class FieldFormatter extends Plugin {
*/
public $settings = array();
+ /**
+ * An integer to determine the weight of this formatter relative to other
+ * formatter in the Field UI when selecting a formatter for a given field
+ * instance.
+ *
+ * @var int optional
+ */
+ public $weight = NULL;
+
}
diff --git a/core/lib/Drupal/Core/Field/FormatterPluginManager.php b/core/lib/Drupal/Core/Field/FormatterPluginManager.php
index b825272..d81ff57 100644
--- a/core/lib/Drupal/Core/Field/FormatterPluginManager.php
+++ b/core/lib/Drupal/Core/Field/FormatterPluginManager.php
@@ -167,13 +167,15 @@ class FormatterPluginManager extends DefaultPluginManager {
*/
public function getOptions($field_type = NULL) {
if (!isset($this->formatterOptions)) {
- $field_types = $this->fieldTypeManager->getDefinitions();
$options = array();
- foreach ($this->getDefinitions() as $name => $formatter) {
- foreach ($formatter['field_types'] as $formatter_field_type) {
+ $field_types = $this->fieldTypeManager->getDefinitions();
+ $formatter_types = $this->getDefinitions();
+ uasort($formatter_types, array('Drupal\Component\Utility\SortArray', 'sortByWeightElement'));
+ foreach ($formatter_types as $name => $formatter_type) {
+ foreach ($formatter_type['field_types'] as $formatter_field_type) {
// Check that the field type exists.
if (isset($field_types[$formatter_field_type])) {
- $options[$formatter_field_type][$name] = $formatter['label'];
+ $options[$formatter_field_type][$name] = $formatter_type['label'];
}
}
}
diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldDefaultFormatter.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldDefaultFormatter.php
index 457c76f..97beb5c 100644
--- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldDefaultFormatter.php
+++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldDefaultFormatter.php
@@ -22,7 +22,8 @@ use Drupal\Core\Field\FieldItemListInterface;
* },
* settings = {
* "test_formatter_setting" = "dummy test string"
- * }
+ * },
+ * weight = 1
* )
*/
class TestFieldDefaultFormatter extends FormatterBase {
diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptyFormatter.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptyFormatter.php
index 592a958..e624bf9 100644
--- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptyFormatter.php
+++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptyFormatter.php
@@ -21,7 +21,8 @@ use Drupal\Core\Field\FormatterBase;
* },
* settings = {
* "test_empty_string" = "**EMPTY FIELD**"
- * }
+ * },
+ * weight = -5
* )
*/
class TestFieldEmptyFormatter extends FormatterBase {
diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptySettingFormatter.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptySettingFormatter.php
index e825aa8..fa4d9a9 100644
--- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptySettingFormatter.php
+++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldEmptySettingFormatter.php
@@ -21,7 +21,8 @@ use Drupal\Core\Field\FieldItemListInterface;
* },
* settings = {
* "field_empty_setting" = ""
- * }
+ * },
+ * weight = -1
* )
*/
class TestFieldEmptySettingFormatter extends FormatterBase {
diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php
index 588ad66..87d9553 100644
--- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php
+++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldMultipleFormatter.php
@@ -22,7 +22,8 @@ use Drupal\Core\Field\FieldItemListInterface;
* },
* settings = {
* "test_formatter_setting_multiple" = "dummy test string"
- * }
+ * },
+ * weight = 5
* )
*/
class TestFieldMultipleFormatter extends FormatterBase {
diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldNoSettingsFormatter.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldNoSettingsFormatter.php
index 1d3d150..4ab58f1 100644
--- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldNoSettingsFormatter.php
+++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldNoSettingsFormatter.php
@@ -19,6 +19,7 @@ use Drupal\Core\Field\FormatterBase;
* field_types = {
* "test_field",
* },
+ * weight = -10
* )
*/
class TestFieldNoSettingsFormatter extends FormatterBase {
diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldPrepareViewFormatter.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldPrepareViewFormatter.php
index a8ff2a9..85b97f1 100644
--- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldPrepareViewFormatter.php
+++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldFormatter/TestFieldPrepareViewFormatter.php
@@ -22,7 +22,8 @@ use Drupal\Core\Field\FieldItemListInterface;
* },
* settings = {
* "test_formatter_setting_additional" = "dummy test string"
- * }
+ * },
+ * weight = 10
* )
*/
class TestFieldPrepareViewFormatter extends FormatterBase {
diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidget.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidget.php
index bf6e093..ae80042 100644
--- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidget.php
+++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidget.php
@@ -23,7 +23,8 @@ use Symfony\Component\Validator\ConstraintViolationInterface;
* },
* settings = {
* "test_widget_setting" = "dummy test string"
- * }
+ * },
+ * weight = -10
* )
*/
class TestFieldWidget extends WidgetBase {
diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidgetMultiple.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidgetMultiple.php
index be8fed9..a16eb3f 100644
--- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidgetMultiple.php
+++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/Field/FieldWidget/TestFieldWidgetMultiple.php
@@ -25,7 +25,8 @@ use Symfony\Component\Validator\ConstraintViolationInterface;
* settings = {
* "test_widget_setting_multiple" = "dummy test string"
* },
- * multiple_values = TRUE
+ * multiple_values = TRUE,
+ * weight = 10
* )
*/
class TestFieldWidgetMultiple extends WidgetBase {
diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php
index 4a25ca6..133b578 100644
--- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php
+++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php
@@ -51,12 +51,28 @@ class ManageDisplayTest extends FieldUiTestBase {
$setting_name = key($default_settings);
$setting_value = $display_options['settings'][$setting_name];
- // Display the "Manage display" screen and check that the expected formatter is
- // selected.
+ // Display the "Manage display" screen and check that the expected formatter
+ // is selected.
$this->drupalGet($manage_display);
$this->assertFieldByName('fields[field_test][type]', $format, 'The expected formatter is selected.');
$this->assertText("$setting_name: $setting_value", 'The expected summary is displayed.');
+ // Check whether formatter weights are respected.
+ $result = $this->xpath('//select[@id=:id]/option', array(':id' => 'edit-fields-field-test-type'));
+ $options = array_map(function($item) {
+ return (string) $item->attributes()->value[0];
+ }, $result);
+ $expected_options = array (
+ 'field_no_settings',
+ 'field_empty_test',
+ 'field_empty_setting',
+ 'field_test_default',
+ 'field_test_multiple',
+ 'field_test_with_prepare_view',
+ 'hidden',
+ );
+ $this->assertEqual($options, $expected_options, 'The expected formatter ordering is respected.');
+
// Change the formatter and check that the summary is updated.
$edit = array('fields[field_test][type]' => 'field_test_multiple', 'refresh_rows' => 'field_test');
$this->drupalPostAjaxForm(NULL, $edit, array('op' => t('Refresh')));
@@ -139,6 +155,18 @@ class ManageDisplayTest extends FieldUiTestBase {
$this->assertFieldByName('fields[field_test][type]', $widget_type, 'The expected widget is selected.');
$this->assertText("$setting_name: $setting_value", 'The expected summary is displayed.');
+ // Check whether widget weights are respected.
+ $result = $this->xpath('//select[@id=:id]/option', array(':id' => 'edit-fields-field-test-type'));
+ $options = array_map(function($item) {
+ return (string) $item->attributes()->value[0];
+ }, $result);
+ $expected_options = array (
+ 'test_field_widget',
+ 'test_field_widget_multiple',
+ 'hidden',
+ );
+ $this->assertEqual($options, $expected_options, 'The expected widget ordering is respected.');
+
// Change the widget and check that the summary is updated.
$edit = array('fields[field_test][type]' => 'test_field_widget_multiple', 'refresh_rows' => 'field_test');
$this->drupalPostAjaxForm(NULL, $edit, array('op' => t('Refresh')));