Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to create a dropdown select in CMS for a has_one data object?


Go to End


6 Posts   4759 Views

Avatar
Turismo

Community Member, 28 Posts

17 May 2016 at 2:10am

Edited: 17/05/2016 10:43am

Hey, I'm new to SS, can someone advise on the below please.

I've got a has_one setup in mysite/code/Page.php:

private static $has_one = array (
        'Icons' => 'icon'
);

I just need to figure out how to create a CMS field in the form of a dropdown, that lets me assign an icon to a page. I only want to be able to assign one icon per page. To keep it D.R.Y., I'd ideally like to be able to add the icons into the CMS as data objects, which could have the same icon associated with multiple pages.

My /mysite/code/Icon.php is:

<?php
class Icon extends DataObject {

    private static $db = array (
        'Title' => 'Varchar',
        'CSS' => 'Varchar',
    );

    public function getCMSFields() {
        $fields = FieldList::create(
            TextField::create('Title'),
            TextareaField::create('CSS')
        );

        return $fields;
    }
}

Avatar
Matt

Community Member, 86 Posts

17 May 2016 at 11:03am

Edited: 17/05/2016 11:03am

Hey Turismo,

You can select from a list of icons with the following code added to your getCMSFields() method on the Page class:

public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', new DropdownField('IconID', 'Choose an Icon', Icon::get()->map('ID', 'Title'));
    return $fields;
}

Note: Your $has_one relationship should be 'Icon' => 'Icon' for this to work (the left-side of the array is the name of the DB field, and the right side is the object you want to link to, both are case-sensitive.

SilverStripe will automatically create a database field called 'IconID' if your has_one is called 'Icon', so that's why the DropdownField uses the 'IconID' value, rather than just 'Icon'.

HTH!

Avatar
Turismo

Community Member, 28 Posts

17 May 2016 at 11:25am

Edited: 17/05/2016 11:56am

Hi Matt,

I've got the icon dropdown working thank you! Obviously it's empty as I haven't got any icon records yet to choose from; can you please clarify where I add these? As these are data objects, do I need to setup a GridFieldConfig_RecordEditor?

This is my Icon.php class:

class Icon extends DataObject {

    private static $db = array (
        'Title' => 'Varchar'
    );

    private static $has_one = array (
        'Page' => 'Page'
    );

    public function getCMSFields() {
        $fields = FieldList::create(
            TextField::create('Title')
        );

        return $fields;
    }
}

Avatar
dhensby

Community Member, 253 Posts

18 May 2016 at 1:12am

Where you manage your Icon objects is up to you. You have 3 options.

1. Use a `ModelAdmin` and create a dedicated section of the CMS.

You can create an admin section for managing objects by creating a class that extends `ModelAdmin` (eg: `IconAdmin`) see https://docs.silverstripe.org/en/3.1/developer_guides/customising_the_admin_interface/modeladmin/ and https://www.silverstripe.org/learn/lessons/introduction-to-modeladmin

You can easily set permissions on this ModelAdmin so that only ADMIN users can access it.

2. Manage them on the same page

You could add a `GridField` to manage all Icons on the page. This may be confusing for the CMS users as normally you'd manage a relationship this way, but you can manage arbitrary models on a page by adding something like this to your `Page` model:

public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Icons', GridField::create(
        'Icons', //make sure you don't have a relation with this name
        'Icons',
        Icons::get(),
        GridFieldConfig_RecordEditor::create()
    );
    return $fields;
}

3. Add them to `SiteConfig`

For this you'd need to create a `DataExtension` and add it to `SiteConfig`, in that extension you'd define `updateCMSFields($fields)` and add the gridfield as above. CMS users can then manage icons by going to the Settings section of the CMS. see https://docs.silverstripe.org/en/3.3/developer_guides/configuration/siteconfig/#extending-siteconfig

Avatar
Turismo

Community Member, 28 Posts

18 May 2016 at 10:15am

Hi Matt and Dan,

Just to let you know that I've got the icons all setup in the CMS! Thanks for the support. Dan, I went with ModelAdmin which was exactly what I was after.

Still got an issue with calling in my $has_one and $has_many values into my template though :( See here http://www.silverstripe.org/community/forums/general-questions/show/111957 I must be missing something obvious. Similar to the banner and sections, the $has_one icon isn't showing up either. Tried ?flush but no such luck. Have checked with $ClassName and the correct class is being used.

Avatar
Turismo

Community Member, 28 Posts

19 May 2016 at 1:04am

Trying to introduce setEmptyString, but what am I doing wrong?

Error message:

Fatal error: Call to undefined method SS_Map::setEmptyString() in /Users/christiangoodrich/Documents/git/evans-jones/mysite/code/ServiceSection.php on line 19

mysite/code/ServiceSection.php:

class ServiceSection extends DataObject {

    private static $db = array (
        'Title' => 'Varchar(255)',
        'Content' => 'HTMLText',
    );

    private static $has_one = array (
        // 'Photo' => 'Image',
        'ServicePage' => 'ServicePage',
        'Icon' => 'Icon'
    );

    public function getCMSFields() {
        return new FieldList(
            new TextField('Title'),
            new DropdownField('IconID', 'Choose an Icon', Icon::get()
                ->map('ID', 'Title')->setEmptyString('(Select one)')
            ),
            new HTMLEditorField('Content')
        );

        // $uploader->setFolderName('region-photos');
        // $uploader->getValidator()->setAllowedExtensions(array('png','gif','jpeg','jpg'));

        return $fields;
    }
}