Jump to:

23355 Posts in 17836 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » SS3 DataExtension + Data Object

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

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

Page: 1 2
Go to End
Author Topic: 1993 Views
  • Craftnet
    Avatar
    Community Member
    58 Posts

    SS3 DataExtension + Data Object Link to this post

    Hi,

    I have problem.

    Generally I want in one place add pictures to the sliders and they are to be displayed on all pages.

    In SS 2.4.7 I add to SiteConfig
    In SS3 I would do the same so:

    I try add to DataExtension DataObject

    AllSite.php

    class AllSite extends DataExtension {
       
       static $has_many = array (
    'Sliders' => 'Slider'
    );
       
       public function updateCMSFields(FieldList $fields) {
       
       
        //SLIDER
          $gridFieldConfigSlider = GridFieldConfig::create()->addComponents(
             new GridFieldToolbarHeader(''),
    new GridFieldAddNewButton('toolbar-header-right'),
             new GridFieldDataColumns(),
             new GridFieldEditButton(),
             new GridFieldDeleteAction(''),
             new GridFieldDetailForm()
          );
          
          $gridFieldSlider = new GridField('Sliders', 'Slider', new DataList('Slider'), $gridFieldConfigSlider);
          $fields->addFieldToTab('Root.Slider', $gridFieldSlider);

    }    
    }

    class Slider extends DataObject
    {
    static $db = array (

    );

    static $has_one = array (
    'Slider' => 'Image',

    );
       
       function getCMSFields() {
    return new FieldSet(
    new UploadField('Slider')
    );
    }
    }

    in template i add:

    <% loop SiteConfig %>
       $Slider
    <% end_loop %>

    inn backend i can add image to slider but in frontend in right place picture not show

    Who have any idea?

    Sorry for my bad English

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: SS3 DataExtension + Data Object Link to this post

    Maybe something like:

    <% with SiteConfig %>
       <% loop $Sliders %>
          $Slider
       <% end_loop %>
    <% end_with %>

  • Craftnet
    Avatar
    Community Member
    58 Posts

    Re: SS3 DataExtension + Data Object Link to this post

    thx for replay.

    still nothing,
    In you'r set i have errorp (you have to remove the $ in the control)
    . I'm not sure if the code is correct, but even that everything looks good

    If for example I will add to the database
    static $ db = array (
             'FooterContent' => 'htmlText'
         );

    then <% control SiteConfig%> work but not in my example.

    In last resort, write a function that collects in Page_Controller in someone side of these figures but I'd also know such a solution.

    Sorry for my bad english

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: SS3 DataExtension + Data Object Link to this post

    Sorry about the &

    Does your Slide object link back to SiteConfig, using something like:

    static $has_one = array('SiteConfig' => 'SiteConfig');

    i've not yet used the GridField from the SiteConfig, so I don't know if it works at all...
    You could try a ?showqueries=1 to check if the query on Slides is actually executed and what it looks like...

  • Jaymonkey
    Avatar
    Community Member
    10 Posts

    Re: SS3 DataExtension + Data Object Link to this post

    I don't suppose anybody got to the bottom of this problem?

    I am using a data extension to extend the site config. I have a $has_many data object in my extension. I have used a GridField and all the relevant fields are showing in the settings page only when I hit the save button they are not being stored!

    My CustomSiteConfig Class:

    <?php
    class CustomSiteConfig extends DataExtension {

    private static $has_many = array(
    'Sponsors' => 'Sponsor'
    );

    public function updateCMSFields(FieldList $fields) {

    $config = GridFieldConfig_RelationEditor::create();

    $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
    'Title' => 'Title'
    ));

    $sponsorfield = new GridField(
    'Sponsors',
    'Sponsor',
    new DataList('Sponsor'),
    $config
    );

    $fields->addFieldToTab('Root.Main', $sponsorfield);
    }

    }

    ?>

    My DataObject Class:

    <?php
    class Sponsor extends DataObject {

    static $db = array (
    'title' => 'text'
    );

    static $has_one = array (
    'CustomSiteConfig' => 'CustomSiteConfig',
    'Image' => 'Image',
    'Link' => 'SiteTree'
    );

    public function getCMSFields() {
    $sizeMB = 2; // 2 MB
    $size = $sizeMB * 1024 * 1024; // 2 MB in bytes

    $uploadField = new UploadField('SponsorImage', 'Sponsor Image');

    $uploadField->getValidator()->setAllowedMaxFileSize($size);

    $fields = new FieldList(
    new TextField('Title'),
    $uploadField,
    new TreeDropdownField('LinkID', 'Link', 'SiteTree')
    );

    return $fields;
    }
    }

    ?>

    Can anyone help please?

  • Craftnet
    Avatar
    Community Member
    58 Posts

    Re: SS3 DataExtension + Data Object Link to this post

    Do somthing like this:
    Of course this is example!

    <?php
    class Galeria extends DataObject {

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

    private static $has_one = array(
    'Image' => 'Image',
    'GalleryPage' => 'GalleryPage'
    );

    private static $searchable_fields = array(
    'Title' => 'Tytuł'
    );

    public function getCMSFields() {
    $fields = new FieldList();
    $fields->push( $type = new UploadField("Image", "Image"));
    $type->allowedExtensions = array('jpg', 'gif', 'png');
          $type->setFolderName('Galeria');
    $fields->push( new TextField("Title", "Krótki opis"));

    return $fields;
    }

    // Tell the datagrid what fields to show in the table
    public static $summary_fields = array(
    'Title' => 'Tytuł',
    'Thumbnail' => 'Thumbnail'
    );

    public function getThumbnail() {
    return $this->Image()->CMSThumbnail();
    }

    }

    <?php
    class GalleryPage extends SiteTree {

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

    private static $has_one = array(

    );

    private static $has_many = array(
    'Gallerys' => 'Gallery',
    );

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

    $gridFieldConfig = GridFieldConfig_RecordEditor::create();
    $gridfield = new GridField("Gallerys", "Galeria", $this->Gallerys(), $gridFieldConfig);
    $fields->addFieldToTab('Root.Galeria', $gridfield);

    /*
    $fields->addFieldsToTab('Root.Top', array(
       new TextField('TitleTop', 'Tytuł')
    ));
          */

    return $fields;
    }

    }

    class GalleryPage_Controller extends Page_Controller {

    }

    IMPORTANT
    in Page.php in Controller

    public function GallerysOnAllSite() { return GalleryPage::get(); }

    then in template

    <% loop GallerysOnAllSite%>
    <% loop Gallery %>
    ...
    <% end_loop %>
    <% end_loop %>

    Sorry for my bad English

  • Jaymonkey
    Avatar
    Community Member
    10 Posts

    Re: SS3 DataExtension + Data Object Link to this post

    So it's not possible to do this by extending site config?

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: SS3 DataExtension + Data Object Link to this post

    i would think your Sponsor's has_one should point to the SiteConfig class, not the CustomSiteConfig, as the latter is not a DataObject in itself, it.just enhances the SiteConfig...

    Edit: An alternative would be to just use ModelAdmin for your Sponsors and in your Page_Controller class create a function like this:

    public function getAllSponsors() {
    return Sponsor::get();
    }

    Then <% loop $AllSponsors %> in your Page template...

    1993 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.