0
votes

How to add a link category_id added to the admin? (Joomla 2.5)

You could write in the function JToolBarHelper::addNew('select.add'); or other functions... For example,

index.php?option=com_pictures&view=select&layout=edit&category_id=14

Help, please. Thanks in advance

Reply David F:

Hi, David F. I almost got it.
After clicking "Add" appears category_id=14, and the rest did not work after clicking "Edit", "Save", "Save Close" ... - category_id=0
I've been programming. Here's an example:

...
protected $catid;

public function __construct($config = array()) {
    parent::__construct($config);

    if (empty($this->catid)) {
        $this->catid = JRequest::getInt('category_id', 0);
    }
}

protected function allowAdd($data = array()) {
    $user = JFactory::getUser();
    $categoryId = JArrayHelper::getValue($data, 'catid', JRequest::getInt('filter_category_id'), 'int');
    $allow = null;

    if ($categoryId) {
        $allow = $user->authorise('core.create', $this->option . '.category.' . $categoryId);
    }

    if ($allow === null) {
        return parent::allowAdd($data);
    } else {
        return $allow;
    }
}

protected function allowEdit($data = array(), $key = 'id') {
    $recordId = (int) isset($data[$key]) ? $data[$key] : 0;
    $categoryId = 0;

    if ($recordId) {
        $categoryId = (int) $this->getModel()->getItem($recordId)->catid;
    }

    if ($categoryId) {
        return JFactory::getUser()->authorise('core.edit', $this->option . '.category.' . $categoryId);
    } else {
        return parent::allowEdit($data, $key);
    }
}

protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id') {
    $append = parent::getRedirectToItemAppend($recordId);
    $append .= '&category_id=' . $this->category_id;

    return $append;
}

protected function getRedirectToListAppend() {
    $append = parent::getRedirectToListAppend();
    $append .= '&category_id=' . $this->category_id;

    return $append;
}
1

1 Answers

0
votes

I believe that the functions that you are looking for are part of JController and should be added to your controller. In your case, this would likely be the select.php controller based on the view name in your url.

You can see a good example of this in the categories component, specifically at administrator/components/com_categories/controllers/category.php.

The following is the code in com_categories. You would want to rename extension to 'category_id' and may want to grab the value from JInput instead of the current class:

/**
 * Gets the URL arguments to append to an item redirect.
 *
 * @param   integer  $recordId  The primary key id for the item.
 * @param   string   $urlVar    The name of the URL variable for the id.
 *
 * @return  string  The arguments to append to the redirect URL.
 *
 * @since   1.6
 */
protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
{
    $append = parent::getRedirectToItemAppend($recordId);
    $append .= '&extension=' . $this->extension;

    return $append;
}

/**
 * Gets the URL arguments to append to a list redirect.
 *
 * @return  string  The arguments to append to the redirect URL.
 *
 * @since   1.6
 */
protected function getRedirectToListAppend()
{
    $append = parent::getRedirectToListAppend();
    $append .= '&extension=' . $this->extension;

    return $append;
}

*EDIT:

You also have to add it as part of the form. This is the easy, but important part. Just make sure the form has a hidden input with the value or add it to the url in the action section of the form:

<form action="<?php echo JRoute::_('index.php?option=com_component&layout=edit&id='.(int) $this->item->id . '&category_id='.$this->category_id); ?>" method="post" name="adminForm">

or use this:

<input type="hidden" name="category_id" value="<?php echo $this->category_id; ?>" />

To further explain what happens, when you click an item to go to the form, you likely add on the variable that you need. Then you add it to the form so that when one of the items is clicked, it will get submitted with the form. Joomla processes this form and either saves it or not depending on the toolbar button clicked. Then Joomla redirects, either back to the form or to the list view. Without the functions in your controller, your variable gets lost.