1

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

| improve this question | | | | |
2

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 }}
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.