PHP code example of gorriecoe / silverstripe-linkfield

1. Go to this page and download the library: Download gorriecoe/silverstripe-linkfield 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/ */

    

gorriecoe / silverstripe-linkfield example snippets




use gorriecoe\Link\Models\Link;
use gorriecoe\LinkField\LinkField;

class MyClass extends DataObject
{
    private static $has_one = [
        'Button' => Link::class
    ];

    private static $many_many = [
        'Buttons' => Link::class
    ];

    private static $many_many_extraFields = [
        'Buttons' => [
            'Sort' => 'Int' // Required for all many_many relationships
        ]
    ];

    /**
     * CMS Fields
     * @return FieldList
     */
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldsToTab(
            'Root.Main',
            [
                LinkField::create(
                    'Button',
                    'Button',
                    $this
                ),
                LinkField::create(
                    'Buttons',
                    'Buttons',
                    $this
                )
            ]
        );
        return $fields;
    }
}

// Allow only SiteTree and URL types, implicitly allow displaying title field.
$linkConfig = [
    'types' => [
        'SiteTree',
        'URL',
    ],
];
LinkField::create('FieldName', 'Field Title', $this, $linkConfig);

// Allow only SiteTree and URL types.
$linkConfig = [
    'types' => [
        'SiteTree' => TRUE,
        'URL' => TRUE,
        'Email' => FALSE,
        'Phone' => FALSE,
        'File' => FALSE,
    ],
];
LinkField::create('FieldName', 'Field Title', $this, $linkConfig);

$linkConfig = [
    'title_display' => false,
];
LinkField::create('FieldName', 'Field Title', $this, $linkConfig);

$linkField = LinkField::create('FieldName', 'Field Title', $this);
$linkConfig = [
    'types' => [
        'SiteTree',
        'URL',
    ],
];
$linkField->setLinkConfig($linkConfig);

LinkField::create(
    'Buttons',
    'Buttons',
    $this
)->setSortColumn('SortOrder')