PHP code example of maxime-rainville / anyfield

1. Go to this page and download the library: Download maxime-rainville/anyfield 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/ */

    

maxime-rainville / anyfield example snippets



use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\AnyField\Form\AnyField;
use SilverStripe\AnyField\Form\ManyAnyField;

class Page extends SiteTree
{
    private static array $has_one = [
        'SingleLink' => Link::class,
    ];

    private static array $has_many = [
        'ManyLinks' => Link::class,
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->addFieldsToTab(
            'Root.Main',
            [
                AnyField::create('SingleLink'),
                ManyAnyField::create('ManyLinks'),
            ]
        )

        return $fields;
    }
}

ManyAnyField::create('Pets')->setSort('Sort')


use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\AnyField\Form\AnyField;
use SilverStripe\AnyField\Form\ManyAnyField;

class Page extends SiteTree
{
    private static array $has_one = [
        'SingleLink' => Link::class,
    ];

    private static array $has_many = [
        'ManyLinks' => Link::class,
    ];

    /** Publishing the page will automatically publish those relations */
    private static $owns = [
        'SingleLink',
        'ManyLinks'
    ];

    /** The relations will be deleted when the page is deleted */
    private static $cascade_deletes = [
        'SingleLink',
        'ManyLinks'
    ];

    /** The relations will be duplicated when the page is duplicated */
    private static $cascade_duplicates = [
        'SingleLink',
        'ManyLinks'
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->addFieldsToTab(
            'Root.Main',
            [
                AnyField::create('SingleLink'),
                ManyAnyField::create('ManyLinks'),
            ]
        )

        return $fields;
    }
}

AnyField::create('MyItem', 'My Item', $this->Item)->setBaseClass(Link::class);

AnyField::create('MyItem')->setRecursivelyAddChildClass(false);

// The filed will allow you to create child classes of Link, but not a plain Link
AnyField::create('MyLink')->setBaseClass(Link::class)->addExcludedClass(Link::class);