【问题标题】:How to add a link category_id added to the admin (JToolBarHelper::addNew)? - Joomla 2.5如何添加添加到管理员的链接 category_id (JToolBarHelper::addNew)? - Joomla 2.5
【发布时间】:2013-02-04 17:23:46
【问题描述】:

如何添加链接category_id添加到管理员? (Joomla 2.5)

您可以在函数JToolBarHelper::addNew('select.add'); 或其他函数中编写... 例如,

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

请帮忙。提前致谢

回复大卫 F:

嗨,David F。我几乎明白了。
点击“添加”后出现category_id=14,其余点击“编辑”、“保存”、“保存关闭”后均无效... - category_id=0
我一直在编程。这是一个例子:

...
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;
}

【问题讨论】:

    标签: php joomla joomla2.5


    【解决方案1】:

    我相信您正在寻找的功能是 JController 的一部分,应该添加到您的控制器中。在您的情况下,这可能是基于您 url 中的视图名称的 select.php 控制器。

    您可以在类别组件中看到一个很好的例子,特别是在管理员/组件/com_categories/controllers/category.php。

    以下是 com_categories 中的代码。您可能希望将扩展名重命名为“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;
    }
    

    *编辑:

    您还必须将其添加为表单的一部分。这是简单但重要的部分。只需确保表单具有隐藏的输入值或将其添加到表单操作部分的 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">
    

    或者使用这个:

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

    为了进一步解释会发生什么,当您单击一个项目转到表单时,您可能会添加您需要的变量。然后将其添加到表单中,以便在单击其中一个项目时,它将与表单一起提交。 Joomla 处理此表单并根据单击的工具栏按钮保存或不保存它。然后 Joomla 重定向,要么返回表单,要么返回列表视图。如果没有控制器中的函数,您的变量就会丢失。

    【讨论】:

    • 嗨,David F。请阅读上述问题。
    • 我想我每次将它添加到我自己的组件时都会犯这个错误。您还需要将其添加到保存按钮所在的表单中。我正在更新我的答案以添加这篇文章。
    猜你喜欢
    • 2011-05-05
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2011-11-27
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    相关资源
    最近更新 更多