1
votes

So, disclaimer first: I'm a bit of a noob when it comes to SilverStripe, but this one is vexxing me.

I'm using GridField to add and edit the entries in a DataObject. This is all well and good, and works perfectly. The only thing I can't figure out is how to change the order of the EDITABLE fields - this isn't the initial table display of the entries (which is set by $config), it's the actual input fields once you click "add new" or go to edit a record.

At the moment the Image uploadForm and the Signature <select> box are below the Body HTMLText field, which is messy and doesn't work right. I want them up the top, right below the Summary element.

I've tried playing around with changeFieldOrder(), but that doesn't work on a GridField object type and $fields doesn't know anything about the input elements (I dump()'ed it and had a look).

MediaReleaseItem.php:

class MediaReleaseItem extends DataObject {
static $db = array (
    'Title'     => 'Varchar',
    'DateUpdated'   => 'Date',
    'Summary'   => 'Varchar',
    'Image'     => 'Varchar',
    'Body'      => 'HTMLText',
    );

private static $has_one = array(
    "Image"             => "Image",
    "MediaReleaseItem"  => "MediaReleases",
    "Signature"         => "MediaReleaseSignature",
    );
}

And MediaReleases.php:

class MediaReleases extends Page {

private static $has_many = array(
    "MediaReleaseItems" => "MediaReleaseItem",
    "Signature"         => "MediaReleaseSignature",
    );

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

    $config = GridFieldConfig_RecordEditor::create();
    $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
        'Title'=> 'Title',
        'DateUpdated' => 'Date',
        'Summary' => 'Summary',
        ));
    $mediaReleasesField = new GridField(
        'MediaReleaseItem', // Field name
        'Media Releases', // Field title
        $this->MediaReleaseItems(),
        $config
        );

    $fields->addFieldToTab('Root.MediaReleaseItems', $mediaReleasesField);
    return $fields;
    }
}

(Signature is just another DataObject with a different GridField on a different tab, I didn't include the code for it because it's almost identical.)

1

1 Answers

4
votes

so, you mean when you edit a MediaReleaseItem the fields are not the way you want then to be?

simple: just also define a method getCMSFields() on the class MediaReleaseItem.

<?php

class MediaReleaseItem extends DataObject {
    private static $db = array (
        'Title'     => 'Varchar',
        'DateUpdated'   => 'Date',
        'Summary'   => 'Varchar',
        'Image'     => 'Varchar',
        'Body'      => 'HTMLText',
    );
    private static $has_one = array(
        "Image"             => "Image",
        "MediaReleaseItem"  => "MediaReleases",
        "Signature"         => "MediaReleaseSignature",
    );
    public function getCMSFields() {
        $arrayOfSignatures = MediaReleaseSignature::get()->map()->toArray();
        $fields = FieldList::create(array(
            TextField::create('Title', 'Title for this Item'),
            DateField::create('DateUpdated', 'Updated')->setConfig('showcalendar', true),
            TextField::create('Image', 'Image'),
            // not sure if it works to have both a DB field and a has_one with the same name
            UploadField::create('ImageID', 'Image'),
            DropdownField::create('Signature', 'Signature', $arrayOfSignatures),

            // you can add more fields here
        ));

        // but you can also add fields here
        $fields->insertBefore(TextField::create('Summay', 'Summary'), 'DateUpdated');
        $fields->push(HTMLEditorField::create('Body', 'Body Content'));
        return $fields;
    }
}