1

I'm fairly new to Joomla and I'm developing my first component. I finished up the backend and I'm working on the frontend. I have most of the functionality figured out but I can't figure out how to get the Page heading to display from the Menu item params.

I looked all around but I can't get mine to display. I've looked at the Article component for guidance but it doesn't work.

I copied this directly from the Article component, but after debugging I noticed that params is null:

<?php if ($this->params->get('show_page_heading',1)) : ?>
    <h1><?php echo  $this->params->get('page_heading') ? $this->params->get('page_heading') : JFactory::getApplication()->getMenu()->getActive()->title; ?></h1>
<?php endif; ?>

So then I figured I must add some code to the view.html.php file. So I made some modifications to my display method to match what the Article component has. This time I get the following error message that points to when I'm trying to assign params to $this:

Fatal error: Call to a member function get() on null

I'm thinking maybe it's because I'm working on a list view and the Article code is for a single item view.

Any help would be appreciated.

Here's the code I have in my view.html.php file:

class LibraryViewItems extends JViewLegacy {

    protected $items;
    protected $params;
    protected $state;

    public function display($tpl = null) {          
        $this->items = $this->get('Items');
        $state      = $this->get('State');
        $this->params = $this->state->get('params');

        if (count($errors = $this->get('Errors'))) {
            JError::raiseError(500, implode("\n", $errors));
            return false;
        }

        parent::display($tpl);
    }


}

3 Answers 3

1

It looks like you are trying to get the menu parameters for your current active menu. Try the following code, this hasn't been tested, I am giving you this reference from memory.

$app = JFactory::getApplication();
$params = $app->getMenu()->getActive()->getParams();

That may work for the current active menu parameters. If not you may have to pass the current Itemid into get params to get that array. In that instance try the following:

$app = JFactory::getApplication();
$Itemid = $app->getMenu()->getActive()->id;
$params = $menu->getParams($Itemid);

Maybe one of those 2 solutions will work for you. If you put this in your view.html.php file you can assign the variables to $this so that is passes into your template file for the component. ($this->Itemid, etc etc)

3
  • yeah, I had found a similar solution on another thread and it did work on getting me the menu title but it doesn't behave like the code in the Articles component. I was really hoping to get mine to have code similar to it with all of the params available where I could use this code: <?php if ($this->params->get('show_page_heading')) : ?> <div class="page-header"> <h1> <?php echo $this->escape($this->params->get('page_heading')); ?> </h1> </div> <?php endif; Thanx for your help
    – chavab_1
    Commented Aug 9, 2017 at 16:40
  • Those parameters are stored as part of the global params for com_content I believe. You can take a look to see how the devs did it there.
    – Terry Carter
    Commented Aug 9, 2017 at 17:01
  • Oh, I get it. So I can't get those specific parameters. I thought those parameters were the menu globals. That makes sense. So I guess it would've never worked without adding my own global parameters. One last question, do you have any idea why I keep getting an error pointing to this: $state = $this->get('State'); $this->params = $this->state->get('params'); I have the populateState() method in my model but I still get this error: Fatal error: Call to a member function get() on null
    – chavab_1
    Commented Aug 9, 2017 at 17:56
1

You are getting $params from $state

 $state = $this->get('State');

This means the state must have $params populated in your model. Check if you are populating the $state with $params in your model's populateState function

protected function populateState($ordering = null, $direction = null) {
    $params = JComponentHelper::getParams('com_mycomponent');
    $this->setState('params', $params);

    parent::populateState($ordering, $direction);
}
4
  • Yeah, I had tried that before but my populateState method was a bit different. I tried your code and I still get the same results. Fatal error: Call to a member function get() on null And it points to $this->params = $this->state->get('params') Any other suggestions? Thanx
    – chavab_1
    Commented Aug 9, 2017 at 16:32
  • See my edited code. Use JComponentHelper::getParams('com_mycomponent'); to get params instead
    – Nagarjun
    Commented Aug 10, 2017 at 13:34
  • I tried that with your revised code but I'm still getting the same error message. After reading @Terry Carter 's response You think it might be because I don't have any global parameters set up for my component? My problem was that I was trying to make my component work like com_component. It has global parameters that it's referencing in its template. I was trying to do the same thing but I realized I can't access its parameters. Thanx for your help.
    – chavab_1
    Commented Aug 10, 2017 at 21:21
  • JComponentHelper::getParams('com_mycomponent'); should never return null. So either you are calling different model or the populateState method is not getting called. Try to call this method in your __construct constructor of your model.
    – Nagarjun
    Commented Aug 11, 2017 at 20:29
1

Addendum:

OP wants the menu item params accessible in the view, but he tries to override the component params var ($this->params) with the menu item params object/class. Which is unnecessary complicated. In the core article component, the component and the menu item params are merged (in the view.html.php), which is cool if you are trying to override some stuff. The correct answer to his question is to use $params = JFactory::getApplication()->getMenu()->getActive()->getParams(); in the default.php of the view. Which allows: $params->get('page_heading'); (IF that view gets called via a menu item.)

Get menu item params:

    $app = JFactory::getApplication();
    $menu = $app->getMenu()->getActive();
    $params = $menu->getParams();

Get component params:

$this->params = $this->state->get('params');

Merge them in the view.html.php -> display function (taken from the view.html.php of the article view):

    $this->params = $this->state->get('params');
    $active       = $app->getMenu()->getActive();
    $temp         = clone $this->params;

if ($active)
        {
            $currentLink = $active->link;

            // If the current view is the active item and an article view for this article, then the menu item params take priority
            if (strpos($currentLink, 'view=article') && strpos($currentLink, '&id=' . (string) $item->id))
            {
                // Load layout from active query (in case it is an alternative menu item)
                if (isset($active->query['layout']))
                {
                    $this->setLayout($active->query['layout']);
                }
                // Check for alternative layout of article
                elseif ($layout = $item->params->get('article_layout'))
                {
                    $this->setLayout($layout);
                }

                // $item->params are the article params, $temp are the menu item params
                // Merge so that the menu item params take priority
                $item->params->merge($temp);
            }
            else
            {
                // Current view is not a single article, so the article params take priority here
                // Merge the menu item params with the article params so that the article params take priority
                $temp->merge($item->params);
                $item->params = $temp;

                // Check for alternative layouts (since we are not in a single-article menu item)
                // Single-article menu item layout takes priority over alt layout for an article
                if ($layout = $item->params->get('article_layout'))
                {
                    $this->setLayout($layout);
                }
            }
        }
        else
        {
            // Merge so that article params take priority
            $temp->merge($item->params);
            $item->params = $temp;

            // Check for alternative layouts (since we are not in a single-article menu item)
            // Single-article menu item layout takes priority over alt layout for an article
            if ($layout = $item->params->get('article_layout'))
            {
                $this->setLayout($layout);
            }
        }
5
  • I've tried that but I keep getting Fatal error: Call to a member function get() on null error. I have the following populateState() method in my model but I still get this error. protected function populateState($ordering = null, $direction = null) { $app = JFactory::getApplication(); $params = $app->getParams(); $this->setState('params', $params); parent::populateState($ordering, $direction); }
    – chavab_1
    Commented Aug 10, 2017 at 21:27
  • You are mixing menu item params and application (component) params again, take a closer look to the first two points in my answer and how the params are called differently. Commented Aug 10, 2017 at 21:32
  • Instead of overcomplicating things, just load the active menu item params in your view (default.php), then you can do things like $params->get('page_heading') ! You don't need to merge component params with the menu item params if there is no overriding logic behind you component in that view. They did it in the core component, but you don't need that here as far as I understand you main approach. The point "Get menu item params" is the correct answer for you. This loads your menu item params, with that you can do the get('page_heading). Commented Aug 10, 2017 at 21:43
  • Yeah, I finally figured that out thanx to @Terry Carter. Like I said, I'm a Joomla beginner so I was trying to go by the book and get my component to behave like the core ones. I just wanted to make sure it wasn't something I was doing wrong. Seems like I was on the right track. Thanx for your help and explanation.
    – chavab_1
    Commented Aug 10, 2017 at 21:50
  • Yeah I got that, just a reminder: You can load nearly everything everywhere in Joomla, mostly you should keep it simple like I said in the beginning of my now updated answer. Most simple questions like "how can I get the params" or "how can I override a function" have been already answered in the Joomla docs or here. Please consider that for your next question. ;) Commented Aug 10, 2017 at 22:02

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.