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

[SOLVED] Calling CustomSiteConfig DataObjects in 2.4


Go to End


10 Posts   2680 Views

Avatar
ambient

Community Member, 130 Posts

27 February 2015 at 4:25am

Hi all :)

I've been trying for sometime to get DataObjects and CustomSiteConfig to work in 2.4. Is it even possible or am I wasting my time?

I have it working in the Admin where I can add dataobject items to the site config and it saves/works fine.
The problem is how do I call it from the Template? I've tried all sorts of variations as can be seen from my code below.
Am I missing something obvious?

Footer.ss

<% control FooterItems %>
		   <p>$SiteConfig.FooterText</p>
          <% end_control %>
          
          <% control SiteConfig.FooterItems %>
		   <p>$FooterText</p>
          <% end_control %>
          
          <% control MyFooters %>
		   <p>$SiteConfig.FooterText</p>
          <% end_control %>

FooterItem.php

<?php

class FooterItem extends DataObject {
	
	static $db = array(
		
		'FooterText' => 'Text',
		'FooterLink' => 'Text'
	);
	
	static $has_one = array(
		//'FooterImage' => 'Page_Image',
		'MyFooter' => 'CustomSiteConfig'
	);
	
	function MyFoom() {
		return DataObject::get( 'CustomSiteConfig', "`MyFooterItemID` = '{$this->ID}'" );
	}
	
	
	function getCMSFields_forPopup() {
    $fields = new FieldSet();
 
    $fields->push( new TextField('FooterText', 'Text' ) );
    $fields->push( new TextField('FooterLink', 'Link' ) );
 
    return $fields;
		}
		
	
	
}

?>

CustomSiteConfig .php

<?php
  
class CustomSiteConfig extends DataObjectDecorator {
     
   function extraStatics() { 
      return array(
	  'db' => array(
			
		),
			
	  'has_one' => array(	
		  
		  ),
		  
		'has_many' => array(	
		  'MyFooter' => 'FooterItem',
		  ),
      );
	  
    } 
  
    public function updateCMSFields(FieldSet &$fields) {
         
		
        $manager = new DataObjectManager(
    $this->owner,
    'MyFooter',
    'FooterItem',
    array(
      'FooterText' => 'Text',
      'FooterLink' => 'Link'
    ),
    'getCMSFields_forPopup'
  );
  $manager->setSourceID($this->owner->ID);
  $manager->setAddTitle( 'a new location' );
 
  $fields->addFieldToTab('Root.Footer', $manager ); 
    }
	
	
     
}

Avatar
Pyromanik

Community Member, 419 Posts

27 February 2015 at 4:54am

Edited: 27/02/2015 5:02am

You are confusing your scope it seems.

The objects are on the site config, not the page.
So $FooterItems is basically asking Page.FooterItems, not SiteConfig.FooterItems
Either add a <% control SiteConfig %> before you start your inner loop, or jump directly with <% control SiteConfig.FooterItems %>

Your footer items are also called 'MyFooter'
And ensure you're in the root scope first (Not already in a control).

<% control SiteConfig.MyFooter %>$FooterText [...]

Avatar
ambient

Community Member, 130 Posts

27 February 2015 at 6:16am

Hi Pyromanik,
I had tried <% control SiteConfig.MyFooter %>$FooterText<% end_control %> but get the following error...
I am definitely confused :)

[User Error] Couldn't run query: SELECT "FooterItem"."ClassName", "FooterItem"."Created", "FooterItem"."LastEdited", "FooterItem"."FooterText", "FooterItem"."FooterLink", "FooterItem"."MyFooterID", "FooterItem"."ID", CASE WHEN "FooterItem"."ClassName" IS NOT NULL THEN "FooterItem"."ClassName" ELSE 'FooterItem' END AS "RecordClassName" FROM "FooterItem" WHERE ("ParentID" = '1') Unknown column 'ParentID' in 'where clause'

Avatar
Pyromanik

Community Member, 419 Posts

27 February 2015 at 12:05pm

Oh, CustomSiteConfig is a decorator, not a model object.
FooterItem's reverse relation (has_one) should be to just SiteConfig.

'ParentID' is a fallback SS tries when it can't figure it out for itself.

Also, as a side note, never ?> your (pure) php files. There's no need, and it can cause confusing issues if you accidentally have white space or something on the other side of it (say if source management [svn|git|etc.] adds a newline automatically at the end of the file if it doesn't exist).

Avatar
Webdoc

Community Member, 349 Posts

28 February 2015 at 2:04am

Edited: 28/02/2015 2:19am

Look to arvixe blog : or my sample code:
CustomSiteConfig.php

<?php
class CustomSiteConfig extends Extension {
	function extraStatics() {
		return array(
			'has_many' => array(
			'MyFooters' => 'MyFooter'
		)
	);
}
public function updateCMSFields(FieldSet &$fields) {
$manager = new DataObjectManager(
$this->owner,
'MyFooters',
'MyFooter',
MyFooter::$summary_fields,
'getCMSFields_forPopup'
);
$fields->addFieldToTab("Root.Footer", $manager);
}
function MyFooters() {
return DataObject::get('MyFooter');
}
}

MyFooter.php
<?php
class MyFooter extends DataObject
{
static $db = array (
   'FooterText' => 'Text',
   'Link' => 'Text'   
);
static $has_one = array (
   'SiteConfig' => 'SiteConfig',
);
   
public function getCMSFields_forPopup()
   {
      return new FieldSet(
         new TextField('FooterText'),
         new TextField('Link'),
      );
   }
}
?>

in theme use
<% control SiteConfig.MyFooters %>
<% if Link %><a href="$Link"><% end_if %>$FooterText<% if Link %></a><% end_if %>
<% end_control %>

------------------------------------------------------------------------------------------------------------------------------------
Arvixe Web Hosting / SilverStripe Community Liaison | Looking for quality SilverStripe Web Hosting? Look no further than Arvixe Web Hosting!

Avatar
ambient

Community Member, 130 Posts

3 March 2015 at 1:06am

Sorry I wasn't able to get to my computer over the weekend.
Hey it worked!! Changing the (has_one) to just SiteConfig did it.

Although the link seems to be a problem. It's adding the base URL to the href.

$FooterLink.Link gives www.mywebsite.com and

$FooterLink gives www.mywebsite.com/footerlink

<% control SiteConfig.MyFooter %>
           <p><a href="$FooterLink.Link" target="_blank">$FooterText</a></p>
<% end_control %>

Avatar
Pyromanik

Community Member, 419 Posts

3 March 2015 at 3:19am

Edited: 03/03/2015 3:21am

FooterLink is a text object.
FooterLink.Link is a nothing - it is blank, meaning your href="" meaning your browser will take you to whatever the base tag is set to (wherever your SS install is relative to domain root eg. domain.tld).

$FooterLink will give just whatever text is stored in that property. So href="FreeText" - which again combined with the base tag is giving you yourdomain.tld/FreeText

This part is all the same as 3.x

You need to enter a fully qualified link into your text field in the cms, including the http:// part.
Sometimes it's good to have a check for http(s):// and add it if it is missing through a getter or something (function getFooterLink() {...})

Avatar
ambient

Community Member, 130 Posts

3 March 2015 at 4:04am

Success!!! :) Thanks you so much for your help Pyromanik!

For anyone trying to do the same thing with CustomSiteConfig and DataObjects here's the finished code!

CustomSiteConfig.php

<?php
  
class CustomSiteConfig extends DataObjectDecorator {
     
   function extraStatics() { 
      return array(
	  'db' => array(
			
		),
			
	  'has_one' => array(	
		  
		  ),
		  
		'has_many' => array(	
		  'MyFooter' => 'FooterItem',
		  ),
      );
	  
    } 
  
    public function updateCMSFields(FieldSet &$fields) {
         
		
      $manager = new DataObjectManager(
    $this->owner,
    'MyFooter',
    'FooterItem',
    array(
      'FooterText' => 'Text',
      'FooterLink' => 'Link'
    ),
    'getCMSFields_forPopup'
  );
  $manager->setSourceID($this->owner->ID);
  $manager->setAddTitle( 'a new location' );
 
  $fields->addFieldToTab('Root.Footer', $manager ); 
    }
	
	
     
}

FooterItem.php

<?php

class FooterItem extends DataObject {
	
	static $db = array(
		
		'FooterText' => 'Text',
		'FooterLink' => 'Text'
	);
	
	static $has_one = array(
		//'FooterImage' => 'Page_Image',
		'MyFooter' => 'SiteConfig'
	);
	
	
	function MyFoom() {
		return DataObject::get( 'CustomSiteConfig', "`MyFooterItemID` = '{$this->ID}'" );
	}
	
	
	function getCMSFields_forPopup() {
    $fields = new FieldSet();
 
    $fields->push( new TextField('FooterText', 'Text' ) );
    $fields->push( new TextField('FooterLink', 'Link' ) );
 
    return $fields;
		}
		
	
	
}

Footer.ss

<% control SiteConfig.MyFooter %>
           <p><a href="http//:$FooterLink" target="_blank">$FooterText</a></p> 
<% end_control %>

Go to Top