Skip to content

Commit d960aa8

Browse files
committed
Allow entity bundle traits to specify display and form mode settings
1 parent 0472e91 commit d960aa8

File tree

6 files changed

+79
-14
lines changed

6 files changed

+79
-14
lines changed

commerce.module

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,27 @@ function commerce_field_widget_form_alter(&$element, FormStateInterface $form_st
8080
* The bundle.
8181
* @param string $display_context
8282
* The display context ('view' or 'form').
83+
* @param string $mode
84+
* The display mode, defaults to 'default'
8385
*
8486
* @throws \InvalidArgumentException
8587
* Thrown when an invalid display context is provided.
8688
*
8789
* @return \Drupal\Core\Entity\Display\EntityDisplayInterface
8890
* The entity display.
8991
*/
90-
function commerce_get_entity_display($entity_type, $bundle, $display_context) {
92+
function commerce_get_entity_display($entity_type, $bundle, $display_context, $mode = 'default') {
9193
if (!in_array($display_context, ['view', 'form'])) {
9294
throw new \InvalidArgumentException(sprintf('Invalid display_context %s passed to _commerce_product_get_display().', $display_context));
9395
}
9496

9597
$storage = \Drupal::entityTypeManager()->getStorage('entity_' . $display_context . '_display');
96-
$display = $storage->load($entity_type . '.' . $bundle . '.default');
98+
$display = $storage->load($entity_type . '.' . $bundle . '.' . $mode);
9799
if (!$display) {
98100
$display = $storage->create([
99101
'targetEntityType' => $entity_type,
100102
'bundle' => $bundle,
101-
'mode' => 'default',
103+
'mode' => $mode,
102104
'status' => TRUE,
103105
]);
104106
}

src/ConfigurableFieldManager.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\Core\Entity\EntityTypeManagerInterface;
66
use Drupal\field\Entity\FieldConfig;
77
use Drupal\field\Entity\FieldStorageConfig;
8+
use Drupal\entity\BundleFieldDefinition;
89

910
class ConfigurableFieldManager implements ConfigurableFieldManagerInterface {
1011

@@ -70,16 +71,27 @@ public function createField(BundleFieldDefinition $field_definition, $lock = TRU
7071
]);
7172
$field->save();
7273

74+
$modes = [];
7375
// Show the field on default entity displays, if specified.
7476
if ($view_display_options = $field_definition->getDisplayOptions('view')) {
75-
$view_display = commerce_get_entity_display($entity_type_id, $bundle, 'view');
76-
$view_display->setComponent($field_name, $view_display_options);
77-
$view_display->save();
77+
$modes['view']['default'] = $view_display_options;
7878
}
7979
if ($form_display_options = $field_definition->getDisplayOptions('form')) {
80-
$form_display = commerce_get_entity_display($entity_type_id, $bundle, 'form');
81-
$form_display->setComponent($field_name, $form_display_options);
82-
$form_display->save();
80+
$modes['form']['default'] = $form_display_options;
81+
}
82+
$this->configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes);
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
public function configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes) {
89+
foreach ($modes as $display => $mode) {
90+
foreach ($mode as $name => $view_display_options) {
91+
$view_display = commerce_get_entity_display($entity_type_id, $bundle, $display, $name);
92+
$view_display->setComponent($field_name, $view_display_options);
93+
$view_display->save();
94+
}
8395
}
8496
}
8597

src/ConfigurableFieldManagerInterface.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Drupal\commerce;
44

5+
use Drupal\entity\BundleFieldDefinition;
6+
57
/**
68
* Manages configurable fields based on field definitions.
79
*
@@ -12,7 +14,7 @@ interface ConfigurableFieldManagerInterface {
1214
/**
1315
* Creates a configurable field from the given field definition.
1416
*
15-
* @param \Drupal\commerce\BundleFieldDefinition $field_definition
17+
* @param \Drupal\entity\BundleFieldDefinition $field_definition
1618
* The field definition.
1719
* @param bool $lock
1820
* Whether the created field should be locked.
@@ -25,10 +27,31 @@ interface ConfigurableFieldManagerInterface {
2527
*/
2628
public function createField(BundleFieldDefinition $field_definition, $lock = TRUE);
2729

30+
/**
31+
* Configure display modes for the given field definition.
32+
*
33+
* @param string $field_name
34+
* The field name.
35+
* @param string $entity_type_id
36+
* The entity type ID.
37+
* @param string $bundle
38+
* The bundle.
39+
* @param array $modes
40+
* The display mode configuration, keyed by display type, then mode.
41+
* Display type is one of 'form' or 'view', with their values being arrays
42+
* keyed by display mode ID. The display modes are created if they do not
43+
* already exist.
44+
*
45+
* @throws \InvalidArgumentException
46+
* Thrown when given an incomplete field definition (missing name,
47+
* target entity type ID, or target bundle).
48+
*/
49+
public function configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes);
50+
2851
/**
2952
* Deletes the configurable field created from the given field definition.
3053
*
31-
* @param \Drupal\commerce\BundleFieldDefinition $field_definition
54+
* @param \Drupal\entity\BundleFieldDefinition $field_definition
3255
* The field definition.
3356
*
3457
* @throws \InvalidArgumentException
@@ -42,7 +65,7 @@ public function deleteField(BundleFieldDefinition $field_definition);
4265
/**
4366
* Checks whether the configurable field has data.
4467
*
45-
* @param \Drupal\commerce\BundleFieldDefinition $field_definition
68+
* @param \Drupal\entity\BundleFieldDefinition $field_definition
4669
* The field definition.
4770
*
4871
* @return bool

src/EntityTraitManager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public function installTrait(EntityTraitInterface $trait, $entity_type_id, $bund
101101
$field_definition->setName($field_name);
102102

103103
$this->configurableFieldManager->createField($field_definition);
104+
105+
}
106+
// Traits may also pass mode definitions for fields they did not contribute.
107+
foreach ($trait->buildDisplayModes() as $field_name => $definition) {
108+
$this->configurableFieldManager
109+
->configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $definition);
104110
}
105111
}
106112

src/Plugin/Commerce/EntityTrait/EntityTraitBase.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ public function buildFieldDefinitions() {
3030
// Entity traits are not required to provide fields.
3131
}
3232

33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function buildDisplayModes() {
37+
// Entity traits are not required to provide additional display modes.
38+
}
39+
3340
}

src/Plugin/Commerce/EntityTrait/EntityTraitInterface.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,27 @@ public function getEntityTypeIds();
3535
/**
3636
* Builds the field definitions.
3737
*
38-
* THe provided field definitions will be created as configurable
38+
* The provided field definitions will be created as configurable
3939
* fields when the entity trait is installed for an entity type/bundle.
4040
*
41-
* @return \Drupal\commerce\BundleFieldDefinition[]
41+
* @return \Drupal\entity\BundleFieldDefinition[]
4242
* An array of field definitions, keyed by field name.
4343
*/
4444
public function buildFieldDefinitions();
4545

46+
/**
47+
* Builds display mode settings for non-default modes.
48+
*
49+
* Display mode settings for default form and displays should be set
50+
* using BundleFieldDefinition::setDisplayOptions() and are processed by
51+
* ConfigurableFieldManager::createField(). To configure additional display
52+
* and form modes, return their configuration here. (Specifying default
53+
* settings here will overwrite the config from the field definition.)
54+
*
55+
* @return array
56+
* The display mode configuration, keyed by field name, values described in
57+
* ConfigurableFieldManagerInterface::configureFieldDisplayModes().
58+
*/
59+
public function buildDisplayModes();
60+
4661
}

0 commit comments

Comments
 (0)