Coder Social home page Coder Social logo

chtombleson / silverstripe-linkfield Goto Github PK

View Code? Open in Web Editor NEW

This project forked from elliot-sawyer/silverstripe-linkfield

0.0 0.0 0.0 84 KB

Adds a Linkfield for gorriecoe/silverstripe-link

License: BSD 3-Clause "New" or "Revised" License

JavaScript 0.55% PHP 95.98% SCSS 3.48%

silverstripe-linkfield's Introduction

Silverstripe linkfield

Adds a Linkfield for gorriecoe/silverstripe-link

Installation

Composer is the recommended way of installing SilverStripe modules.

composer require gorriecoe/silverstripe-linkfield

Requirements

  • gorriecoe/silverstripe-link ^1.0
  • silvershop/silverstripe-hasonefield 3.0.1. See Issue #11
  • symbiote/silverstripe-gridfieldextensions 3.1

Maintainers

Screenshots

Link list Detail edit form

Usage

<?php

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;
    }
}

Configuration

By default all link types allowed in the Link model are displayed in the edit form, but this can be configured per-field, allowing multiple configurations without requiring multiple otherwise-identical Link model subclasses.

The Title field can also be hidden, which is useful if you intend on using the URL for a link but not the user-configured Title.

This configuration can be passed into the constructor, or set later using the LinkField::setLinkConfig() method.

Declare only which link types are allowed.

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

Explicitly declare whether each type of link is allowed or not.

// 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);

Hide the Title field

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

Setting the configuration later

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

Sort column

By default the LinkField assumes that the sort column is named Sort. If you want to use another field name such as SortOrder, you can specify it using the setSortColumn method like so:

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

You can also globally define the sort column. In your config.yml add the following:

gorriecoe\LinkField\LinkField:
  sort_column: 'SortOrder'

Belongs many many

We can also add a belongs_many_many relation to the Link class. In your config.yml add the following:

gorriecoe\Link\Models\Link:
  belongs_many_many:
    MyCustomObject : MyCustomObject.Buttons

silverstripe-linkfield's People

Contributors

gorriecoe avatar elliot-sawyer avatar alex-dna avatar guysartorelli avatar chillu avatar jsirish avatar mfendeksilverstripe avatar sharronrobins avatar tomaszpirc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.