2

I’m trying to set data within a global set. I’ve tried the following without success:

$globalSetHandle = 'settings';
$globalSet = craft()->globals->getSetByHandle($globalSetHandle);

$globalSet->fieldHandle = 'value';

craft()->globals->saveContent(GlobalSetModel $globalSet);
James
  • 1,138
  • 8
  • 20

2 Answers2

3

Try using setContentFromPost, as mentioned in this answer from Brandon.

$content = array(
    'fieldHandle' => 'value',
);

$globalSet->setContentFromPost($content);
craft()->globals->saveContent($globalSet);
Paul
  • 6,338
  • 12
  • 26
0

You can also use a front-end form:

<form method="post">
  {{ getCsrfInput() }}
  <input type="hidden" name="action" value="globals/saveContent">
  <input type="hidden" name="setId" value="123"> 
  <input type="text" name="fields[fieldname]" value="{{globalsetslug.fieldname}}">
  <input type="text" name="fields[anotherfieldname]" value="{{globalsetslug.anotherfieldname}}">
  <input type="submit" value="Save globals" />
</form>

Of course you need to replace 123 by your globals set id and globalsetslug by your globals set slug. You are not required to include all globals set fields in your form, Craft will save only those inside the form.

I know it's an old post, but perhaps this will help others!

r-ninja
  • 147
  • 10