Answers.Body: <p>It seems like what you want is to get elements related to an entry that is pre-loaded in a template, and that <code>elementId</code> is a custom field for <code>MyElementType</code>. If that's the case, this should work out:</p>
  
  <pre><code>public function totalitemsForElements($elementID)
  {
      $criteria = craft()-&gt;elements-&gt;getCriteria('MyElementType');
  
      $criteria-&gt;elementId = $elementID;
  
      return $criteria-&gt;find();
  }
  </code></pre>
  
  <p>We'll pass the entry id to the variable function. Here's the template code:</p>
  
  <pre><code>{% set itemElementType =  craft.pluginName.totalitemsForElements(entry.id) %}
  {{ itemElementType|length }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-16T19:24:43.407Z
Answers.LastActivityDate: 2017-05-16T19:24:43.407Z
Answers.UserDisplayName: Dan Ott
Body: <p>I need to get the current entry ID in plugin's variable file without submitting any form. I tried this but it gave an error <strong>Undefined property: Craft\PluginNameVariable::$element</strong></p>
  
  <pre><code>namespace Craft;
  
  class PluginNameVariable
  {
  /**
   */
  public function totalitemsForElements($elements = array())
  {
      $criteria = craft()-&gt;elements-&gt;getCriteria('MyElementType');
  
      $criteria-&gt;elementId = $this-&gt;element-&gt;id;
  
      $items = $criteria-&gt;find();
  
      return $criteria-&gt;find();
  }
  }
  </code></pre>
  
  <p>I was accessing this variable through</p>
  
  <pre><code>{% set itemElementType =  craft.pluginName.totalitemsForElements %}
  {{ itemElementType|length }}
  </code></pre>
  
  <p>Please Help. Thanks in advance !!</p>
  
Tags: <plugin-development><variable>
Title: How to get Current Entry ID in Plugin Variable?
ThreadText: # How to get Current Entry ID in Plugin Variable?
  
  (Asked by: username_0 on 2017-05-16)
  
  I need to get the current entry ID in plugin's variable file without submitting any form. I tried this but it gave an error **Undefined property: Craft\PluginNameVariable::$element**
  
  ```
  namespace Craft;
  
  class PluginNameVariable
  {
  /**
   */
  public function totalitemsForElements($elements = array())
  {
      $criteria = craft()->elements->getCriteria('MyElementType');
  
      $criteria->elementId = $this->element->id;
  
      $items = $criteria->find();
  
      return $criteria->find();
  }
  }
  
  ```
  
  I was accessing this variable through
  
  ```
  {% set itemElementType =  craft.pluginName.totalitemsForElements %}
  {{ itemElementType|length }}
  
  ```
  
  Please Help. Thanks in advance !!
  
  **Tags:** plugin-development, variable
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-05-16. Score: 2 | Accepted answer
  
  It seems like what you want is to get elements related to an entry that is pre-loaded in a template, and that `elementId` is a custom field for `MyElementType`. If that's the case, this should work out:
  
  ```
  public function totalitemsForElements($elementID)
  {
      $criteria = craft()->elements->getCriteria('MyElementType');
  
      $criteria->elementId = $elementID;
  
      return $criteria->find();
  }
  
  ```
  
  We'll pass the entry id to the variable function. Here's the template code:
  
  ```
  {% set itemElementType =  craft.pluginName.totalitemsForElements(entry.id) %}
  {{ itemElementType|length }}
  
  ```
  
  
  ---
  
  