Commit 0990c7ee authored by webchick's avatar webchick

Issue #1982776 by netsensei, Dave Reid, plopesc: Formatters do not support weights like widgets do.

parent 82bb0f33
......@@ -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;
}
......@@ -167,13 +167,15 @@ public function prepareConfiguration($field_type, array $configuration) {
*/
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'];
}
}
}
......
......@@ -22,7 +22,8 @@
* },
* settings = {
* "test_formatter_setting" = "dummy test string"
* }
* },
* weight = 1
* )
*/
class TestFieldDefaultFormatter extends FormatterBase {
......
......@@ -21,7 +21,8 @@
* },
* settings = {
* "test_empty_string" = "**EMPTY FIELD**"
* }
* },
* weight = -5
* )
*/
class TestFieldEmptyFormatter extends FormatterBase {
......
......@@ -21,7 +21,8 @@
* },
* settings = {
* "field_empty_setting" = ""
* }
* },
* weight = -1
* )
*/
class TestFieldEmptySettingFormatter extends FormatterBase {
......
......@@ -22,7 +22,8 @@
* },
* settings = {
* "test_formatter_setting_multiple" = "dummy test string"
* }
* },
* weight = 5
* )
*/
class TestFieldMultipleFormatter extends FormatterBase {
......
......@@ -19,6 +19,7 @@
* field_types = {
* "test_field",
* },
* weight = -10
* )
*/
class TestFieldNoSettingsFormatter extends FormatterBase {
......
......@@ -22,7 +22,8 @@
* },
* settings = {
* "test_formatter_setting_additional" = "dummy test string"
* }
* },
* weight = 10
* )
*/
class TestFieldPrepareViewFormatter extends FormatterBase {
......
......@@ -23,7 +23,8 @@
* },
* settings = {
* "test_widget_setting" = "dummy test string"
* }
* },
* weight = -10
* )
*/
class TestFieldWidget extends WidgetBase {
......
......@@ -25,7 +25,8 @@
* settings = {
* "test_widget_setting_multiple" = "dummy test string"
* },
* multiple_values = TRUE
* multiple_values = TRUE,
* weight = 10
* )
*/
class TestFieldWidgetMultiple extends WidgetBase {
......
......@@ -51,12 +51,28 @@ function testFormatterUI() {
$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 @@ public function testWidgetUI() {
$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')));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment