简体   繁体   English

Joomla使用“ JModelLegacy getInstance article”按ID呈现全文

[英]Joomla render full article by id with “JModelLegacy getInstance article”

I'm stuck trying to get joomla full article to render in a tab. 我一直试图让joomla全文在选项卡中呈现。 The tab is working. 该选项卡正在工作。 I just can´t render the article content. 我只是无法呈现文章内容。 This is where I am now. 这就是我现在的位置。

This is helper.php 这是helper.php

public static function getArticle($articleId)

{

JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model = JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request' => true));
$article = $model->getItem((int) $articleId);

$fullarticle = $item->fulltext;

$itemsHtml = '<div>'. $fullarticle .'</div>';

return $itemsHtml;
}

And this is in default.php 这是default.php

  ...code...
  else if ($list_of_tabs['use'][$i][0] == 'article'){            
    echo '<div class="tab-pane '.$active.'" id="'.$i.$rid.'">'.
            modJpTabsHelper::getArticle($list_of_tabs['article'][$i], $params) .
         '</div>';
  }
  ...code...

If you need more info. 如果您需要更多信息。 Don't hesitate to ask. 不要犹豫了。

What are you trying to achieve: to write your own Joomla! 您要实现的目标是:编写自己的Joomla! extension which displays articles in a tab or you just need to display your J! 扩展程序,它在选项卡中显示文章,或者您只需要显示J! articles in a tab? 标签中的文章?

If it's a latter, then there are already some nice and free (as in a "free bear") add-ons written just for that. 如果是后者,那么已经有一些专门为此编写的不错且免费的插件(如“免费熊”)。

You are trying to use the model part of an MVC as a thing to render. 您正在尝试使用MVC的模型部分作为要渲染的东西。 You should use the MVC system - using a controller to gathering the model and the view, and then you can render the model with the attached view, via the controller. 您应该使用MVC系统-使用控制器收集模型和视图,然后可以通过控制器使用附带的视图渲染模型。

So you use something like (I've not tested this - you will need to correct it). 因此,您使用类似(我还没有测试过的东西-您需要对其进行更正)之类的东西。

$filter=array('id' => $i->query['id']);
$options=array('filter_fields' => $filter,'ignore_request' => true);

$ctl = new ContentModelController();
$view = $ctl->getView( 'Article');
$model = $ctl->getModel( 'Article','',$options);

you may need to set params from application, eg.. 您可能需要从应用程序中设置参数,例如。

$model->setState('params', JApplication::getInstance('site')->getParams());

then continue 然后继续

$view->setModel( $model, true );
$result = $view->display();

Make sure that you have JLoader::import'ed any classes/classpaths - j. 确保您已将JLoader :: import的所有类/类路径导入-j。 tends to fail silently if they aren't found, which can be difficult to trace. 如果找不到它们,它们往往会静默地失败,这可能很难跟踪。

Sorry, it's only a partial solution - but hopefully it may put you on the right track. 抱歉,这只是部分解决方案-但希望它可以使您走上正确的道路。

Here was the problem: 问题出在这里:

$fullarticle = $item->fulltext;

Article object from model was in variable $article not $item: 来自模型的文章对象位于变量$ article而非$ item中:

$article = $model->getItem((int) $articleId);

So getting property fulltext from article object should be: 因此,从文章对象获取属性全文应该是:

$fullarticle = $article->fulltext;

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

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