7

I am a Joomla developer who has been asked to start developing Craft plugins and I'm afraid my brain is so moulded to the joomla way of doing things that I cannot even see how the template links to the plugin and fires events

I'm especially interested in form processing (being the cornerstone of application functionality) and I've been taking a look at the contact form plugin. In the contact.html template I can see {{ getCsrfInput() }} which is Twig syntax for calling a method- but there is no method called getCsrfInput() anywhere in the plugin. However it works, hidden fields are returned with an 'action' pointing to contactForm/sendMessage which (I'm assuming the Craft app decides, because there is no form tag action) fires the actionSendMessage event in the contact form plugin.

I just don't see the sequence! I can program PHP fine, but (perhaps due to ONLY doing things the Joomla way) don't see how plugins are called/included in a page.

It's a silly example, but could someone explain to me how I could get a function to just echo Hello World on a page:

function printHello()
{
echo 'Hello world!'
}

Would this go in the plugin root class? If so, what do I put in the example.html template to call this plugin function?

If I can do that then I can (presumably) use it to do everything else. Just 'including' a PHP function on a page doesn't seem like the Craft way of doing thing- I've read about hooks and events, and they don't make sense to me. Hooks seem to link a plugin to a URI, and it seems to suggest that you don't need to put anything into the template html calling the plugin. The Other, 'Events', is all PHP, so I'm assuming isn't intended to be put into the template files (Any php I put in there is escaped).

So, in short, how can add a hello world output to a page using a plugin (Stupid, but just to get me started)

My thought is that as soon as I can do that I can have a function that grabs form data from a previous page and processes it in order to do some real work.

Thank you so much for your help!

Andy

1
  • Welcome to the Craft CMS Stack Exchange, Andy! Please consider accepting the answer Douglas provided if it was helpful, or post your own answer if you achieved "Hello World" and can help other Joomla-Craft-plugin-developers along!
    – Matt Stein
    May 10, 2017 at 0:16

1 Answer 1

8

Apart from reading the docs on plugin development, I would suggest checking out a plugin called Business Logic, which provides a plugin template for creating project specific business logic, with a basic controller, service, and variables file already set up, and doc blocks which explain what the various components do.

For just printing a string I would look into creating a custom variable in your plugins 'variable' file. If the method is more complicated then you can use your variable as a wrapper to call one your service methods defined in your plugin's 'service' file. Here is an example variables file, with a call to a service method:

<?php namespace Craft;

class MypluginVariable
{
    // return the result directly
    public function exampleVariable()
    {
        return 'Anything you want!';
    }
    // call a service method and return the result
    public function exampleServiceMethodWrapper($value)
    {
        return craft()->myPlugin->myServiceMethod($value);
    }

}

You could call these from your template using {{ craft.myplugin.exampleVariable }} and {{ craft.myplugin.exampleServiceMethodWrapper('customValue') }}. These are assuming you want to just print the result of course.

The contactForm plugin that you are looking at is interacting with the plugin through the hidden form field <input type="hidden" name="action" value="contactForm/sendMessage"> which directs craft to submit the form to the contactForm's actionSendMessage() method defined in the plugin's 'controller' file. See controller docs for more info.

Fyi {{ getCsrfInput() }} is a native craft method so won't be in the plugin.

Your Answer

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

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