PHP code example of silverstripe / tagfield

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

    

silverstripe / tagfield example snippets


use SilverStripe\ORM\DataObject;

class BlogPost extends DataObject
{
    private static $many_many = [
        'BlogTags' => BlogTag::class
    ];
}

use SilverStripe\ORM\DataObject;

class BlogTag extends DataObject
{
    private static $db = [
        'Title' => 'Varchar(200)',
    ];

    private static $belongs_many_many = [
        'BlogPosts' => BlogPost::class
    ];
}

$field = TagField::create(
    'BlogTags',
    'Blog Tags',
    BlogTag::get(),
    $this->BlogTags()
)
    ->setShouldLazyLoad(true) // tags should be lazy loaded
    ->setCanCreate(true);     // new tag DataObjects can be created

class BlogCategory extends DataObject
{
    private static $db = [
        'Title' => 'Varchar(200)',
    ];
}

use SilverStripe\ORM\DataObject;

class BlogPost extends DataObject
{
    private static $has_one = [
        'BlogCategory' => BlogCategory::class
    ];
}

$field = TagField::create(
    'BlogCategoryID',
    $this->fieldLabel('BlogCategory'),
    BlogCategory::get()
)
    ->setIsMultiple(false)
    ->setCanCreate(true);

use SilverStripe\ORM\DataObject;

class BlogPost extends DataObject
{
    private static $db = [
        'Tags' => 'Text',
    ];
}

$field = StringTagField::create(
    'Tags',
    'Tags',
    ['one', 'two'],
    explode(',', $this->Tags ?: '')
);

$field->setShouldLazyLoad(true); // tags should be lazy loaded

$field = TagField::create(
    'Tags',
    'Blog Tags',
    TaxonomyTerm::get(),
)
    ->setTitleField('Name');