1

I am new to craft and I need help to understand how plugins work. I am trying inject these functions to the CraftVariable

{% for item in craft.plugin.exampleFunction(test).hide("noTitle") %}  

craft.plugin.exampleFunction(test) is working perfectly but .hide() is not. Can anybody tell me how to call this function?

8
  • Could you tell us what you want to achieve? What does your exampleFunction return? It should be an object with the function hide in it Commented Apr 17, 2018 at 7:07
  • Do you have any sample for similer? Commented Apr 17, 2018 at 7:12
  • Since I don't know you want to do I can't really provide any help. I can create a simple example but that won't help you Commented Apr 17, 2018 at 7:13
  • I just need to know how to access hide function as my exampleFunction is returning array. What changes i need to make here? Commented Apr 17, 2018 at 7:15
  • What should your hide function be? what should it do? Commented Apr 17, 2018 at 7:15

1 Answer 1

3

Based on the discussion you'll need to implement a fetch function similar like Craft does it with the ElementCriteriaModel / ElementQuery. So you'll return some kind of search object until you execute the .find()/fetch() (or whatever you like to call it) function

class ClassName{

    /** @var array $_searchObjects */
    private $_searchObjects;

    /**
     * @param $something
     *
     * @return $this
     */
    public function exampleFunction($something){
        $this->_searchObjects = ['test', 'insert', 'your', 'array', 'here'];

        return $this;
    }

    /**
     * @param $string
     *
     * @return $this
     */
    public function hide($string){
        // filter your elements
        $this->_searchObjects = ...//

        return $this;
    }

    /**
     * @return array
     */
    public function find(){
        return $this->_searchObjects;
    }
}

You can then use it like

{% set elements = craft.plugin.exampleFunction.find() %}
{% set filterElements = craft.plugin.exampleFunction.hide('something').find() %}
1
  • Thank you so much, it seems to be working for me. Thanks again !!. Commented Apr 17, 2018 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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