Skip to content

Allow entity bundle traits to specify display and form mode settings #838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions commerce.module
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,27 @@ 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.
*
* @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,
]);
}
Expand Down
24 changes: 18 additions & 6 deletions src/ConfigurableFieldManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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();
}
}
}

Expand Down
29 changes: 26 additions & 3 deletions src/ConfigurableFieldManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\commerce;

use Drupal\entity\BundleFieldDefinition;

/**
* Manages configurable fields based on field definitions.
*
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/EntityTraitManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Plugin/Commerce/EntityTrait/EntityTraitBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public function getEntityTypeIds() {
*/
public function buildFieldDefinitions() {
// Entity traits are not required to provide fields.
return [];
}

/**
* {@inheritdoc}
*/
public function buildDisplayModes() {
// Entity traits are not required to provide additional display modes.
return [];
}

}
19 changes: 17 additions & 2 deletions src/Plugin/Commerce/EntityTrait/EntityTraitInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}