WillieBoy
WillieBoy

Reputation: 5

Add new menu-item in Silverstripe CMS

I'm trying to create a new menu item in the Silverstripe CMS. I still do not get it. I do not know what I did wrong. It's about Silverstripe 3.6.

I also tried "/dev/build" and "dev/build?flush=1". But no result.

Footerinfos.php (mysite/code/)

class FooterInfos extends ModelAdmin {

public static $managed_models = array('FooterInfo');
static $url_segment = 'footerinfo';
static $menu_icon = 'framework/admin/images/menu-icons/network.png';
static $menu_title = 'Footer';

}

Footerinfo.php (mysite/code/)

class FooterInfo extends DataExtension {
static $db = array(
    'FooterHead1' => 'Varchar(255)',
    'FooterHead2' => 'Varchar(255)',
    'FooterHead3' => 'Varchar(255)',
    'FooterText1' => 'HTMLText',
    'FooterText2' => 'HTMLText',
    'FooterText3' => 'HTMLText',
);

function getCMSFields() {   
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root', new TabSet('Footer'));
    $fields->addFieldToTab('Root.Footer', new TextField('FooterHead1', 'Footer - Kop 1'));
    $fields->addFieldToTab('Root.Footer', $h1=new HTMLEditorField('FooterText1', 'Footer - Tekst 1'));  
    $fields->addFieldToTab('Root.Footer', new TextField('FooterHead2', 'Footer - Kop 2'));
    $fields->addFieldToTab('Root.Footer', $h2=new HTMLEditorField('FooterText2', 'Footer - Tekst 2'));
    $fields->addFieldToTab('Root.Footer', new TextField('FooterHead3', 'Footer - Kop 3'));
    $fields->addFieldToTab('Root.Footer', $h3=new HTMLEditorField('FooterText3', 'Footer - Tekst 3'));  
    $h1->setRows(7);
    $h2->setRows(7);
    $h3->setRows(7);

    return $fields;
}

}

Update 14:53u:

It has now been possible to create a menu item. Only I wonder if it's immediately visible to these fields instead of a selection menu. So like the same with the Settings menu, the fields will be displayed immediately.

I do not get to see the fields right away. Only a list of IDs. If you click on that ID, you will only see the fields. That is not the intention. It must be all the fields right away. Because there are no more than 1 footer.

I have translated the code a bit into my language ;)

Voettekst.php

class VoetTekst extends DataObject {

static $db = array(
    'VoetKop1' => 'Varchar(255)',
    'VoetKop2' => 'Varchar(255)',
    'VoetKop3' => 'Varchar(255)',
    'VoetTekst1' => 'HTMLText',
    'VoetTekst2' => 'HTMLText',
    'VoetTekst3' => 'HTMLText',
);

public function getCMSFields() {

    return FieldList::create(
        TextField::create('VoetKop1', 'Footer - Kop 1'),
        HTMLEditorField::create('VoetTekst1', 'Footer - Tekst 1')->setRows(7),

        TextField::create('VoetKop2', 'Footer - Kop 2'),
        HTMLEditorField::create('VoetTekst2', 'Footer - Tekst 2')->setRows(7),

        TextField::create('VoetKop3', 'Footer - Kop 3'),
        HTMLEditorField::create('VoetTekst3', 'Footer - Tekst 3')->setRows(7) 
      );

}
}

VoettekstAdmin.php

class VoettekstAdmin extends ModelAdmin {

private static $managed_models = array('Voettekst');
private static $url_segment = 'voettekst';
private static $menu_icon = 'framework/admin/images/menu-icons/16x16/network.png';
private static $menu_title = 'Voettekst';

}

Screenshots:

First page

Second page after click on ID

Upvotes: 0

Views: 798

Answers (4)

Terry Apodaca
Terry Apodaca

Reputation: 51

I believe starting with SS3.* moving forward things needed to be "private", so maybe try changing your code to:

class FooterInfoAdmin extends ModelAdmin {
    private static $managed_models = array('FooterInfo');
    private static $url_segment = 'footerinfo';
    private static $menu_icon = 'framework/admin/images/menu-icons/network.png';
    private static $menu_title = 'Footer';
}

*** Above answer fixed your first problem...

UPDATE 1::Now for your DataObject

class FooterInfo extends DataObject {
    static $db = array(
        'FooterHead1' => 'Varchar(255)',
        'FooterHead2' => 'Varchar(255)',
        'FooterHead3' => 'Varchar(255)',
        'FooterText1' => 'HTMLText',
        'FooterText2' => 'HTMLText',
        'FooterText3' => 'HTMLText',
    );

    public function getCMSFields() {
        return new FieldList(
            TextField::create('FooterHead1', 'Footer - Kop 1'),
            HTMLEditorField::create('FooterText1', 'Footer - Tekst 1')->setRows(7),

            TextField::create('FooterHead2', 'Footer - Kop 2'),
            HTMLEditorField::create('FooterText2', 'Footer - Tekst 2')->setRows(7),

            TextField::create('FooterHead3', 'Footer - Kop 3'),
            HTMLEditorField::create('FooterText3', 'Footer - Tekst 3')->setRows(7) 

        );
    }
}

UPDATE 2::This is a new update to explain how I would redo what you are doing for such a small task that doesn't really need to be managed by ModelAdmin.

I would extend SiteConfig to manage your Footer content. Others might not agree but to me, anything that is "site wide" should be managed in a central location, and SiteConfig is perfect location for that. This way you wouldn't even need a whole new DataObject or new ModelAdmin. See code below:

Place in a new file called SiteConfigExtension.php:

class SiteConfigExtension extends DataExtension
{
    private static $db = [
        'FooterHead1' => 'Varchar(255)',
        'FooterHead2' => 'Varchar(255)',
        'FooterHead3' => 'Varchar(255)',
        'FooterText1' => 'HTMLText',
        'FooterText2' => 'HTMLText',
        'FooterText3' => 'HTMLText',
    ];

    public function updateCMSFields(FieldList $fields){
        $fields->addFieldToTab('Root.Footer', TextField::create('FooterHead1', 'Footer - Kop 1'));
        $fields->addFieldToTab('Root.Footer', HTMLEditorField::create('FooterText1', 'Footer - Tekst 1')->setRows(7));

        $fields->addFieldToTab('Root.Footer', TextField::create('FooterHead2', 'Footer - Kop 2'));
        $fields->addFieldToTab('Root.Footer', HTMLEditorField::create('FooterText2', 'Footer - Tekst 2')->setRows(7));

        $fields->addFieldToTab('Root.Footer', TextField::create('FooterHead3', 'Footer - Kop 3'));
        $fields->addFieldToTab('Root.Footer', HTMLEditorField::create('FooterText3', 'Footer - Tekst 3')->setRows(7));
    }
}

Then in your config.yml add:

SiteConfig:
  extensions:
    - SiteConfigExtension

Then, run /dev/build, and you should see a new tab in the Settings tab called Footer with your new fields.

UPDATE 3:: maybe you can try this in your DataObject:

public function getCMSFields()
{
    $fields = new FieldList(
        new TabSet(
            "Root",
            $tabMain = new Tab(
                TextField::create('FooterHead1', 'Footer - Kop 1'),
                HTMLEditorField::create('FooterText1', 'Footer - Tekst 1')->setRows(7),

                TextField::create('FooterHead2', 'Footer - Kop 2'),
                HTMLEditorField::create('FooterText2', 'Footer - Tekst 2')->setRows(7),

                TextField::create('FooterHead3', 'Footer - Kop 3'),
                HTMLEditorField::create('FooterText3', 'Footer - Tekst 3')->setRows(7)
            ),
        ),
        new HiddenField('ID')
    );

    $this->extend('updateCMSFields', $fields);

    return $fields;
}

***Disclaimer: the above code is not tested.

Upvotes: 0

Gavin Bruce
Gavin Bruce

Reputation: 1809

You are probably better off using SiteConfig for this as you only really want one footer. Data Objects are used for multiple records.

mysite/code/CustomSiteConfig.php

class CustomSiteConfig extends DataExtension {

  private static $db = array(
    'VoetKop1' => 'Varchar(255)',
    'VoetKop2' => 'Varchar(255)',
    'VoetKop3' => 'Varchar(255)',
    'VoetTekst1' => 'HTMLText',
    'VoetTekst2' => 'HTMLText',
    'VoetTekst3' => 'HTMLText'
  );

  public function updateCMSFields(FieldList $fields) {
    $fields->addFieldsToTab('Root.Footer', array(
      TextField::create('VoetKop1', 'Footer - Kop 1'),
      HTMLEditorField::create('VoetTekst1', 'Footer - Tekst 1')->setRows(7),

      TextField::create('VoetKop2', 'Footer - Kop 2'),
      HTMLEditorField::create('VoetTekst2', 'Footer - Tekst 2')->setRows(7),

      TextField::create('VoetKop3', 'Footer - Kop 3'),
      HTMLEditorField::create('VoetTekst3', 'Footer - Tekst 3')->setRows(7) 
    ));
  }
}

mysite/_config/extensions.yml

SiteConfig:
  extensions:
    - CustomSiteConfig

Now in your templates, you can use $SiteConfig.variable to access the variables. To grab the first title you can use $SiteConfig.VoetKop1

This would usually be in a template include.

themes/themename/templates/Includes/Footer.ss

<div class="row">
  <div class="medium-4 small-12 columns">
    <h5>$SiteConfig.VoetKop1</h5>
    <div class="typography">$SiteConfig.VoetTekst1</div>
  </div>
  <div class="medium-4 small-12 columns">
    <h5>$SiteConfig.VoetKop2</h5>
    <div class="typography">$SiteConfig.VoetTekst2</div>
  </div>
  <div class="medium-4 small-12 columns">
    <h5>$SiteConfig.VoetKop3</h5>
    <div class="typography">$SiteConfig.VoetTekst3</div>
  </div>
</div>

Then you would use `<% include Footer %>

This would usually occur in the themes/themename/templates/Page.ss file.

Don't forget to change themename with your themes folder.

Under the Settings link in the admin, there will now be a tab called Footer with these fields.

This code isn't tested but it should work.

Upvotes: 0

Gavin Bruce
Gavin Bruce

Reputation: 1809

Referring to your above answer, $fields = parent::getCMSFields(); is used on pages, not dataobjects.

The easier way is to use the following:

function getCMSFields() {
  $fields = singleton( $this->ClassName )->getFrontendFields();
  return $fields;
}

If you want more control, you can use something like:

function getCMSFields() {
  return FieldList::create(
    TextField::create('FooterHead1', 'Footer - Kop 1'),
    HTMLEditorField::create('FooterText1', 'Footer - Tekst 1')->setRows(7)
  );
}

Upvotes: 0

Simon Erkelens
Simon Erkelens

Reputation: 762

What's in your template? Just simply calling $FooterInfo or <% loop $FooterInfo %>` won't work, because your base page class needs to know about it. You could add a method to the Page base like this:

class Page extends SiteTree
{
    public function FooterInfo()
    {
        return FooterInfo::get();
    }
}

To return the footer info on every page.

Unless you're actually using your DataExtension as DataExtension and apply it to Page that is.

In that case, please provide your config that tells the manifest to apply the data extension (e.g. your config.yml )

From the looks of it though, I think you want FooterInfo to extend DataObject, not DataExtension.

Upvotes: 0

Related Questions