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.

General Questions /

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

Set Email::setAdminEmail from SiteConfig?


Reply


2 Posts   520 Views

Avatar
MarijnKampf

Community Member, 164 Posts

26 September 2013 at 8:00pm

Edited: 26/09/2013 8:01pm

I'm trying to set the admin email from an entry in the SiteConfig (SilverStripe 3.0). However, the siteconfig class can't be initiated in the _config.php and gives Fatal error: Call to a member function hasField() on a non-object in \framework\model\DataObject.php on line 195. Is there a way to set the admin email using a SiteConfig value?

mysite/_config.php

Object::add_extension('SiteConfig', 'MySiteConfig');

// The following fails with Fatal error.

$config = SiteConfig::current_site_config( );
Email::setAdminEmail($config->AdminEmail);

MySiteConfig.php

<?php

class MySiteConfig extends DataExtension {
   static $db = array(
      "AdminEmail" => "Text"
   );

   public function updateCMSFields(FieldList $fields) {
      $fields->addFieldToTab('Root.Main', new EmailField("AdminEmail", "Admin email address (from address used for forms etc.)"));
   }
}

Avatar
martimiz

Forum Moderator, 1105 Posts

28 September 2013 at 5:06am

I don't think there is any way to get to the values stored in database from within _Config.php, so it cannot be set that way at that stage... So the only way that comes to mind at this moment is letting SiteConfig write it to file on save if it changes, and then including that file in your _Config.php. Doable - but I hope someone can come up with a better way...