Skip to content

Commit 40f94d2

Browse files
jsacksickbojanz
authored andcommitted
Issue #2939008 by jsacksick: There is no formatter for the commerce_plugin_item field type
1 parent 5d194b0 commit 40f94d2

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

commerce.module

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ function commerce_field_widget_info_alter(array &$info) {
3535
}
3636
}
3737

38+
/**
39+
* Implements hook_field_formatter_info_alter().
40+
*
41+
* Exposes the commerce_plugin_item_default formatter for each of the field
42+
* type's derivatives, since core does not do it automatically.
43+
*/
44+
function commerce_field_formatter_info_alter(array &$info) {
45+
if (isset($info['commerce_plugin_item_default'])) {
46+
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
47+
foreach ($field_type_manager->getDefinitions() as $key => $definition) {
48+
if ($definition['id'] == 'commerce_plugin_item') {
49+
$info['commerce_plugin_item_default']['field_types'][] = $key;
50+
}
51+
}
52+
}
53+
}
54+
3855
/**
3956
* Implements hook_field_widget_form_alter().
4057
*
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Drupal\commerce\Plugin\Field\FieldFormatter;
4+
5+
use Drupal\Core\Field\FieldItemListInterface;
6+
use Drupal\Core\Field\FormatterBase;
7+
8+
/**
9+
* Plugin implementation of the 'commerce_plugin_item_default' formatter.
10+
*
11+
* @FieldFormatter(
12+
* id = "commerce_plugin_item_default",
13+
* label = @Translation("Default"),
14+
* field_types = {
15+
* "commerce_plugin_item"
16+
* }
17+
* )
18+
*/
19+
class PluginItemDefaultFormatter extends FormatterBase {
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function viewElements(FieldItemListInterface $items, $langcode) {
25+
$elements = [];
26+
foreach ($items as $delta => $item) {
27+
$target_definition = $item->getTargetDefinition();
28+
if (!empty($target_definition['label'])) {
29+
$elements[$delta] = [
30+
'#markup' => $target_definition['label'],
31+
];
32+
}
33+
else {
34+
$elements[$delta] = [
35+
'#markup' => $target_definition['id'],
36+
];
37+
}
38+
}
39+
40+
return $elements;
41+
}
42+
43+
}

src/Plugin/Field/FieldType/PluginItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* description = @Translation("Stores configuration for a plugin."),
1717
* category = @Translation("Commerce"),
1818
* default_widget = "commerce_plugin_select",
19-
* default_formatter = "string",
19+
* default_formatter = "commerce_plugin_item_default",
2020
* deriver = "\Drupal\commerce\Plugin\Field\FieldType\PluginItemDeriver"
2121
* )
2222
*/

0 commit comments

Comments
 (0)