Skip to content

Commit c6e1558

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

File tree

6 files changed

+69
-9
lines changed

6 files changed

+69
-9
lines changed

commerce.module

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,22 @@ 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,

src/ConfigurableFieldManager.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,33 @@ public function createField(BundleFieldDefinition $field_definition, $lock = TRU
7070
]);
7171
$field->save();
7272

73+
$modes = [];
7374
// Show the field on default entity displays, if specified.
7475
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();
76+
$modes['view']['default'] = $view_display_options;
7877
}
7978
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();
79+
$modes['form']['default'] = $view_display_options;
80+
}
81+
$this->configureFieldDisplayModes($field_definition, $modes);
82+
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
public function configureFieldDisplayModes(BundleFieldDefinition $field_definition, $modes) {
88+
$field_name = $field_definition->getName();
89+
$entity_type_id = $field_definition->getTargetEntityTypeId();
90+
$bundle = $field_definition->getTargetBundle();
91+
if (empty($field_name) || empty($entity_type_id) || empty($bundle)) {
92+
throw new \InvalidArgumentException('The passed $field_definition is incomplete.');
93+
}
94+
foreach ($modes as $display => $mode) {
95+
foreach ($mode as $name => $view_display_options) {
96+
$view_display = commerce_get_entity_display($entity_type_id, $bundle, $display, $name);
97+
$view_display->setComponent($field_name, $view_display_options);
98+
$view_display->save();
99+
}
83100
}
84101
}
85102

src/ConfigurableFieldManagerInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ interface ConfigurableFieldManagerInterface {
2525
*/
2626
public function createField(BundleFieldDefinition $field_definition, $lock = TRUE);
2727

28+
/**
29+
* Configure display modes for the given field definition.
30+
*
31+
* @param \Drupal\entity\BundleFieldDefinition $field_definition
32+
* The field definition.
33+
* @param $modes array
34+
* The display mode configuration, keyed by display type, then mode.
35+
* Display type is one of 'form' or 'view', with their values being arrays
36+
* keyed by display mode ID. The display modes are created if they do not
37+
* already exist.
38+
*
39+
* @throws \InvalidArgumentException
40+
* Thrown when given an incomplete field definition (missing name,
41+
* target entity type ID, or target bundle).
42+
*/
43+
public function configureFieldDisplayModes(BundleFieldDefinition $field_definition, $modes);
44+
2845
/**
2946
* Deletes the configurable field created from the given field definition.
3047
*

src/EntityTraitManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,17 @@ public function detectConflicts(EntityTraitInterface $trait, array $installed_tr
9595
public function installTrait(EntityTraitInterface $trait, $entity_type_id, $bundle) {
9696
// The fields provided by an entity trait are maintained as locked
9797
// configurable fields, for simplicity.
98+
$modes = $trait->buildDisplayModes();
9899
foreach ($trait->buildFieldDefinitions() as $field_name => $field_definition) {
99100
$field_definition->setTargetEntityTypeId($entity_type_id);
100101
$field_definition->setTargetBundle($bundle);
101102
$field_definition->setName($field_name);
102103

103104
$this->configurableFieldManager->createField($field_definition);
105+
if (!empty($modes[$field_name])) {
106+
$this->configurableFieldManager
107+
->configureFieldDisplayModes($field_definition, $modes[$field_name]);
108+
}
104109
}
105110
}
106111

src/Plugin/Commerce/EntityTrait/EntityTraitBase.php

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

33+
public function buildDisplayModes() {
34+
// Entity traits are not required to provide additional display modes.
35+
}
36+
3337
}

src/Plugin/Commerce/EntityTrait/EntityTraitInterface.php

Lines changed: 16 additions & 1 deletion
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
*
4141
* @return \Drupal\commerce\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)