From d960aa8e56ad0b51660d0e148ff5a67359a64f41 Mon Sep 17 00:00:00 2001 From: Brad Jones Date: Fri, 24 Nov 2017 12:11:51 -0700 Subject: [PATCH 1/2] Allow entity bundle traits to specify display and form mode settings --- commerce.module | 8 +++-- src/ConfigurableFieldManager.php | 24 +++++++++++---- src/ConfigurableFieldManagerInterface.php | 29 +++++++++++++++++-- src/EntityTraitManager.php | 6 ++++ .../Commerce/EntityTrait/EntityTraitBase.php | 7 +++++ .../EntityTrait/EntityTraitInterface.php | 19 ++++++++++-- 6 files changed, 79 insertions(+), 14 deletions(-) diff --git a/commerce.module b/commerce.module index 1c367a58cd..056628f210 100644 --- a/commerce.module +++ b/commerce.module @@ -80,6 +80,8 @@ function commerce_field_widget_form_alter(&$element, FormStateInterface $form_st * The bundle. * @param string $display_context * The display context ('view' or 'form'). + * @param string $mode + * The display mode, defaults to 'default' * * @throws \InvalidArgumentException * Thrown when an invalid display context is provided. @@ -87,18 +89,18 @@ function commerce_field_widget_form_alter(&$element, FormStateInterface $form_st * @return \Drupal\Core\Entity\Display\EntityDisplayInterface * The entity display. */ -function commerce_get_entity_display($entity_type, $bundle, $display_context) { +function commerce_get_entity_display($entity_type, $bundle, $display_context, $mode = 'default') { if (!in_array($display_context, ['view', 'form'])) { throw new \InvalidArgumentException(sprintf('Invalid display_context %s passed to _commerce_product_get_display().', $display_context)); } $storage = \Drupal::entityTypeManager()->getStorage('entity_' . $display_context . '_display'); - $display = $storage->load($entity_type . '.' . $bundle . '.default'); + $display = $storage->load($entity_type . '.' . $bundle . '.' . $mode); if (!$display) { $display = $storage->create([ 'targetEntityType' => $entity_type, 'bundle' => $bundle, - 'mode' => 'default', + 'mode' => $mode, 'status' => TRUE, ]); } diff --git a/src/ConfigurableFieldManager.php b/src/ConfigurableFieldManager.php index 7f33d69e50..669f7c669c 100644 --- a/src/ConfigurableFieldManager.php +++ b/src/ConfigurableFieldManager.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\entity\BundleFieldDefinition; class ConfigurableFieldManager implements ConfigurableFieldManagerInterface { @@ -70,16 +71,27 @@ public function createField(BundleFieldDefinition $field_definition, $lock = TRU ]); $field->save(); + $modes = []; // Show the field on default entity displays, if specified. if ($view_display_options = $field_definition->getDisplayOptions('view')) { - $view_display = commerce_get_entity_display($entity_type_id, $bundle, 'view'); - $view_display->setComponent($field_name, $view_display_options); - $view_display->save(); + $modes['view']['default'] = $view_display_options; } if ($form_display_options = $field_definition->getDisplayOptions('form')) { - $form_display = commerce_get_entity_display($entity_type_id, $bundle, 'form'); - $form_display->setComponent($field_name, $form_display_options); - $form_display->save(); + $modes['form']['default'] = $form_display_options; + } + $this->configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes); + } + + /** + * {@inheritdoc} + */ + public function configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes) { + foreach ($modes as $display => $mode) { + foreach ($mode as $name => $view_display_options) { + $view_display = commerce_get_entity_display($entity_type_id, $bundle, $display, $name); + $view_display->setComponent($field_name, $view_display_options); + $view_display->save(); + } } } diff --git a/src/ConfigurableFieldManagerInterface.php b/src/ConfigurableFieldManagerInterface.php index bf38321714..bcd48974de 100644 --- a/src/ConfigurableFieldManagerInterface.php +++ b/src/ConfigurableFieldManagerInterface.php @@ -2,6 +2,8 @@ namespace Drupal\commerce; +use Drupal\entity\BundleFieldDefinition; + /** * Manages configurable fields based on field definitions. * @@ -12,7 +14,7 @@ interface ConfigurableFieldManagerInterface { /** * Creates a configurable field from the given field definition. * - * @param \Drupal\commerce\BundleFieldDefinition $field_definition + * @param \Drupal\entity\BundleFieldDefinition $field_definition * The field definition. * @param bool $lock * Whether the created field should be locked. @@ -25,10 +27,31 @@ interface ConfigurableFieldManagerInterface { */ public function createField(BundleFieldDefinition $field_definition, $lock = TRUE); + /** + * Configure display modes for the given field definition. + * + * @param string $field_name + * The field name. + * @param string $entity_type_id + * The entity type ID. + * @param string $bundle + * The bundle. + * @param array $modes + * The display mode configuration, keyed by display type, then mode. + * Display type is one of 'form' or 'view', with their values being arrays + * keyed by display mode ID. The display modes are created if they do not + * already exist. + * + * @throws \InvalidArgumentException + * Thrown when given an incomplete field definition (missing name, + * target entity type ID, or target bundle). + */ + public function configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $modes); + /** * Deletes the configurable field created from the given field definition. * - * @param \Drupal\commerce\BundleFieldDefinition $field_definition + * @param \Drupal\entity\BundleFieldDefinition $field_definition * The field definition. * * @throws \InvalidArgumentException @@ -42,7 +65,7 @@ public function deleteField(BundleFieldDefinition $field_definition); /** * Checks whether the configurable field has data. * - * @param \Drupal\commerce\BundleFieldDefinition $field_definition + * @param \Drupal\entity\BundleFieldDefinition $field_definition * The field definition. * * @return bool diff --git a/src/EntityTraitManager.php b/src/EntityTraitManager.php index 53ccf4be5a..191e9e3571 100644 --- a/src/EntityTraitManager.php +++ b/src/EntityTraitManager.php @@ -101,6 +101,12 @@ public function installTrait(EntityTraitInterface $trait, $entity_type_id, $bund $field_definition->setName($field_name); $this->configurableFieldManager->createField($field_definition); + + } + // Traits may also pass mode definitions for fields they did not contribute. + foreach ($trait->buildDisplayModes() as $field_name => $definition) { + $this->configurableFieldManager + ->configureFieldDisplayModes($field_name, $entity_type_id, $bundle, $definition); } } diff --git a/src/Plugin/Commerce/EntityTrait/EntityTraitBase.php b/src/Plugin/Commerce/EntityTrait/EntityTraitBase.php index f3b9687952..077d00595e 100644 --- a/src/Plugin/Commerce/EntityTrait/EntityTraitBase.php +++ b/src/Plugin/Commerce/EntityTrait/EntityTraitBase.php @@ -30,4 +30,11 @@ public function buildFieldDefinitions() { // Entity traits are not required to provide fields. } + /** + * {@inheritdoc} + */ + public function buildDisplayModes() { + // Entity traits are not required to provide additional display modes. + } + } diff --git a/src/Plugin/Commerce/EntityTrait/EntityTraitInterface.php b/src/Plugin/Commerce/EntityTrait/EntityTraitInterface.php index 144ecb95c3..f6885942e9 100644 --- a/src/Plugin/Commerce/EntityTrait/EntityTraitInterface.php +++ b/src/Plugin/Commerce/EntityTrait/EntityTraitInterface.php @@ -35,12 +35,27 @@ public function getEntityTypeIds(); /** * Builds the field definitions. * - * THe provided field definitions will be created as configurable + * The provided field definitions will be created as configurable * fields when the entity trait is installed for an entity type/bundle. * - * @return \Drupal\commerce\BundleFieldDefinition[] + * @return \Drupal\entity\BundleFieldDefinition[] * An array of field definitions, keyed by field name. */ public function buildFieldDefinitions(); + /** + * Builds display mode settings for non-default modes. + * + * Display mode settings for default form and displays should be set + * using BundleFieldDefinition::setDisplayOptions() and are processed by + * ConfigurableFieldManager::createField(). To configure additional display + * and form modes, return their configuration here. (Specifying default + * settings here will overwrite the config from the field definition.) + * + * @return array + * The display mode configuration, keyed by field name, values described in + * ConfigurableFieldManagerInterface::configureFieldDisplayModes(). + */ + public function buildDisplayModes(); + } From e319c854f51788256449ef853dc6126e83120636 Mon Sep 17 00:00:00 2001 From: Brad Jones Date: Sat, 25 Nov 2017 18:01:31 -0700 Subject: [PATCH 2/2] Return empty arrays in base implementation to be consistent with interface --- src/Plugin/Commerce/EntityTrait/EntityTraitBase.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Plugin/Commerce/EntityTrait/EntityTraitBase.php b/src/Plugin/Commerce/EntityTrait/EntityTraitBase.php index 077d00595e..0f8e0ac44f 100644 --- a/src/Plugin/Commerce/EntityTrait/EntityTraitBase.php +++ b/src/Plugin/Commerce/EntityTrait/EntityTraitBase.php @@ -28,6 +28,7 @@ public function getEntityTypeIds() { */ public function buildFieldDefinitions() { // Entity traits are not required to provide fields. + return []; } /** @@ -35,6 +36,7 @@ public function buildFieldDefinitions() { */ public function buildDisplayModes() { // Entity traits are not required to provide additional display modes. + return []; } }