1

I am having a hard time figuring out how the exactly the routing works in JomSocial. Does anyone know how to create a new view?

codingbiz
  • 26,179
  • 8
  • 59
  • 96
Matt Campbell
  • 289
  • 2
  • 9

2 Answers2

4

First of all, you create a controller to make a request of a view:

file: controllers/hello.php

        <?php
        // Check to ensure this file is included in Joomla!
        defined('_JEXEC') or die();
        class CommunityHelloController extends CommunityBaseController
           {
        function helloWorld()  //index.php?option=com_community&view=hello&task=helloWorld
             {

                $document   = JFactory::getDocument();
                $viewType   = $document->getType();
                        $view = $this->getView('hello', '' , $viewType);          
                        echo $view->get('helloWorld');  
             }

 function hello()  //index.php?option=com_community&view=hello&task=hello
         {

            $document   = JFactory::getDocument();
            $viewType   = $document->getType();
                    $view = $this->getView('hello', '' , $viewType);          
                    echo $view->get('helloWorld');  
         }
       }
?>

View: views/hello/view.html.php Here you place variables that will be passed to template file For example:

    <?php
    defined('_JEXEC') or die('Restricted access');
    jimport ( 'joomla.application.component.view' );

    class CommunityViewHello extends CommunityView {

    function helloWorld()  //This function shows a "Hello World" without an template view
       {

          echo 'Hello World';
       }


    function hello()

    {
        $user = CFactory::getUser($userid);

        $tmpl = new CTemplate( );   //Call a template file

       echo $tmpl->set ( 'user', $user )
                 ->fetch ( 'hello' ); // Returns the templates/default/hello.php file

    }
}

File templates/default/hello.php:

<?php   defined('_JEXEC') or die();?>
<h2> This is an example </h2>
<div class="container">
<p> Hello, <?php echo $user->name; ?></p>
</div>

That's all!

Thavia Farias
  • 261
  • 1
  • 11
  • I have been searching for this answer for a while so thank you. However when I try to navigate to index.php?option=com_community&view=hello&task=hello it simply redirects me back to index.php. Have I missed something? – 0x6C77 Dec 13 '13 at 12:42
  • I think I have figured it out ... `view.html` needs to be `view.html.php` – 0x6C77 Dec 13 '13 at 13:17
0

I may have made this as a comment to the answer given by @Thavia-Farias in 2013, but my reputation is not high enough to comment. The content of my answer will restate her information along with crucial new information, corrections, and enhancements according to my experience using Jomsocial 4.2.1:

First off, the controllers/hello.php as provided by @Thavia-Farias has an error: In both the function helloWorld() and function helloWorld()function hello(), the final line is echo $view->get('helloWorld');, but the function in function hello() should be echo $view->get('hello');. As it stands, both *index.php?option=com_community&view=hello&task=helloworld and index.php?option=com_community&view=hello&task=hello will both call the helloworld view, rather than the second one calling the hello view as it should.

Also, in my experience, rather than putting the template at the path /templates/default/hello.php, I put it at /templates/customtemplatename/html/com_community/layouts if you are using a cusomt template or /components/com_community/templates/jomsocial/layouts/ if you are using the default jomsocial template.

create /components/com_community/controllers/hello.php:

<?php
defined('_JEXEC') or die();
class CommunityHelloController extends CommunityBaseController
{   
        public function renderView($viewfunc, $var = NULL) {
        $my = CFactory::getUser();
        $jinput = JFactory::getApplication()->input;
        $document = JFactory::getDocument();
        $viewType = $document->getType();
        $viewName = $jinput->get('view', $this->getName());
        $view = $this->getView($viewName, '', $viewType);
        echo $view->get($viewfunc, $var);
    }

    function helloWorld()
    {
        $this->renderView(__FUNCTION__);
    }

    function hello()
    {
        $this->renderView(__FUNCTION__);
    }

      function display($cacheable = false, $urlparams = false) {
        $this->renderView(__FUNCTION__);
    }
}
?>

create /var/www/html/components/com_community/views/hello/view.html.php:

    <?php
    defined('_JEXEC') or die('Restricted access');
    jimport ( 'joomla.application.component.view' );

    class CommunityViewHello extends CommunityView {

    function helloWorld()  //This function shows a "Hello World" without an template view
       {
          echo 'Hello World';
       }

        function display()  //This function what happens when the hello view is called without a task
       {
              echo 'welcome to the main landing page for the hello view! There is nothing else shown here besides this message.';
       }


    function hello()

    {
        echo $tmpl->fetch('hello');
    }


    }

As you can see, if you want your view to have a default view even when no task is called, similar to what happens with /index.php?option=com_community&view=groups then you will want to name a task as function display in the controller and in the view.

finally, create /components/com_community/templates/jomsocial/layouts/hello.php:

<?php   defined('_JEXEC') or die();?>
<h2> This is an example </h2>
<div class="container">
<p> Hello, <?php echo $my->name; ?></p>
</div>

$my was defined way back in the controller! When your views and tasks group big enough, you will have different files for each task. The tasks files are with the fetch function in the view.html.php.

$tmpl = new CTemplate( );   //Call a template file
echo $tmpl->set ( 'vars1', $vars1)
echo $tmpl->set ( 'vars2', $vars2)
echo $tmpl->set ( 'vars3', $vars3)
           ->fetch ( 'hello' ); 

calls the /components/com_community/templates/jomsocial/layouts/hello.php file.

Using ->fetch ( 'hello.greeting' ); calls /components/com_community/templates/jomsocial/layouts/hello.greeting.php.

If you want to create a new directory for these then ->fetch ( 'hello/create' ); calls /components/com_community/templates/jomsocial/layouts/hello/create.php

If you want to create menu items and aliases for your new components, then you need to create a new file (as well as a second and modify a third if you want to do pass menu-item defined parameters to your tasks):

create file: /components/com_community/views/hello/metadata.xml:

<?xml version="1.0" encoding="utf-8"?>
<metadata>
        <view title="Groups">
                <message>
                        <![CDATA[
                                Hello view
                        ]]>
                </message>
                <options var="task">
                        <default name="Hello" msg="displays landing page" />
                        <option value="hello" name="- one greeting" msg="Display detail page of one greeting" />
                        <option value="helloWorld" name="- helloworldview" msg="Display whatever you have in the hello world task" />
                </options>
        </view>
        <state>
                <name>Hello Groups Layout</name>
                <description>Hello Groups listings</description>
        </state>
</metadata>

This file will add items to the "community" section of the menu in the administrator menu panel. The option values are the names of the tasks. The option without a value that is uses the default tag will pull up the display function described earlier.

If you need to add parameters to the file, then you need to do something a bit complicated:

create /components/com_community/views/hello/tmpl/default.xml:

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <layout title="Name" option="View">
        <message>
        </message>
    </layout>
    <fields name="params">
        <fieldset
            name="basic"
            label="Selected Group">
            <field
                name="item_id"
                query="SELECT `id`, `name` FROM #__community_groups_category WHERE ORDER BY `id`"
                type="sql"
                key_field="id"
                value_field="name"
                label="Associated Group"
                require="true"
                description="Select the jomsocial group whose hello task this will be associated with">
            </field>
        </fieldset>
    </fields>
</metadata>

This will create a tab wherein users can specify one group out of available groups in the database. It will assign the id of the group to the parameters field in the #__menu database table in the params colums JSON object as the value for the item_id key. In order for your view to use that value when rendering the page, include the following code in views/hello/view.html.php:

        $mainframe = JFactory::getApplication();
        $jinput = $mainframe->input;
        $document = JFactory::getDocument();


        // Get category id from the query string if there are any.
        $groupId = $jinput->getInt('group', 0);


                                 // Load the parameters.
                $params       = $mainframe->getParams();
                $params_array = $params->toArray();

                if (isset($params_array['item_id']))
                {
                            $groupId = $params_array['item_id'];
                }

In this way, your task can receive the necessary specifics from either the URL if given from within your component(option=com_community&view=hello&task=hello&groupid=5), or from being called by a main menu or jomsocial toolbar item drawing of the stored parameters in the menu database table for that menu item.

The options and tab that you create here will be visible for all menu items of this task. If you want different tabs for different menu options, you will have to create entirely different views. having everything in one view may lead to unused and potentially misleading tabs where values can be set by your users but that will not or should not be used by the actual task specified by the user.

Forgive me for not testing every line of this code in an integrated component. I have done all of these functions in my view but have abridged my code which was built with initial guidance from @Thavia-Farias's answer. While it is more clear than posting my extensive code, it has not been tested in it's current form for functionality. Be sure to check your php error logs to debug your project. I have to log in as root (sudo su) and check with nano /var/log/mysqld/error_log on my system. Good luck!