Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I have limited knowledge of PHP but I would like to try and create a widget. Basically all it needs to do is provide some text and a link to the user profile page as you can't route directly to that page on login. Would someone be able to point me in the write direction please. I have in my grWidget.php file:

<?php

namespace Craft;

class GrWidget extends BasePlugin
{
  function getName()
    {
      return Craft::t('Gareth Widget');
    }

    function getVersion()
    {
      return '1.0';
    }

    function getDeveloper()
    {
      return 'Gareth Redfern';
    }

    function getDeveloperUrl()
    {
      return 'http://garethredfern.com';
    }
}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

I think you are mixing up your Primary Plugin Class with your Dashboard Widget.

Plugin Structure:

Assuming your plugin is called Gareth and your widget Gr, this files should be in your /craft/plugins/gareth/ directory:

  • GarethPlugin.php
  • templates/
    • GrWidget.html
  • widgets/
    • Gareth_GrWidget.php

Primary Plugin Class: GarethPlugin.php

Your primary plugin class defines all of the things that Craft needs to know about your plugin, such as the plugin’s name, version number, developer’s name and URL, and Global Settings.

<?php
namespace Craft;

class GarethPlugin extends BasePlugin
{
    function getName()
    {
         return Craft::t('Gareth');
    }

    function getVersion()
    {
        return '1.0';
    }

    function getDeveloper()
    {
        return 'Gareth Redfern';
    }

    function getDeveloperUrl()
    {
        return 'http://garethredfern.com';
    }
}

Widget Class: Gareth_GrWidget.php

This class defines the widget types name getName() and its content getBodyHtml().

<?php
namespace Craft;

class Gareth_GrWidget extends BaseWidget
{
    public function getName()
    {
        return Craft::t('Gr');
    }

    public function getBodyHtml()
    {
        return craft()->templates->render('gareth/GrWidget');
    }
}

Widget Template: GrWidget.html

This is just some text which will be shown inside the GrWidget.
<a href="{{ url('myaccount') }}">A link to my user profile</a>

Result:

How the example Widget will look like


More:

Check out the Widget documentation. Also take a look at Craft's default widgets which can be found in the craft/app/widgets directory.

share|improve this answer
    
Perfect, thank you Victor. You are right I wasn't really sure where to start but you have put me on the right path now. I hadn't seen the widget docs either so my bad there. –  Gareth Redfern Jul 20 at 16:24
    
That is useful, the super simple happy brad widget also helped a lot github.com/mattstauffer/happybrad –  noregt Aug 15 at 13:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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