<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>

CC BY-SA 3.0
2018-03-26T15:24:27.660Z
Now that's what's up.
Dan Zuzevich
CC BY-SA 3.0
2018-03-26T15:41:11.657Z
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.
Dan Zuzevich
CC BY-SA 3.0
2018-03-26T15:50:52.237Z
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?
Dan Zuzevich
CC BY-SA 3.0
2018-03-26T15:18:40.063Z
2018-03-26T16:33:44.823Z
2018-03-26T16:33:44.823Z
Robin Schambach
<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>

<matrix>
Get the handle of a Matrix block
# 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?  

---

