Skip navigation

I’m currently working on a project that requires applying some modifications and creating plugins for SocialEngine 4. I’m new to socialengine so I’ll post here my findings during the work. Probably I’m not allowed to post the code in public, so I’m just going to post generic how tos that you could use to accomplish similar tasks.

My first task to is to allow users to post comments to other users’ profile. So the first challenge is to create a new tab where there would be a form, and other people comments displayed below it. More like the news feed, except that it shows and posts comments.

First thing to think about is where does this Widget belong? In my case, since it is displayed in a user’s profile then it should belong to the User module. So navigate to the Widgets folder inside the User module ({social_engine_root}/application/modules/User/widgets) and create a new directory with your widget’s name (mywidget for example). Create 2 files inside this directory:

  • Controller.php
  • index.tpl

As you’ve most probably figured out, SocialEngine uses the MVC architecture. So in this case the controller class lies in Controller.php and the view is in index.tpl. For the model I haven’t yet stepped into it so I’ll skip it now and come back to it when I find out more.

Controller.php:

<?php

class User_Widget_mywidgetController extends Engine_Content_Widget_Abstract {

public function indexAction()  {

$this->view->msg=”Hello Widget!”;

}
}

The indexAction() method is invoked whenever the widget is going to be displayed. So inside this method you should prepare data (if any) that should be passed to the view. To pass data to the view, use this convention $this->view->variablename=value like above, replacing variablename with any name of your choice, and value with the associated data.

Then in the view index.tpl:

<?php

echo $this->msg;

?>

Use this convention to get variables assigned in controller: $this->variablename

Now the next step is to tell the User module that it has got a shiny brand new widget. To do this edit the content file associated with User module: ({social_engine_root}/application/modules/User/settings/content.php)  and append the following at the very bottom of page (before the very last closing bracket ‘)’ )

array( ‘title’ => ‘My Widget’,

‘description’ => ‘This is a brand new User Widget’,

‘category’ => ‘User’,

‘type’ => ‘widget’,

‘name’ => ‘user.mywidget’,

),

always of course replacing “mywidget” with your widget’s name.

Now the Widget is registered with SocialEngine. I am going to add it under a new tab in User Profile. To do so, login as admin, click Admin from top menu and go to Layout Editor. Then Change Editing to Member profile, and in the Available Blocks scroll till you find the User section. If everything was done correctly you should find your widget under this section. Just drag it to the tabbed block and you’re done. Next up is how to create a model, and make the controller interact with it.

 

UPDATE:

I quote this from John Boehr’s comment:

If you want to create your own widgets, I’d recommend placing them in application/widgets instead. The widgets in the modules use a common content file, which would be replaced if you upgraded. The widgets in the application/widgets folder are designed to be atomic, so their files are not touched unless you specifically upgrade that widget (or never if it’s your own).

You can render a widget in a view script using:
echo $this->content()->renderWidget(‘user.list-online’, array(‘key’ => ‘value’));

Check out application/modules/Core/views/scripts/admin-index/index.tpl for examples in the code.

Great piece of info, thanks!

65 Comments

  1. Thanks so much for this tutorial! There’s so little around.

    One thing, surely there’s a better way to register the widget. There must be a function call somewhere to register it. Editing a core file to register our widget seems cumbersome…

    • I agree, it’s probably little cumbersome. But I got that from expecting several modules, in addition to 3rd party ones. They all follow the same method to register their widgets (by editing content.php).

      I’m going to post soon a Guide on how to create a module. Creating extensions in the form of modules is a much better approach and prevents messing up original SocialEngine files.

  2. The class should be:

    class User_Widget_MywidgetController

    if you named your widget my-widget, you would have the class:

    class User_Widget_MyWidgetController

    • Well it seems to be working however with both class names (User_Widget_MywidgetController and class User_Widget_mywidgetController)

      Thanks for the my-widget tip!

  3. TGALAL I love you so much!!
    i am SE newbie, you’ve posted the tutorial that i searchin for late this two weeks, its really help me out how to modified the existing module on Social Engine, keep on updated your blog, i am startin to be your FANS!! 😉

    Cheers from Indonesia!

  4. Hi,
    how can I call a widget in my custom controller (not a Layout Manager created page)

    thank you

  5. One thing I should have added before:

    If you want to create your own widgets, I’d recommend placing them in application/widgets instead. The widgets in the modules use a common content file, which would be replaced if you upgraded. The widgets in the application/widgets folder are designed to be atomic, so their files are not touched unless you specifically upgrade that widget (or never if it’s your own).

    You can render a widget in a view script using:
    echo $this->content()->renderWidget(‘user.list-online’, array(‘key’ => ‘value’));

    Check out application/modules/Core/views/scripts/admin-index/index.tpl for examples in the code.

    • That’s a great piece of info John, really thanks a lot! I’ve added it to the post.

    • Hi,

      would also be a good solution to place widget in your own modules, wouldn’t it? Like e.g. application/modules/MY_MODULE/widgets/

    • Also, for those of you that take this info to heart…

      If you’re going to create, or move, your widget into the application/widgets folder; there’s no settings/content.php file to edit to get the widget included in the layout editor blocks menu. It took me about 20 minutes of searching to figure out that you need to add a third file (manifest.php) to your widget’s folder. You can copy one from another widget, like clock/manifest.php, and then just modify the entries to match your widget. I commented out the “revision” & “repository” entries in the arrays because I don’t think they apply to my widget. I could be wrong though.

      I’m also not really sure about the “backwards compatibility” array either. I just mimicked it as best as I could. =D

      Many many thanks for this post. As a web developer it’s been driving me crazy working on my friend’s site within the confines of SE’s control panel.

      Todd

  6. Its very useful to me.

  7. Great blog! Am really enjoying this. There’s very little info around. Any ideas what values to pass in that renderWidget for custom menus?

    • Aren’t the custom menus widgets? If so then I guess they’re rendered the same way from a view:

      echo $this->content()->renderWidget(‘module_name.widget_name’, array(‘key’ => ‘value’));

      replace module_name and widget_name with the correct ones.

    • custom menus are driven by informations in the ENGINE4_CORE_MENUITEMS table.

  8. Ya I tried rendering as a widget. Unsure as to which values to pass identify the menu though. Funnily the code used to print all menus doesn’t use renderWidget.

    • Hi Jason.
      The normal flow is that a menu item is linked to a controller. In this controller’s view you can call the renderWidget method.
      The renderWindget() method is useful because you can delegate the rendering of a part of your view to the widget. In this way you can reuse code, too.

  9. Hi all,

    I want to add a new menu in the “Available blocks” column in the “Layout Editor” section of the SocialEngine Admin. I want to add a new menu to the predefined “Core”, “User”, “Widget” that are already present there.

    I tried to add a new mudule and in this module add a /settings/content.php file. But it’s content isn’t automatically added to the “availiable blocks”.

    Can some one get me directions?
    thank s a lot.

    • for a whole new module you must add a new row in the engine4_core_modules table. Make sure the name of the module directory must match the case in the row column.
      Reply kinfly provided by Clare@Se4.me forum

  10. Hi!

    I have a SocialEngine question and came across your blog through Google. I hope this question is relevant to this specific post.

    I would like to limit the number of items in my Activity Feed widget. Currently, if I add it to my home page, it displays too many.

    I found the following code in “/application/modules/Activity/widgets/feed/Controller.php” and changed 20 to 5, but didn’t seem to make a difference.

    getSetting(‘activity.length’, 20);

    Any clues?

    🙂

    Thanks!!

  11. Hi,

    I love your findings. One thing that I thing would be extremely helpful is how to pull values from custom profile fields. Have you had a go at that yet?

    Cheers.

  12. Can anybody get links to developer’s guide on creating modules? I found nothing in inet.

  13. Hi,

    I just want to make 2 seperate textpages in the footer (plans and affiliate). The site is multilanguage but according to the SE guys the normal translation doesn’t work in this case and a new html widget has to be created.
    So in a Dutch site I always get the English text even if a new phrase is added.
    Haven’t got a clue however what should be in a .tpl and php file for a new widget.

    Is there an easy way so that even I can understand it :)?

    Thanks
    Ruben

  14. Thanks a lot for this wonderful piece of information!

    Looking forward to the piece you will hopefully still about creating the model and letting the controller interact with it.

    Regards,

    Ruben

  15. How should we co in code to display a widget that is not part of the core module or the user module.

    As an Exemple , Id like to do something like that for the new user who signup :

    noneFriendCount
    content()->renderWidget(‘Inviter.home-inviter’) ?>

    The code doesnt crash the page , it simply wont load the widget.
    how to call another module aside from core/user with a widget name ?

    When i change my line for :

    content()->renderWidget(‘user.listpopular’) ?>

    It display without any problem …

    Thanks in advance !@!@!@!

    Regards,

    Yan on behalf of team populee.com

  16. I am able to register the widget, and it appears in the layout editor, but when I drag and drop it into the profile page, save the settings and then try to view it, Hello Widget does not appear where it should.

  17. Hi,

    Can I add use multiple action in widget controller if yes then how.

    Like I want to create a action like public function keywordAction how can i use or call it.

    Thanks

    • Hi, did you discover how?

    • Try adding a param “action” with the name of the action. If that doesn’t work then you can do it manually like so (example based on application/libraries/Engine/View/Helper/Content.php):

      $structure = array(
      ‘type’ => ‘widget’,
      ‘name’ => $name,
      ‘action’ => ( !empty($params[‘action’]) ? $params[‘action’] : ‘index’ ),
      );
      if( !empty($params) ) {
      $structure[‘request’] = new Zend_Controller_Request_Simple(‘index’,
      ‘index’, ‘core’, $params);
      }

      // Create element (with structure)
      $element = new Engine_Content_Element_Container(array(
      ‘elements’ => array($structure),
      ‘decorators’ => array(
      ‘Children’,
      ‘Container’
      )
      ));

      return $element->render();

  18. Hi Experts…I have a question..regarding calling widget.

    How can i set the form setting values which can be set through admin panel when we set widget particularly on page. I need to set that widget again but by above code like $this->content()-renderWidget() but how can i default value.

    for example:
    Recent blogs have default 5 limit of listing and but i need to set that blog widget by code but 10 limit.How can i pass that 10 limit parameter.

    Sorry for poor english. Hope you all understand.

    Thank you
    Regards
    Gitesh

  19. Hi,

    thanks for your post. There is little documentation about SE developing.

    One question. Is it possible to embed a widget in a page, via controller or template file or I must use the layout editor as unique way?

    Santi

  20. You should be able to do this:

    content()-renderWidget(‘widgetname’, array(
    ‘params’ => ‘value’
    )) ?>

    • Ok that should have been:

      <php $this->content()->renderWidget(‘widgetname’, array(
      ‘params’ => ‘value’
      )) ?>

      • I want to add a page with a google maps. I developed a module to do that as a normal page, with a controller and a view. That’s all right… but I want to add widgets from another modules, for example, profile/user widgets.

        It seems that the only way to do that is to create a new page for the layout editor in my module and convert my google maps in a widget to append to that page. I’m not sure if this is the only way. Is it?

        Thanks in advance.

        • There is a special widget in the core module called “content.” When placed on a page, it will be given the content that is generated by the controller. You only need to use this if you’re hooking into the Layout Editor. If you just want to statically place widgets on the page, just use the renderWidget method and place them wherever you want in the view script.

          • renderWidget seems don’t work to me. Can you paste me a real example with a profile widget, for example.

            thanks

          • I tried the ‘content’ widget and that is the final solution.

            Thanks 😉

            Social Engine is a good product but it needs more and better documentation

  21. Thank you so much for that !!!!!

  22. very useful to me thanks.

  23. Hi thanks for the tuts, totally up to speed with making custom widgets. I am in the process of making a custom profile information for the groups plugin and all seems to work fine with calling info from the database.

    what I would like to do is a similar custom widget for members profile information….but have got a bit stuck calling info from engine4_user_fields_options. I have added a drop down selection box in sign up form, this then appears on profile information as standard. For my custom widget I can lift information to display username, email and status using,

    subject()->username; ?>
    subject()->email; ?>
    subject()->status; ?>

    but am a bit stuck calling information from selection boxes, and other all information that is displayed on profile page.

    Anybody got some code hints I could use

    To view the custom group details/info widget http://www.1000fraggers.com/group/1 drop me a PM if you want this widget

    • Check the code in application/modules/User/widgets/profile-fields/*, that’s where the existing code to render the fields is.

      The simplest way to get some of the data is using:
      Engine_Api::_()->getApi(‘fields’, ‘core’)->getFieldsValuesByAlias($user);

      For select boxes, you’d then have to go and get the correct fields and options for each one, however.

  24. Well I have been messing around with the code, getting a bit stuck…..doh, dont know if its too early in the morning lol.
    Is there any chance you could help, say for example calling the ‘First Name’ then I can take it from there

    cheers again 🙂

  25. Thanks a lot ….!

  26. Hi dear,

    Mind blowing. Thank you so much for posting this tutorial. Please mail me your id i want to add you as a friend.
    thanks very much.

    Your best friend,
    Vaibhav Gade
    vaibhavgade2000@gmail.com

  27. your tutorial is great…It saved my alot of headache…
    thnx alot 🙂

  28. Hello everybody I’m new in this forum and I want to ask everybody a question:
    I want to duplicate the profile photo widget(beacuse i want to insert in every profile the possibility to add a second photo with the same structure of the photo profile) in a new widget block(which is situated in the layout editor). I created a new widget’s structure and I don’t know how to write the right code, which regard the new widget, to implement my new widget block.
    Besides I need an help as regard the saving of the new photos in the database.
    How can I do it?
    Thanks for your attention.

  29. Great Article and it would definitely help newbies like me 🙂 . Thanks ….

  30. After creating a widget, I added some Files (a Form etc.). If I open the URL containing the Widget (it’s a login widget meant to be shown on the start page), I get a Class not found Error. So I tried to adjust the package.json file adding an element with the according attributes (path, dir file perms etc). But it still doesn’t work. Do you have any idea if I have to include the added File somewhere especially? Thank your in advance. Nice tutorial btw 🙂

  31. Ooops! Problem fixed!

  32. Thank you.

  33. Thanks, good tutorial!!!

    One question. Anybody knows if it is possible to use an action helper from a widget controller? And how we can load it?

    Santi

  34. Hi,

    Can anyone help me. I am new to Social Engine and am not a developer. However, I want to link through to a third party website but capture the members ID in my URL. Something like http://www.masterscoreboard.co.uk/ListOfFutureCompetitions.php?CWID=2922&CUID= Does anyone know the variable to grab the members ID ? Any help is very much appreciated.

  35. My spouse and i have been so thrilled Ervin could complete his / her research using the ideas your puppy received through the web site. It’s not at all simplistic just to choose to be offering tips that many people today might have been selling. We really know we have got you to appreciate for this. The specific explanations you made, the simple site navigation, unquestionably the friendships your site give support to engender ( blank ) it is many extraordinary, and it is making our very own baby and our family imagine that the subject matter has become pleasurable, and that’s particularly vital. Thanks for all!

  36. hello

  37. Hi,
    I am new to social engine. I have developed a widget . I need to load that widget through AJAX. So I need the URL of that widget. Please can you help me out? Or I need a way in which we can properly use AJAX in Social Engine.


    Thanks

  38. How is widget in application/widgets folder created? I created one using the above way but didnt work.I found that theres no content.php file, rather theres a manifest file in other widgets folder in there.Can I make User widget (same as above)in the application/widgets ?

  39. vvvvvvvvvvvvvvv

  40. vvvvvvvvvvvvv

  41. great tutorial, you have just unlocked loads of ideas. I’m off to have a fiddle.

    Respect

  42. One of his dad’s favorite stories is the three of us walking past a television in the Castro district of San Francisco
    and Evan yelling, ‘Hey, that’s Mary Martin. Ask yourself, “when was the last time I took a walk on the wild side. He’s on (or over) the edge, bordering on rude and doesn’t seem to give a damn about anyone but himself – what exactly do women see in a guy like that.

  43. How can I create a comment widget for any new page I make for social engine?

  44. view->msg=’Hello Widget!’;

    }

    there a syntax error here I fixed it.


4 Trackbacks/Pingbacks

  1. […] This post was mentioned on Twitter by John Boehr, SocialEngine. SocialEngine said: We're still fleshing out our developer docs, but check these tutorials by "Just Another Developer" http://bit.ly/f8LCnT http://bit.ly/gfOsPe […]

  2. […] Creating a Widget […]

  3. By Social Engine 4: Modules | My blog on 23 Apr 2015 at 8:40 pm

    […] Creating a Widget […]

Leave a reply to John Boehr (@kutsujoku) Cancel reply