Answers.Body: <p>As you discovered, you can get the current element that a field type is getting applied to via <code>$this-&gt;element</code>. However you must keep in mind that that property will not always be set. So make sure that your PHP/template code is defensive about the possibility that there is no known element yet.</p>
  
  <p>Another assumption to avoid is that the current element is going to be an entry. Craft lets you associate fields with any type of element - entries, users, assets, categories, tags, globals, and Matrix blocks, and even any custom element types that plugins may add. So <code>$this-&gt;element-&gt;section</code> should be avoided unless you know that <code>$this-&gt;element</code> is actually an EntryModel; otherwise that will give you a PHP error.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-21T18:15:53.867Z
Answers.Text: Thank you brandon! Those are all great points. I will make sure and update my plugin to test for the element type. When might the element not be known yet?
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-21T18:43:02.737Z
Answers.Text: When creating new entries, for example. And sometimes `$this->element` might be set, but the element doesn’t actually have an ID yet, because it hasn’t been saved, such as when creating a new entry, attempting to save it, but there was a validation error.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-21T17:55:52.673Z
Answers.LastActivityDate: 2014-10-21T17:55:52.673Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>This might provide it's own answer, but I thought I would ask to be sure. How do you reference the current entry in custom fieldType or CP templates? I have tried <code>{{ entry.id }}</code>, <code>{{ element.id }}</code> but neither work. Do you need to explicitly set these variables in your <code>getInputHTML()</code> function?</p>
  
  <p>I see in the craft '_entry.html' template that {{ entry.id }} is used for a hidden field. Since that reference is not available, I assume that template variables are scoped to the template and not inherited by other templates on the same page?</p>
  
  <p>Through trial and error I finally discovered the current entry can be referenced in PHP as <code>$this-&gt;element;</code>, and the id and section via <code>$this-&gt;element-&gt;id</code>, <code>$this-&gt;element-&gt;section</code> respectively. So, I could add <code>entry</code> as a template variable, but I just assumed it would already be there.</p>
  
  <pre><code>public function getInputHtml($name, $value)
  {
      $entry = $this-&gt;element;
      return craft()-&gt;templates-&gt;render('myPlugin/fieldtypes/MyField/input', array(
          'name'          =&gt; $name,
          'value'         =&gt; $value,
          'entry'         =&gt; $entry
      );
  }
  </code></pre>
  
Tags: <plugin-development><control-panel><custom-fieldtype>
Title: How to reference the current entry element in custom Control Panel templates?
ThreadText: # How to reference the current entry element in custom Control Panel templates?
  
  (Asked by: username_0 on 2014-10-13)
  
  This might provide it's own answer, but I thought I would ask to be sure. How do you reference the current entry in custom fieldType or CP templates? I have tried `{{ entry.id }}`, `{{ element.id }}` but neither work. Do you need to explicitly set these variables in your `getInputHTML()` function?
  
  I see in the craft '\_entry.html' template that {{ entry.id }} is used for a hidden field. Since that reference is not available, I assume that template variables are scoped to the template and not inherited by other templates on the same page?
  
  Through trial and error I finally discovered the current entry can be referenced in PHP as `$this->element;`, and the id and section via `$this->element->id`, `$this->element->section` respectively. So, I could add `entry` as a template variable, but I just assumed it would already be there.
  
  ```
  public function getInputHtml($name, $value)
  {
      $entry = $this->element;
      return craft()->templates->render('myPlugin/fieldtypes/MyField/input', array(
          'name'          => $name,
          'value'         => $value,
          'entry'         => $entry
      );
  }
  
  ```
  
  **Tags:** plugin-development, control-panel, custom-fieldtype
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-10-21. Score: 5 | Accepted answer
  
  As you discovered, you can get the current element that a field type is getting applied to via `$this->element`. However you must keep in mind that that property will not always be set. So make sure that your PHP/template code is defensive about the possibility that there is no known element yet.
  
  Another assumption to avoid is that the current element is going to be an entry. Craft lets you associate fields with any type of element - entries, users, assets, categories, tags, globals, and Matrix blocks, and even any custom element types that plugins may add. So `$this->element->section` should be avoided unless you know that `$this->element` is actually an EntryModel; otherwise that will give you a PHP error.
  
  > **Comments:**
  > **username_0**: Thank you username_1! Those are all great points. I will make sure and update my plugin to test for the element type. When might the element not be known yet?  
  > **username_1**: When creating new entries, for example. And sometimes `$this->element` might be set, but the element doesn’t actually have an ID yet, because it hasn’t been saved, such as when creating a new entry, attempting to save it, but there was a validation error.  
  
  ---
  
  