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
- Add the SKU of the product variation SKU to the table
- Hide the price from the table
Just create a simple custom mini module or add this snippet to your already existing custom module:
/**
* Alter the fields used to represent an entity in the IEF table.
*
* @param array $fields
* The fields, keyed by field name.
* @param array $context
* An array with the following keys:
* - parent_entity_type: The type of the parent entity.
* - parent_bundle: The bundle of the parent entity.
* - field_name: The name of the reference field on which IEF is operating.
* - entity_type: The type of the referenced entities.
* - allowed_bundles: Bundles allowed on the reference field.
*
* @see \Drupal\inline_entity_form\InlineFormInterface::getTableFields()
*/
function MYMODULE_inline_entity_form_table_fields_alter(&$fields, $context)
{
/* Product variations (product edit form) IEF table display:
- Display SKU in the table
- Hide the price
*/
if ($context['entity_type'] == 'commerce_product_variation') {
// Show SKU
$fields['sku'] = [
'type' => 'field',
'label' => t('SKU'),
'weight' => -1,
];
// Hide price
unset($fields['price']);
}
}
?>
The snippets works for Drupal 8 and could perhaps work similar in Drupal 7.
Here's a screenshot of the result:
If it helped you, please leave a comment.