Setting a variable in dashboard that is accessed from block and $pkg->saveConfig vs Config::save

Permalink 1 user found helpful
I made a paypal button block but would like to be able to set the paypal email address from the dashboard. The address could change but there is multiple areas its used. Going back and editing each block would be troublesome.

I need to make a dashboard page that stores the email address and then access it from either page controllers or blocks. I still don't know how to do this.

I've asked before about making dashboard pages and have checked out some peoples simple blocks that allow you to store some data via a dashboard page and pull it into a block, but I don't quite understand how its really working.

Can someone walk me through a basic example of this?


My first post on the matter a while back:
http://www.concrete5.org/community/forums/5-7-discussion/making-a-b...

Helpful links for others learning about single pages:
http://documentation.concrete5.org/developers/working-with-pages/si...
http://documentation.concrete5.org/api/class-Concrete.Core.Page.Sin...

ob7dev
 
hutman replied on at Permalink Best Answer Reply
hutman
So what you do is create a single page in the Dashboard, with a controller and a view. In the view method of the controller you will get the config variable and pass it to the view with code such as this:

$pkg = Package::getByHandle('your_package_handle');
$paypal_email = $pkg->getConfig()->get('app.paypal.email_address');
$this->set('paypal_email', $paypal_email);


Then in your view you would create an input field to let the user set this variable such as

<fieldset>
    <legend><?php  echo t('PayPal Settings'); ?></legend>
    <div class="form-group">
        <?php  echo $form->label('paypal_email', 'Email Address'); ?>
        <?php  echo $form->text('paypal_email', $paypal_email); ?>
    </div>
</fieldset>


Then you would have that input submit to your save function in the controller, in the save function you would need to write the value to the config like this

$paypal_email = $this->post('paypal_email');
$pkg = Package::getByHandle('your_package_handle');
$pkg->getConfig()->save('app.paypal.paypal_email', $paypal_email);


This will now be stored and you can access it from your block with the same code we had for the view function in the single page controller.

Make sure to change out the "your_package_handle", if you aren't putting this into a package you can refer to the post by @Korvin in this thread for the code to do it without, but the idea is the same -http://www.concrete5.org/community/forums/5-7-discussion/packagesav...
ob7dev replied on at Permalink Reply
ob7dev
I think whats confused me about this all along is we don't have to declare a database schema. My mind is in building blocks mode. When we use Config, we can just give it data and it saves it somewhere??? Thanks for your example, it helped me finally figure this out.

Can you explain to me the different between using $pkg->getConfig()->save and doing Config::save ???

Is one method preferred over the other?
hutman replied on at Permalink Reply
hutman
I am not 100% sure about the difference but I believe they save the config in two different places. I'd have to poke around some to tell you for sure but I believe that's the case.
JohntheFish replied on at Permalink Reply
JohntheFish
In addition, I think package configs get cleared when a package is uninstalled. Hence preferred for packages because it avoids clutter building up.
ob7dev replied on at Permalink Reply
ob7dev
I don't know where package configs are saved, but I did some testing and it looks to me like the configuration actually persists. I have a checkbox that gets saved to the package config, whenever I uninstalled and reinstalled, the last settings of the checkbox persisted. I ended up presetting a value for the config variable on install to get around this.

As for using Config, I think it gets saved to application/config/generated_overrides, as for $pkg->getConfig, I do not know where that data is kept.
ob7dev replied on at Permalink Reply
ob7dev
Difference between $pkg->config and Config:: is:
$pkg->config saves the sql database, inside the table Config.

Config:: saves to a file in application/config/generated overrides..