我在Joomla 3.6中构建了一个自定义组件。组件本身工作正常,但是我无法从组件内部的自定义字段和单独的模块访问用户状态变量。当我尝试时,我没有得到任何回报。
以下是模型中populatestate()的代码:
$app = JFactory::getApplication();
$filter_product_group_category = $app->getUserStateFromRequest('filter.product_group_category', 'filter[product_group_category]', '', 'string');
$this->setState('filter.product_group_category', $filter_product_group_category);
$filter_product_group_type = $app->getUserStateFromRequest('filter.product_group_type', 'filter[product_group_type]', '', 'string');
$this->setState('filter.product_group_type', $filter_product_group_type);
$filter_search = $app->getUserStateFromRequest('filter.search', 'filter[search]', '', 'string');
$this->setState('filter.search', $filter_search);
以下是我在自定义字段和模块中使用的代码:
$mainframe =JFactory::getApplication();
$filter_product_group_category = $mainframe->getUserState("filter.product_group_category");
$filter_product_group_type = $mainframe->getUserState("filter.product_group_type");
$filter_search = $mainframe->getUserState("filter.search");
echo $filter_product_group_category;
echo $filter_product_group_type;
echo $filter_search;
我显然做错了什么,但我已经耗尽了我的知识,花了几个小时的Google研究而没有接近。任何帮助表示赞赏!
答案 0 :(得分:0)
在模块文件中我有这样的东西:
JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_aoeodb/models', 'AoeoDBModel');
$view = ModAoeoDBHelper::getFilterView();
$model = JModelLegacy::getInstance(ucfirst($view), 'AoeoDBModel', array('ignore_request' => true));
然后在帮助者中:
$form = $model->getForm();
$filters = $form->getFieldset();
foreach ($filters as $field)
{
if($field->getAttribute('name') <> 'search')
{
$form->setValue($field->getAttribute('name'), 'filter', $model->getState('com_aoeodb.'. $view . '.filter.' . $field->getAttribute('name')));
}
}
return $filters;
它根据我在模块中使用的表单获取模态值并设置字段值。
答案 1 :(得分:0)
组件状态存储在post而不是session中,所以如果你想获取值,那么应该从模型中获取它们。如Mind gem示例。
如果要在会话中存储值,那么在模型中应该在用户状态中添加值:
$app = JFactory::getApplication();
$filter_product_group_category = $app->getUserStateFromRequest('filter.product_group_category', 'filter[product_group_category]', '', 'string');
$app->setUserState('filter.product_group_category', $filter_product_group_category);
希望得到这个帮助。