<p>A field in Craft can belong to any element (entry, tag, category, user, etc.).</p>

<p>If you want to get the ID of the element the field may or may not be currently attached to, you can do this from your Field Type class:</p>

<pre><code>$elementId = isset($this-&gt;element) ? $this-&gt;element-&gt;id : null;

if ($elementId)
{
    ...
}
</code></pre>

CC BY-SA 4.0
2018-06-05T00:32:37.223Z
how can I get the id in craft3 please? I created a field type and also need to get entry id that it's attached to.
I tried $this->element, but got an error said "element is an unknown property"
yu xia
CC BY-SA 3.0
2017-04-24T23:40:26.560Z
2017-04-24T23:40:26.560Z
Brad Bell
<p>I'm creating a FieldType plugin, but I need the entry's ID in the front-end, I suppose I can get it within the controller, something like</p>

<p>craft()->entryId</p>

<pre><code>$entryId = IN HERE;

$options = array(
    'coreUrl' =&gt; getenv('ENV_CORE_HOST'),
    'newEntry' =&gt; false,
    'entryId' =&gt; $entryId
);

craft()-&gt;templates-&gt;includeJs("$('#{$namespacedId}-field').whatever('" . json_encode($options) . "');");
</code></pre>

<p>I can get the ID in the URL with Regex and stuff, but I wouldn't like to rely on that.</p>

<p>And suggestion? </p>

<plugin-development>
Get entry ID within plugin controller
# Get entry ID within plugin controller

(Asked by: username_0 on 2017-04-24)

I'm creating a FieldType plugin, but I need the entry's ID in the front-end, I suppose I can get it within the controller, something like
craft()->entryId
$entryId = IN HERE;

$options = array(
    'coreUrl' => getenv('ENV_CORE_HOST'),
    'newEntry' => false,
    'entryId' => $entryId
);

craft()->templates->includeJs("$('#{$namespacedId}-field').whatever('" . json_encode($options) . "');");

I can get the ID in the URL with Regex and stuff, but I wouldn't like to rely on that.
And suggestion?

**Tags:** plugin-development

---

## 1 answers

### Answer by username_1 on 2017-04-24. Score: 1 | Accepted answer

A field in Craft can belong to any element (entry, tag, category, user, etc.).
If you want to get the ID of the element the field may or may not be currently attached to, you can do this from your Field Type class:
$elementId = isset($this->element) ? $this->element->id : null;

if ($elementId)
{
    ...
}

> **Comments:**
> **username_2**: how can I get the id in craft3 please? I created a field type and also need to get entry id that it's attached to. I tried $this->element, but got an error said "element is an unknown property"  

---

