【问题标题】:How to Get Global Article Parameters in Joomla?如何在 Joomla 中获取全局文章参数?
【发布时间】:2012-04-04 14:17:08
【问题描述】:

我正在 Joomla 中编写一个模块! 2.5.该模块在给定位置显示一篇文章。 我需要文章属性(即show_titlelink_title ecc。) 使用此代码,我可以获得文章的特定属性:

$db =& JFactory::getDBO();
$query = 'SELECT * FROM #__content WHERE id='.$id.' AND state=1';
$db->setQuery($query);
$item = $db->loadObject();
$attribs = json_decode($item->attribs, true);

如果我得到var_dump $attribs 变量:

array(26) {
  ["show_title"]=>
  string(0) ""
  ["link_titles"]=>
  string(0) ""

  [...]

}

变量$attribs代表文章的具体属性。当一个元素设置为"" 时意味着“使用全局配置”。

我可以通过这个查询获得全局配置:

SELECT params from #__extensions where extension_id=22;

其中 22 是 com_component 扩展的 id。然后我可以将这里的结果与具体文章的结果合并。

但是有没有简单的方法来实现这一点? Joomla!框架中有专门的类吗?

【问题讨论】:

    标签: joomla joomla-extensions joomla2.5


    【解决方案1】:

    我将从加载文章模型开始:

    // Get an instance of the generic articles model
    $model = JModel::getInstance('Article',
                                 'ContentModel',
                                 array('ignore_request' => true));
    

    获取具体文章...

    $model->getItem($id)
    

    要获取组件全局参数,我相信您可以使用:

    $params = &JComponentHelper::getParams( 'COMPONENT_NAME' );
    

    在你的情况下,你需要这样的东西:

    jimport('joomla.application.component.helper'); // load component helper first
    $params = JComponentHelper::getParams('com_content');
    

    我建议您查看 Joomla! 附带的文章模块的代码! 2.5.x 因为他们做了很多与您尝试创建的类似的事情。你也可以阅读this article,它有点过时了,但我认为它仍然大部分是正确的(除了 jparams 被 jforms 取代)。

    【讨论】:

    • $model->getItem($id); 之前你必须设置JModel 的参数。这花了我一段时间。这是代码:$appParams = JFactory::getApplication()->getParams(); $model->setState('params', $appParams);。没有那个你会发生错误。
    猜你喜欢
    • 2014-03-27
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多