Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I have a Global Set called "Settings" (handle = 'settings'); Within that I have a text field called "Address" (handle = 'address').

I have a function in my Plugin and I'd like to get the Address data.

In the front-end I would write this:

{{ settings.address }}

How can I get the same information in my plugin with PHP?

I've got as far as this:

$address = craft()->elements->getCriteria(ElementType::GlobalSet);

When I var_dump this array, I can see the address string. But I can't figure out how to filter down to the address correctly.

Thanks

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You can use the GlobalsService service available with craft()->globals like so:

// Get the settings global set
$settings = craft()->globals->getSetByHandle('settings');

// Access the address field
var_dump($settings->address)

Or you could use ElementCriteria as you suggested:

// Get the GlobalSet element critera
$criteria = craft()->elements->getCriteria(ElementType::GlobalSet);
// Set the handle you want
$criteria->handle = 'settings';
// Find the settings set
$settings = $criteria->first();

// Access the address field
var_dump($settings->address)

You can access all fields on the $settings variable using this synthax.

Mario

share|improve this answer
    
Thanks for the quick and concise answer Mario. They both worked like a charm. I think I'll use use your first suggestion though... as it's more tidy and does exactly what I need. –  Mark Aug 5 at 13:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.