Jump to:

3431 Posts in 1025 Topics by 864 members

Template Questions

SilverStripe Forums » Template Questions » ImageField / Image Variable Problems (only show if something is uploaded)

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

Page: 1 2
Go to End
Author Topic: 1368 Views
  • benni91
    Avatar
    Community Member
    72 Posts

    ImageField / Image Variable Problems (only show if something is uploaded) Link to this post

    (pls excuse my probably bad english and my small knowledge about ss and php i'm totaly new to this)

    Hi everyone,

    i think the title isn't very significantly so i'll tell my problem here more detailed.

    at first, here's my mysite/code/Referenz.php

    <?php

    class Referenz extends Page {
    static $db = array(
       'Description' => 'Text',
       'Kunde' => 'Text',
       'Website' => 'Text',
       'Leistung_1' => 'Text',
       'Leistung_2' => 'Text',
       'Leistung_3' => 'Text',
       'Leistung_4' => 'Text',
       'Leistung_5' => 'Text',
       'Leistung_6' => 'Text',
       'Leistung_7' => 'Text',
       'Leistung_8' => 'Text',
       'Leistung_9' => 'Text',
       'Leistung_10' => 'Text'
    );
    static $has_one = array(
    'Preview' => 'Image',
    'RefImg_1' => 'Image',
    'RefImg_2' => 'Image',
    'RefImg_3' => 'Image',
    'RefImg_4' => 'Image',
    'RefImg_5' => 'Image',
    'RefImg_6' => 'Image',
    'RefImg_7' => 'Image',
    'RefImg_8' => 'Image',
    'RefImg_9' => 'Image',
    'RefImg_10' => 'Image'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
       $fields->addFieldToTab('Root.Content.Main', new TextField('Description','beschreibung der Seite'), 'Content');
          
           $fields->addFieldToTab("Root.Content.Referenz", new ImageField('Preview'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_1'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_2'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_3'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_4'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_5'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_6'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_7'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_8'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_9'));
          $fields->addFieldToTab("Root.Content.Referenz", new ImageField('RefImg_10'));
       
           $fields->addFieldToTab('Root.Content.Main', new TextField('Kunde','Kunde des Projekts'));
          $fields->addFieldToTab('Root.Content.Main', new TextField('Website','Website / Live Preview (mit http://)'));
          
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_1','erbrachte Leistung 1'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_2','erbrachte Leistung 2'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_3','erbrachte Leistung 3'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_4','erbrachte Leistung 4'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_5','erbrachte Leistung 5'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_6','erbrachte Leistung 6'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_7','erbrachte Leistung 7'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_8','erbrachte Leistung 8'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_9','erbrachte Leistung 9'));
          $fields->addFieldToTab('Root.Content.Leistungen', new TextField('Leistung_10','erbrachte Leistung 10'));
       
    return $fields;
    }
       
    }

    class Referenz_Controller extends Page_Controller {
       
    }

    The "RefImg_x" variables are all listed in the Referenz.ss at the same time. But e.g when i uploaded images only to RefImg_1 and RefImg_2, RefImg_3 - 10 will show the folder path "assets/" -.- how can i stop that? so that the variable only shows something if there's an image uploaded for it?

    is there a function which hides the variables where's no image uploaded? which one?

    pls help me
    Benni

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: ImageField / Image Variable Problems (only show if something is uploaded) Link to this post

    You may be better off storing your images in a separate DataObject using the DataObjectManger Module instead of trying to force it into your Page Object. SO That way each instance of a RefImage could keep its own set of variable.

    So your page would have

    static $db = array(
    'Description' => 'Text',
    'Kunde' => 'Text',
    'Website' => 'Text'
    );

    static $has_many = array(
    'RefImages' => 'RefImage'
    );

    and then you'd have a RefImage DataObject

    class RefImage extends DataObject {
    static $db = array(
    'Leistung' => 'Text'
    );

    static $has_one = array(
    'RefImg' => 'Image'
    );

    Refer to the DataObjectManager Documentation for how to implement it in the CMS http://doc.silverstripe.org/old/modules:dataobjectmanager

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: ImageField / Image Variable Problems (only show if something is uploaded) Link to this post

    Thanks,
    i'll try it later an post the result

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: ImageField / Image Variable Problems (only show if something is uploaded) Link to this post

    Damn :/

    trying this: http://doc.silverstripe.org/old/modules:dataobjectmanager doesn't work, if i copy paste the code, the only result is an blank little popup which shows nothing.

    Cause of that i tried this one http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-1-keeping-it-simple/
    i edit the code to fit my requirements like this

    Referenzen.php http://pastie.org/2217623
    &
    Referenz.php http://pastie.org/2217632

    then i did an dev/build?flush=1 and created an Referenz Site on the Admincenter => Error -.-

    What did i wrong?
    I don't understand the whole thing :/

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: ImageField / Image Variable Problems (only show if something is uploaded) Link to this post

    I think you misunderstanding the concept of the relations work. Page should have the general information stored in its $db with a $has_many relation to Refrenzen. $has_one and $has_may represent relationships between data objects. It should be structured like this.

    Your Page Object should have the general information the page plus a has_many relation with your Refrenzen DataObject. Since Refrenzen Is really mainly holding an image you can you an ImageDataObjectManager

    http://pastie.org/2217794

    Then your Refrenzen object only needs to hold Image and Leistung

    http://pastie.org/2217796

    You need to think of Refrenzen as Children of Referenz as opposed to be being fields on it

    Hope this makes sense

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: ImageField / Image Variable Problems (only show if something is uploaded) Link to this post

    I don't know how to explain that in english, so i drawed an image ;)

    So Referenz should be Children of Referenzen

    Do you know what i mean with that picture?

    I tryed to build that with the dataobject, but theres always an error :/

    Attached Files
  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: ImageField / Image Variable Problems (only show if something is uploaded) Link to this post

    Okay are Refrenzen and Refrenz both Pages? or is Refrenz just some data stored on a Refrenzen Page. Are you getting an error when building the Database or when Trying to access it in the CMS

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: ImageField / Image Variable Problems (only show if something is uploaded) Link to this post

    yes Referenzen is one Page and on this page are many links + preview images of the difference referenz. if you click on one of them, you'll be redirected to a page with the type Referenz.

    In english you can say that a referenz is a project you designed (for example) and referenzen is the plural of that.

    The error comes while building a referenz page in the cms.
    silverstripe says server error and theres a blank silverstripe popup

    1368 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.