Skip to content

Commit 2336282

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

6 files changed

+82
-14
lines changed

commerce.module

+5-3
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

+20-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace Drupal\commerce;
44

55
use Drupal\Core\Entity\EntityTypeManagerInterface;
6+
use Drupal\Core\Field\FieldDefinitionInterface;
67
use Drupal\field\Entity\FieldConfig;
78
use Drupal\field\Entity\FieldStorageConfig;
9+
use Drupal\entity\BundleFieldDefinition;
10+
use Drupal\Tests\field\Kernel\FieldDefinitionIntegrityTest;
811

912
class ConfigurableFieldManager implements ConfigurableFieldManagerInterface {
1013

@@ -70,16 +73,27 @@ public function createField(BundleFieldDefinition $field_definition, $lock = TRU
7073
]);
7174
$field->save();
7275

76+
$modes = [];
7377
// Show the field on default entity displays, if specified.
7478
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();
79+
$modes['view']['default'] = $view_display_options;
7880
}
7981
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();
82+
$modes['form']['default'] = $form_display_options;
83+
}
84+
$this->configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes);
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes) {
91+
foreach ($modes as $display => $mode) {
92+
foreach ($mode as $name => $view_display_options) {
93+
$view_display = commerce_get_entity_display($entity_type_id, $bundle, $display, $name);
94+
$view_display->setComponent($field_name, $view_display_options);
95+
$view_display->save();
96+
}
8397
}
8498
}
8599

src/ConfigurableFieldManagerInterface.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Drupal\commerce;
44

5+
use Drupal\Core\Field\FieldDefinitionInterface;
6+
use Drupal\entity\BundleFieldDefinition;
7+
58
/**
69
* Manages configurable fields based on field definitions.
710
*
@@ -12,7 +15,7 @@ interface ConfigurableFieldManagerInterface {
1215
/**
1316
* Creates a configurable field from the given field definition.
1417
*
15-
* @param \Drupal\commerce\BundleFieldDefinition $field_definition
18+
* @param \Drupal\entity\BundleFieldDefinition $field_definition
1619
* The field definition.
1720
* @param bool $lock
1821
* Whether the created field should be locked.
@@ -25,10 +28,31 @@ interface ConfigurableFieldManagerInterface {
2528
*/
2629
public function createField(BundleFieldDefinition $field_definition, $lock = TRUE);
2730

31+
/**
32+
* Configure display modes for the given field definition.
33+
*
34+
* @param string $field_name
35+
* The field name.
36+
* @param string $entity_type_id
37+
* The entity type ID.
38+
* @param string $bundle
39+
* The bundle.
40+
* @param array $modes
41+
* The display mode configuration, keyed by display type, then mode.
42+
* Display type is one of 'form' or 'view', with their values being arrays
43+
* keyed by display mode ID. The display modes are created if they do not
44+
* already exist.
45+
*
46+
* @throws \InvalidArgumentException
47+
* Thrown when given an incomplete field definition (missing name,
48+
* target entity type ID, or target bundle).
49+
*/
50+
public function configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes);
51+
2852
/**
2953
* Deletes the configurable field created from the given field definition.
3054
*
31-
* @param \Drupal\commerce\BundleFieldDefinition $field_definition
55+
* @param \Drupal\entity\BundleFieldDefinition $field_definition
3256
* The field definition.
3357
*
3458
* @throws \InvalidArgumentException
@@ -42,7 +66,7 @@ public function deleteField(BundleFieldDefinition $field_definition);
4266
/**
4367
* Checks whether the configurable field has data.
4468
*
45-
* @param \Drupal\commerce\BundleFieldDefinition $field_definition
69+
* @param \Drupal\entity\BundleFieldDefinition $field_definition
4670
* The field definition.
4771
*
4872
* @return bool

src/EntityTraitManager.php

+6
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

+7
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

+17-2
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)