File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 */
You can’t perform that action at this time.
0 commit comments