PHP code example of fromholdio / silverstripe-dependentgroupeddropdownfield

1. Go to this page and download the library: Download fromholdio/silverstripe-dependentgroupeddropdownfield library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fromholdio / silverstripe-dependentgroupeddropdownfield example snippets


$typeField = DropdownField::create(
    'ProductType',
    'Product Type',
    [
        'animal' => 'Animal',
        'food' => 'Food'
    ]
);

$typeField->setEmptyString('Please select a product type');

$productFieldSource = function($value) {
    if ($value === 'animal') {
        return [
            'Fun' => [
                'puppy' => 'Puppy',
                'dog' => 'Dog'
            ],
            'Evil' => [
                'cat' => 'Cat'
            ]
        ];
    }
    if ($value === 'food') {
        return [
            'Fruit' => [
                'apple' => 'Apple',
                'orange' => 'Orange'
            ],
            'Vegetables' => [
                'carrot' => 'Carrot',
                'celery' => 'Celery'
            ]
        ];
    }
    return [];
};

$productField = DependentGroupedDropdownField::create(
    'Product',
    'Product',
    $productFieldSource
);

$productField->setDepends($typeField);

$fields->addFieldsToTab(
    'Root.Testing',
    [
        $typeField,
        $productField
    ]
);