简体   繁体   English

如何添加添加到管理员(JToolBarHelper :: addNew)的链接category_id? -Joomla 2.5

[英]How to add a link category_id added to the admin (JToolBarHelper::addNew)? - Joomla 2.5

How to add a link category_id added to the admin? 如何添加添加到管理员的链接category_id (Joomla 2.5) (Joomla 2.5)

You could write in the function JToolBarHelper::addNew('select.add'); 您可以在函数JToolBarHelper::addNew('select.add'); or other functions... For example, 或其他功能...例如,

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

Help, please. 请帮忙。 Thanks in advance 提前致谢

Reply David F: 回复David F:

Hi, David F. I almost got it. 嗨,DavidF。我几乎明白了。
After clicking "Add" appears category_id=14 , and the rest did not work after clicking "Edit", "Save", "Save Close" ... - category_id=0 单击“添加”后,将显示category_id=14 ,其余部分在单击“编辑”,“保存”,“保存关闭”后将无法使用...- 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;
}

I believe that the functions that you are looking for are part of JController and should be added to your controller. 我相信您正在寻找的功能是JController的一部分,应该添加到您的控制器中。 In your case, this would likely be the select.php controller based on the view name in your url. 在您的情况下,这可能是基于URL中视图名称的select.php控制器。

You can see a good example of this in the categories component, specifically at administrator/components/com_categories/controllers/category.php. 您可以在类别组件中看到一个很好的示例,特别是在administrator / components / com_categories / controllers / category.php中。

The following is the code in com_categories. 以下是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: 您可能希望将扩展名重命名为“ category_id”,并且可能想要从JInput而不是当前类中获取值:

/**
 * 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: 只要确保表单具有带有值的隐藏输入,或将其添加到表单的操作部分中的url中即可:

<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. Joomla会处理此表单,并根据单击的工具栏按钮保存或不保存。 Then Joomla redirects, either back to the form or to the list view. 然后,Joomla重定向回到表单或列表视图。 Without the functions in your controller, your variable gets lost. 没有控制器中的功能,变量将丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM