many many extra fields on summary fields

Silverstripe Version:
4.7

Question:
I’ve a many many relation from “Invoice” to “Product”.
On “Invoice” i’ve defined the many many extra fields “PurchasedPrice”, “Quantity” and “PriceTotal”.

Now how can i show these entries on the product summary fields when i’m on a invoice object in the products tab?

Details of your query go here

class Invoice extends DataObject
{
    private static $db = [
        "Title" => "Varchar",
        "LastReminder" => "Date",
        "Deadline" => "Date",
        "Total" => "Currency",
        "IsPaid" => "Boolean",
    ];

    private static $has_one = [
        "Member" => Member::class,
    ];

    private static $many_many = [
        "Products" => Product::class,
    ];

    private static $many_many_extraFields = [
        "Products" => [
            "PurchasedPrice" => "Currency",
            "Quantity" => "Int",
            "PriceTotal" => "Currency",
        ],
    ];

    private static $summary_fields = [
        "Created.Nice" => "Created",
        "Member.Name" => "For Member",
        "Title" => "Title",
        "Total" => "Total Price",
        "IsPaid" => "Is paid?",
    ];
....
}
class Product extends DataObject
{
    private static $db = [
        "Name" => "Varchar",
        "Price" => "Currency",
        "IsActive" => "Boolean",
    ];

    private static $has_one = [
        "Category" => Category::class,
        "Image" => Image::class,
    ];

    private static $belongs_many_many = [
        "Invoices" => Invoice::class,
    ];

    private static $summary_fields = array(
        "IsActive" => "Is active?",
        "Name" => "Name",
        "Category.Title" => "Category",
        "Price" => "Price",
    );
.....
}

To only show those extra fields, you should configure the GridFieldDataColumns component, e.g.:

$grid->getConfig()->getComponentByType(GridFieldDataColumns::class)
     ->setDisplayFields([
        'IsActive'       => 'Is active?',
        'Name'           => 'Name',
        'Category.Title' => 'Category',
        'Price'          => 'Price',
        'PurchasedPrice' => 'Purchased price',
        'Quantity'       => 'Quantity',
        'PriceTotal'     => 'Price total',
     ]);

To be able to change those fields in the product form, you should configure the GridFieldDetailForm component (se the relevant developer guide), e.g.:

// Customize your product fields
$product_fields = singleton('Product')->getCMSFields();
$product_fields->addFieldsToTab('Root.Main', [
    CurrencyField::create('ManyMany[PurchasedPrice]', 'Purchased price'),
    NumericField::create('ManyMany[Quantity]', 'Quantity'),
    CurrencyField::create('ManyMany[PriceTotal]', 'Price total'),
]);
$grid->getConfig()->getComponentByType(GridFieldDetailForm::class)
     ->setFields($product_fields);

I personally see that, more often than not, a better approach would be to allow inline editing for the extra fields only. In these cases, you can leverage GridFieldEditableColumns component provided by the GridField extensions module.