1
votes

I'm setting up in a project the many_many_extraFields relation between some DataObjects, following the official doc.

With the following code:

<?php
// Definizione Namespace
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Security\Permission;

/**
 * Classe Sconto catalogo e prodotto
 */
class Sconto extends DataObject
{
    [...]
     private static $many_many = [
        'Regole' => 'Regola'
    ];
    private static $many_many_extraFields = [
        'Regole' => [
            'Area' => 'Varchar',
            'Tipologia' => 'Varchar',
            'Prezzo' => 'Currency'
        ]
    ];
    [...]

    /**
     * Metodo gestione campi CMS
     * Setter
     * @return FieldList $fields Campi Back-End
     */
    public function getCMSfields()
    {
        $fields = parent::getCMSFields();
        [...]
        // Fetching campi Regole
        $regolaFields = singleton('Regola')->getCMSfields();

        // Aggiunta campi specifici Regole
        $regolaFields->addFieldsToTab('Root.Regola', array(
            DropdownField::create('ManyMany[Area]', 'Area di applicazione', array(
                'Prodotto' => 'Prodotto'
            ))->setEmptyString('Applica a'),
            DropdownField::create('ManyMany[Tipologia]', 'Criterio di applicazione', array(
                'Prezzo' => 'Prezzo'
            ))->setEmptyString('Applica per'),
            NumericField::create('ManyMany[Prezzo]', 'Prezzo prodotto (€)')->setScale(2)->setAttribute('placeholder', 'Es. 5,00')
        ), 'Criterio');

        $configRegole = GridFieldConfig_RelationEditor::create();

        $configRegole->getComponentByType('GridFieldDetailForm')->setFields($regolaFields);

        $gridRegole = GridField::create('Regole', 'Regole', $this->Regole(), $configRegole);

        $fields->findOrMakeTab('Root.Sconto')->replaceField('Regole', $gridRegole);
        [...]
        return $fields;
    }
}

The framweork throw this exception:

[Alert] Call to a member function setFields() on null

Referencing on this line:

$configRegole->getComponentByType('GridFieldDetailForm')->setFields($regolaFields);

As you can see, I added before it the canonical instantiation - such as developer guide suggests.

I'm trying to figure out what's the cause, but I'm sure that something is missing.

Thanks in advance.

1

1 Answers

2
votes

You need to reference GridFieldComponent classes using their fully qualified class name. This also applies to any classes in SS4, and easist if you get into the habit of referencing them with ::class, e.g. Regola::class even if they don't have a namespace of their own.

Correct code:

use SilverStripe\Forms\GridField\GridFieldDetailForm;
// ...
$configRegole->getComponentByType(GridFieldDetailForm::class)->setFields($regolaFields);