Snippet: Drupal 8 Commerce: Show SKU in product variation inline entity form table

Before our modifications to the IEF table

Today I'd like to share a little snippet with you on how to add or remove fields from the inline entity form line items table in a product edit form.

By default that inline entity form table contains

  • Weight
  • Product variation title
  • Price
  • Status
  • Operations

What we'd like to do in our example is to

  1. Add the SKU of the product variation SKU to the table
  2. Hide the price from the table

Just create a simple custom mini module or add this snippet to your already existing custom module:

  1. /**
  2.  * Alter the fields used to represent an entity in the IEF table.
  3.  *
  4.  * @param array $fields
  5.  *   The fields, keyed by field name.
  6.  * @param array $context
  7.  *   An array with the following keys:
  8.  *   - parent_entity_type: The type of the parent entity.
  9.  *   - parent_bundle: The bundle of the parent entity.
  10.  *   - field_name: The name of the reference field on which IEF is operating.
  11.  *   - entity_type: The type of the referenced entities.
  12.  *   - allowed_bundles: Bundles allowed on the reference field.
  13.  *
  14.  * @see \Drupal\inline_entity_form\InlineFormInterface::getTableFields()
  15.  */
  16. function MYMODULE_inline_entity_form_table_fields_alter(&$fields, $context)
  17. {
  18.   /* Product variations (product edit form) IEF table display:
  19.     - Display SKU in the table
  20.     - Hide the price
  21.     */
  22.   if ($context['entity_type'] == 'commerce_product_variation') {
  23.     // Show SKU
  24.     $fields['sku'] = [
  25.       'type' => 'field',
  26.       'label' => t('SKU'),
  27.       'weight' => -1,
  28.     ];
  29.     // Hide price
  30.     unset($fields['price']);
  31.   }
  32. }

The snippets works for Drupal 8 and could perhaps work similar in Drupal 7.

Here's a screenshot of the result:
After modifications to the IEF table in Drupal Commerce products

If it helped you, please leave a comment.

AnhangGröße
After IEF table modifications, showing sku and removed title.22.61 KB

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.

Weitere Informationen über Formatierungsoptionen