Answers.Body: <p>I do believe you're missing the handle to the top-level Matrix field when you're passing into <code>setContentFromPost</code>. You should be doing:</p>
  
  <p><code>$couponCodeMatrix-&gt;setContentFromPost(array('couponCodes', $newMatrixData));</code></p>
  
  <p>Otherwise, you're just passing:</p>
  
  <pre><code>'45623':
      'type': 'blockTypeHandle'
      'enabled': true,
      'fields': []
  </code></pre>
  
  <p>to the global set, instead of:</p>
  
  <pre><code>'couponCodes':
      '45623':
          'type': 'blockTypeHandle'
          'enabled': true,
          'fields': []
  </code></pre>
  
  <p>To deal with fetching even disabled blocks, you'll need to actually query the Matrix field's block directly, rather than looking just at the Global Sets content.</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::MatrixBlock);
  $criteria-&gt;fieldId = craft()-&gt;fields-&gt;getFieldByHandle('couponCodes')-&gt;id;
  $criteria-&gt;ownerId = $couponCodeMatrix-&gt;id; // This is your Global Set id
  $criteria-&gt;status = null;
  $blocks = $criteria-&gt;find();
  
  foreach($blocks as $block) { }
  </code></pre>
  
  <p>More complete code for anyone interested. We loop through the matrix and recreate it only if we updated the disabled status on the matching item. </p>
  
  <p>Thanks again!</p>
  
  <pre><code>    $couponCodeMatrix = craft()-&gt;globals-&gt;getSetByHandle('couponCodes');
  
      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::MatrixBlock);
      $criteria-&gt;fieldId = craft()-&gt;fields-&gt;getFieldByHandle('couponCodes')-&gt;id;
      $criteria-&gt;ownerId = $couponCodeMatrix-&gt;id; // This is your Global Set id
      $criteria-&gt;status = null;
      $matrixBlocks = $criteria-&gt;find();
  
      $b_updateMatrix = false;
      $o_foundCoupon = false;
      $newMatrixData = array('couponCodes'=&gt;array());
      foreach($matrixBlocks as $block){
  
          $b_enabled = $block-&gt;enabled;
          if ($this-&gt;couponCheckMatch($s_code, $block)) {
              $o_foundCoupon = $block;
              if($block-&gt;singleUse==1){
                  $b_updateMatrix=true;
                  $b_enabled = false;
              }
          }
  
          $blockData = array(
              'type' =&gt; $block-&gt;getType()-&gt;handle,
              'enabled' =&gt; $b_enabled,
              'fields' =&gt; array(
                  'couponCode' =&gt; $block-&gt;couponCode,
                  'couponType' =&gt; $block-&gt;couponType,
                  'couponValue' =&gt; $block-&gt;couponValue,
                  'expiryDate' =&gt; $block-&gt;expiryDate,
                  'singleUse' =&gt; $block-&gt;singleUse,
              )
          );
          $newMatrixData['couponCodes'][$block-&gt;id] = $blockData;
      }
      if($b_updateMatrix) {
          $couponCodeMatrix-&gt;setContentFromPost($newMatrixData);
          craft()-&gt;globals-&gt;saveContent($couponCodeMatrix);
      }  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-19T04:40:02.570Z
Answers.Text: Oh... I was so close. That worked perfect.
Answers.UserDisplayName: Walker Designs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-19T04:42:21.653Z
Answers.Text: Interestingly when I call
  ​​
  $couponCodeMatrix = craft()->globals->getSetByHandle('couponCodes');
  
  it returns only those matrix entries that are enabled (hence whenever I run this script now it will delete anything which is not enabled).
  
  I can work around this but is there a better way to grab all rows in the matrix including disabled ones?
Answers.UserDisplayName: Walker Designs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-19T06:03:23.853Z
Answers.Text: See my updated answer
Answers.UserDisplayName: crawf
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-19T01:38:57.853Z
Answers.LastActivityDate: 2016-01-19T22:59:23.753Z
Answers.LastEditDate: 2016-01-19T22:59:23.753Z
Answers.UserDisplayName: crawf
Body: <p>We are working on a plugin that needs to update the contents of a Matrix field within Globals. I've found little snippets of code but none that do exactly this. Can anyone provide some code or point me to some code that does such? I can't seem to figure it out. Code sample of what I have so far is below. This does not result in any sort of area, but also does not seem to do anything.</p>
  
  <p>As you can see, I'm trying to force all <code>couponCodes</code> to be 'BBB' whilst copying all other values. Maybe there is a much better way? It would be nice to just update the code value without recreating the full set of data if possible.</p>
  
  <pre><code>$couponCodeMatrix = craft()-&gt;globals-&gt;getSetByHandle('couponCodes');
  $newMatrixData = array();
  
  foreach($couponCodeMatrix-&gt;couponCodes as $block) {
      $blockData = array(
          'type' =&gt; $block-&gt;getType()-&gt;handle,
          'enabled' =&gt; true,
          'fields' =&gt; array(
              'couponCode' =&gt; 'BBB',
              'couponType' =&gt; $block-&gt;couponType,
              'couponValue' =&gt; $block-&gt;couponValue,
              'expiryDate' =&gt; $block-&gt;expiryDate,
              'singleUse' =&gt; $block-&gt;singleUse,
          )
      );
  
      $newMatrixData[$block-&gt;id] = $blockData;
  }
  
  $couponCodeMatrix-&gt;setContentFromPost($newMatrixData);
  
  craft()-&gt;globals-&gt;saveContent($couponCodeMatrix);
  </code></pre>
  
Tags: <plugin-development><matrix><globals>
Title: Update globals Matrix via a plugin
ThreadText: # Update globals Matrix via a plugin
  
  (Asked by: username_0 on 2016-01-19)
  
  We are working on a plugin that needs to update the contents of a Matrix field within Globals. I've found little snippets of code but none that do exactly this. Can anyone provide some code or point me to some code that does such? I can't seem to figure it out. Code sample of what I have so far is below. This does not result in any sort of area, but also does not seem to do anything.
  
  As you can see, I'm trying to force all `couponCodes` to be 'BBB' whilst copying all other values. Maybe there is a much better way? It would be nice to just update the code value without recreating the full set of data if possible.
  
  ```
  $couponCodeMatrix = craft()->globals->getSetByHandle('couponCodes');
  $newMatrixData = array();
  
  foreach($couponCodeMatrix->couponCodes as $block) {
      $blockData = array(
          'type' => $block->getType()->handle,
          'enabled' => true,
          'fields' => array(
              'couponCode' => 'BBB',
              'couponType' => $block->couponType,
              'couponValue' => $block->couponValue,
              'expiryDate' => $block->expiryDate,
              'singleUse' => $block->singleUse,
          )
      );
  
      $newMatrixData[$block->id] = $blockData;
  }
  
  $couponCodeMatrix->setContentFromPost($newMatrixData);
  
  craft()->globals->saveContent($couponCodeMatrix);
  
  ```
  
  **Tags:** plugin-development, matrix, globals
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-01-19. Score: 1 | Accepted answer
  
  I do believe you're missing the handle to the top-level Matrix field when you're passing into `setContentFromPost`. You should be doing:
  
  `$couponCodeMatrix->setContentFromPost(array('couponCodes', $newMatrixData));`
  
  Otherwise, you're just passing:
  
  ```
  '45623':
      'type': 'blockTypeHandle'
      'enabled': true,
      'fields': []
  
  ```
  
  to the global set, instead of:
  
  ```
  'couponCodes':
      '45623':
          'type': 'blockTypeHandle'
          'enabled': true,
          'fields': []
  
  ```
  
  To deal with fetching even disabled blocks, you'll need to actually query the Matrix field's block directly, rather than looking just at the Global Sets content.
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::MatrixBlock);
  $criteria->fieldId = craft()->fields->getFieldByHandle('couponCodes')->id;
  $criteria->ownerId = $couponCodeMatrix->id; // This is your Global Set id
  $criteria->status = null;
  $blocks = $criteria->find();
  
  foreach($blocks as $block) { }
  
  ```
  
  More complete code for anyone interested. We loop through the matrix and recreate it only if we updated the disabled status on the matching item.
  
  Thanks again!
  
  ```
      $couponCodeMatrix = craft()->globals->getSetByHandle('couponCodes');
  
      $criteria = craft()->elements->getCriteria(ElementType::MatrixBlock);
      $criteria->fieldId = craft()->fields->getFieldByHandle('couponCodes')->id;
      $criteria->ownerId = $couponCodeMatrix->id; // This is your Global Set id
      $criteria->status = null;
      $matrixBlocks = $criteria->find();
  
      $b_updateMatrix = false;
      $o_foundCoupon = false;
      $newMatrixData = array('couponCodes'=>array());
      foreach($matrixBlocks as $block){
  
          $b_enabled = $block->enabled;
          if ($this->couponCheckMatch($s_code, $block)) {
              $o_foundCoupon = $block;
              if($block->singleUse==1){
                  $b_updateMatrix=true;
                  $b_enabled = false;
              }
          }
  
          $blockData = array(
              'type' => $block->getType()->handle,
              'enabled' => $b_enabled,
              'fields' => array(
                  'couponCode' => $block->couponCode,
                  'couponType' => $block->couponType,
                  'couponValue' => $block->couponValue,
                  'expiryDate' => $block->expiryDate,
                  'singleUse' => $block->singleUse,
              )
          );
          $newMatrixData['couponCodes'][$block->id] = $blockData;
      }
      if($b_updateMatrix) {
          $couponCodeMatrix->setContentFromPost($newMatrixData);
          craft()->globals->saveContent($couponCodeMatrix);
      }  
  
  ```
  
  > **Comments:**
  > **username_0**: Oh... I was so close. That worked perfect.  
  > **username_0**: Interestingly when I call ​​ $couponCodeMatrix = craft()->globals->getSetByHandle('couponCodes');  it returns only those matrix entries that are enabled (hence whenever I run this script now it will delete anything which is not enabled).  I can work around this but is there a better way to grab all rows in the matrix including disabled ones?  
  > **username_1**: See my updated answer  
  
  ---
  
  