<p><strong><em>Way one:</em></strong>
Inside the field class, there is a <code>inputTemplateVariables</code> function with two params, like this</p>

<pre><code>protected function inputTemplateVariables($value = null, ElementInterface $element = null): array
{
     $vars = parent::inputTemplateVariables($value, $element);

     $settings = $this-&gt;getSettings();

     //here you can get element
     $entryId = $element-&gt;id;

     return $vars;
}
</code></pre>

<p><strong><em>Way two:</em></strong>
In your plugin's main file, put codes below into <code>init()</code> function</p>

<pre><code>Craft::$app-&gt;getView()-&gt;hook('cp.entries.edit.details', function (&amp;$context) 
{

     //here you can also get the element id
     //you can save it into session and use it anywhere you need
     $id = $context['entry']['id'];
});
</code></pre>

CC BY-SA 4.0
2018-06-08T00:36:04.753Z
2018-06-08T00:36:04.753Z
yu xia
<p>It may depend on where in the request you need to access the element.</p>

<p>If you need to access the element in a Field class on your Entry Edit page, the <code>getInputHtml</code> method may be useful:</p>

<pre><code>public function getInputHtml($value, ElementInterface $element = null): string
{
    // $element stuff

    return Craft::$app-&gt;getView()-&gt;renderTemplate('my-plugin/field',
    [
        'field' =&gt; $this,

        // make the Element available in your field's input template
        'element' =&gt; $element
    ]);
}
</code></pre>

<p>If you need to access the element in a Field class after the Element is saved there is the <a href="https://docs.craftcms.com/api/v3/craft-base-field.html#method-afterelementsave" rel="nofollow noreferrer">afterSaveElement</a> method:</p>

<pre><code>public function afterElementSave(ElementInterface $element, bool $isNew)
{
    parent::afterElementSave($element, $isNew);

    // $element stuff
}
</code></pre>

<p>Note: The Entry ID may not exist while a new entry is being created. Accessing the Element via the <code>afterElementSave</code> ensures that the Entry has been saved and has an ID.</p>

CC BY-SA 4.0
2018-06-05T03:15:34.253Z
Thanks! But I hope I can get the entry id after I open the entry editing page in CP. So this function is not for me.
yu xia
CC BY-SA 4.0
2018-06-05T04:50:47.543Z
@yuxia then the getInputHtml https://docs.craftcms.com/api/v3/craft-base-field.html#method-getinputhtml might be something for you.
Robin Schambach
CC BY-SA 4.0
2018-06-06T13:35:52.620Z
I've updated my answer to include the `getInputHtml` method Robin mentions as well.
Ben Parizek
CC BY-SA 4.0
2018-06-06T23:23:50.953Z
@BenParizek Thanks for your code. But I don't understand what the `getInputHtml` do.  Maybe I didn't make myself clear.  Basically there are two Asset field types: A and B. Let's say a user selected three images to A, Ctrl+s to save and then he can only select these three images to B. The field B is what I want to create.

 `afterElementSave` has the limit which is users have to save to trigger this function.
yu xia
CC BY-SA 4.0
2018-06-07T03:11:19.433Z
@BenParizek You are absolutely right! I can get element via `getInputHtml`. But I have to rewrite the template in this function which is gonna take too much effort
yu xia
CC BY-SA 4.0
2018-06-05T02:53:30.857Z
2018-06-06T13:35:26.790Z
2018-06-06T13:35:26.790Z
Ben Parizek
<p>I create a new filed type and attached it to entries. Now I need to get the entry id which the field is attached to. </p>

<p>I tried <code>$this-&gt;element</code> in my field class, but got an error said that I'm calling an unknown property.</p>

<p>Any way to do that please?</p>

<plugin-development><field><fieldtypes><custom-fieldtype><custom-field>
How can I get entry id in field
# How can I get entry id in field

(Asked by: username_0 on 2018-06-05)

I create a new filed type and attached it to entries. Now I need to get the entry id which the field is attached to.

I tried `$this->element` in my field class, but got an error said that I'm calling an unknown property.

Any way to do that please?

**Tags:** plugin-development, field, fieldtypes, custom-fieldtype, custom-field

---

## 2 answers

### Answer by username_0 on 2018-06-08. Score: 0 | Accepted answer

Way one:
Inside the field class, there is a inputTemplateVariables function with two params, like this
protected function inputTemplateVariables($value = null, ElementInterface $element = null): array
{
     $vars = parent::inputTemplateVariables($value, $element);

     $settings = $this->getSettings();

     //here you can get element
     $entryId = $element->id;

     return $vars;
}

Way two:
In your plugin's main file, put codes below into init() function
Craft::$app->getView()->hook('cp.entries.edit.details', function (&$context) 
{

     //here you can also get the element id
     //you can save it into session and use it anywhere you need
     $id = $context['entry']['id'];
});


---

### Answer by username_1 on 2018-06-05. Score: 1

It may depend on where in the request you need to access the element.
If you need to access the element in a Field class on your Entry Edit page, the getInputHtml method may be useful:
public function getInputHtml($value, ElementInterface $element = null): string
{
    // $element stuff

    return Craft::$app->getView()->renderTemplate('my-plugin/field',
    [
        'field' => $this,

        // make the Element available in your field's input template
        'element' => $element
    ]);
}

If you need to access the element in a Field class after the Element is saved there is the afterSaveElement method:
public function afterElementSave(ElementInterface $element, bool $isNew)
{
    parent::afterElementSave($element, $isNew);

    // $element stuff
}

Note: The Entry ID may not exist while a new entry is being created. Accessing the Element via the afterElementSave ensures that the Entry has been saved and has an ID.

> **Comments:**
> **username_0**: Thanks! But I hope I can get the entry id after I open the entry editing page in CP. So this function is not for me.  
> **username_2**: @yuxia then the getInputHtml https://docs.craftcms.com/api/v3/craft-base-field.html#method-getinputhtml might be something for you.  
> **username_1**: I've updated my answer to include the `getInputHtml` method Robin mentions as well.  
> **username_0**: @BenParizek Thanks for your code. But I don't understand what the `getInputHtml` do.  Maybe I didn't make myself clear.  Basically there are two Asset field types: A and B. Let's say a user selected three images to A, Ctrl+s to save and then he can only select these three images to B. The field B is what I want to create.   `afterElementSave` has the limit which is users have to save to trigger this function.  
> **username_0**: @BenParizek You are absolutely right! I can get element via `getInputHtml`. But I have to rewrite the template in this function which is gonna take too much effort  

---

