Answers.Body: <p>You can grab the <code>handle</code> of an element that has different types (<code>entries</code>, <code>matrix</code>) with <code>getType()-&gt;handle</code> for example:</p>
  
  <pre><code>$handle = $entry-&gt;getType()-&gt;handle
  </code></pre>
  
  <p>In your case</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::MatrixBlock);
  $criteria-&gt;relatedTo = array(someCoolId, anotherCoolId);
  $relatedStories = $criteria-&gt;find();
  foreach ($relatedStories as $block){
      $handle = $block-&gt;getType()-&gt;handle;
      switch ($handle){
          case 'foo':
              break;
      }
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Not sure what you mean exactly so I'll just insert some random code parts until something is correct.</p>
  
  <pre><code>// get the parent element -&gt; the owner of that block.
  $parent = $block-&gt;getOwner()
  
  // get the field that relates both elements
  $field = craft()-&gt;fields-&gt;getFieldById($block-&gt;fieldId);
  $fieldHandle = $field-&gt;handle;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-26T15:24:27.660Z
Answers.Text: Now that's what's up.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-26T15:41:11.657Z
Answers.Text: Actually, I needed to deselect this as the selected answer. What I actually meant in my question, was, can I get the handle of the actual top level Matrix field, not the handle of every block in the matrix.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-26T15:50:52.237Z
Answers.Text: For example, Say I have a field called, "coolStuff" which is a matrix, and that matrix is one of the matrixes included in the array sent back to me from my query. Can I get the handle of "coolStuff" when looping through this array?
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-26T15:18:40.063Z
Answers.LastActivityDate: 2018-03-26T16:33:44.823Z
Answers.LastEditDate: 2018-03-26T16:33:44.823Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I am performing the following query, which returns me an array of matrix blocks:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::MatrixBlock);
  $criteria-&gt;relatedTo = array(someCoolId, anotherCoolId);
  $relatedStories = $criteria-&gt;find();
  </code></pre>
  
  <p>When looping through this array, I was wondering if it was possible to access the handle of the matrix block that I am currently in. Reason being is I would like to retrieve this as a string, and perform some type of work on it.</p>
  
Tags: <matrix>
Title: Get the handle of a Matrix block
ThreadText: # Get the handle of a Matrix block
  
  (Asked by: username_0 on 2018-03-26)
  
  I am performing the following query, which returns me an array of matrix blocks:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::MatrixBlock);
  $criteria->relatedTo = array(someCoolId, anotherCoolId);
  $relatedStories = $criteria->find();
  
  ```
  
  When looping through this array, I was wondering if it was possible to access the handle of the matrix block that I am currently in. Reason being is I would like to retrieve this as a string, and perform some type of work on it.
  
  **Tags:** matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-26. Score: 3 | Accepted answer
  
  You can grab the `handle` of an element that has different types (`entries`, `matrix`) with `getType()->handle` for example:
  
  ```
  $handle = $entry->getType()->handle
  
  ```
  
  In your case
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::MatrixBlock);
  $criteria->relatedTo = array(someCoolId, anotherCoolId);
  $relatedStories = $criteria->find();
  foreach ($relatedStories as $block){
      $handle = $block->getType()->handle;
      switch ($handle){
          case 'foo':
              break;
      }
  }
  
  ```
  
  Edit:
  
  Not sure what you mean exactly so I'll just insert some random code parts until something is correct.
  
  ```
  // get the parent element -> the owner of that block.
  $parent = $block->getOwner()
  
  // get the field that relates both elements
  $field = craft()->fields->getFieldById($block->fieldId);
  $fieldHandle = $field->handle;
  
  ```
  
  > **Comments:**
  > **username_0**: Now that's what's up.  
  > **username_0**: Actually, I needed to deselect this as the selected answer. What I actually meant in my question, was, can I get the handle of the actual top level Matrix field, not the handle of every block in the matrix.  
  > **username_0**: For example, Say I have a field called, "coolStuff" which is a matrix, and that matrix is one of the matrixes included in the array sent back to me from my query. Can I get the handle of "coolStuff" when looping through this array?  
  
  ---
  
  