
Reply to comment
Snippet: Drupal 8 Commerce: Show SKU in product variation inline entity form 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
- 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
- }
- }
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.
Attachment | Size |
---|---|
After IEF table modifications, showing sku and removed title. | 22.61 KB |
Reply
Julian Pustkuchen, M. Sc. Wirtschaftsinformatik ist passionierter Software- & Webentwickler mit den Schwerpunkten Softwaredesign, Objektorientierung sowie Usability- & Ablaufoptimierung bei webks. Ein weiterer Schwerpunkt ist die Entwicklung im CMS Drupal.
Entwickelt Julian gerade keine Software, fährt er Ski, Mountainbike, spielt Badminton, segelt oder verbringt Zeit mit Freunden.
Dieser Blog dient als sein "öffentlicher Notizblo(ck/g)".
Webbasierte Lösungen? Machen wir!
webks: websolutions kept simple
http://www.webks.de
Dir hat dieser Beitrag geholfen?
Oder du suchst weitere professionelle Unterstützung? Unser Team bietet dir professionelle Dienstleistungen im Web-Development und Drupal CMS Entwicklung!