Joomla 将文章插入组件

发布于 2024-09-25 16:05:19 字数 57 浏览 3 评论 0原文

我想在我的组件中插入一篇文章,有人有关于如何做到这一点的示例吗?

文章将从后端选择。

I would like to insert an article inside my component, does someone have an example on how to do that?

Article would be selected from backend.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-10-02 16:05:19

在后端,您将选择文章,文章的 ID 将存储在组件参数(config.xml)或自定义组件参数表中的数据库中。

在您的自定义组件中

  1. 将文章 ID 存储到变量
  2. 使用文章 ID 查询数据库 #__content
  3. 显示文章

例如

 //
 // Function for your model
 //
/**
 *
 * @return object 
 * 
 * Object will have following structure 
 * 
 * Field            Type    
 * ----------------------------------        
 * id               "int(11) unsigned"
 * title            varchar(255)
 * alias            varchar(255)
 * title_alias    varchar(255)
 * introtext        mediumtext
 * fulltext         mediumtext
 * state            tinyint(3)
 * sectionid        "int(11) unsigned"
 * mask             "int(11) unsigned"
 * catid            "int(11) unsigned"
 * created          datetime
 * created_by      "int(11) unsigned"
 * created_by_alias varchar(255)
 * modified         datetime
 * modified_by  "int(11) unsigned"
 * checked_out  "int(11) unsigned"
 * checked_out_time datetime
 * publish_up      datetime
 * publish_down  datetime
 * images           text
 * urls             text
 * attribs          text
 * version          "int(11) unsigned"
 * parentid         "int(11) unsigned"
 * ordering         int(11)
 * metakey          text
 * metadesc         text
 * access           "int(11) unsigned"
 * hits             "int(11) unsigned"
 * metadata         text
 */
 public function getMyArticle() {

        //  Get Component parameters (config.xml)
        $params = JComponentHelper::getParams('com_mycomponent');

        //  Get Specific parameter
        $myArticleId = (int) $params->get('articleId', 0);

        //  Make sure parameter is set and is greater than zero
        if ($myArticleId > 0) {

            //  Build Query
            $query = "SELECT * FROM #__content WHERE id = $myArticleId";

            //  Load query into an object
            $db = JFactory::getDBO();
            $db->setQuery($query);
            return $db->loadObject();
        }

        //
        return null;
    }

要在后端进行弹出选择,请编辑组件的 config.xml

addpath 添加到 元素

<!-- Add the path to content elements -->
<params addpath="/administrator/components/com_content/elements">
   <!-- Add Select Article param -->
   <param name="articleId" type="article" default="0" label="Select Article" description="" />

您还需要将 config 按钮添加到组件默认视图的工具栏上

// Add this code in the display() method of the view
// @todo change com_mycomponent to your component's name
JToolBarHelper::preferences('com_mycomponent')   

In the Back-End you will selected the article, the ID of article will be stored in a database in component params (config.xml) or custom component params table.

In you custom component

  1. Store Article Id to variable
  2. Query the database #__content table with Article ID
  3. Display the article

For example

 //
 // Function for your model
 //
/**
 *
 * @return object 
 * 
 * Object will have following structure 
 * 
 * Field            Type    
 * ----------------------------------        
 * id               "int(11) unsigned"
 * title            varchar(255)
 * alias            varchar(255)
 * title_alias    varchar(255)
 * introtext        mediumtext
 * fulltext         mediumtext
 * state            tinyint(3)
 * sectionid        "int(11) unsigned"
 * mask             "int(11) unsigned"
 * catid            "int(11) unsigned"
 * created          datetime
 * created_by      "int(11) unsigned"
 * created_by_alias varchar(255)
 * modified         datetime
 * modified_by  "int(11) unsigned"
 * checked_out  "int(11) unsigned"
 * checked_out_time datetime
 * publish_up      datetime
 * publish_down  datetime
 * images           text
 * urls             text
 * attribs          text
 * version          "int(11) unsigned"
 * parentid         "int(11) unsigned"
 * ordering         int(11)
 * metakey          text
 * metadesc         text
 * access           "int(11) unsigned"
 * hits             "int(11) unsigned"
 * metadata         text
 */
 public function getMyArticle() {

        //  Get Component parameters (config.xml)
        $params = JComponentHelper::getParams('com_mycomponent');

        //  Get Specific parameter
        $myArticleId = (int) $params->get('articleId', 0);

        //  Make sure parameter is set and is greater than zero
        if ($myArticleId > 0) {

            //  Build Query
            $query = "SELECT * FROM #__content WHERE id = $myArticleId";

            //  Load query into an object
            $db = JFactory::getDBO();
            $db->setQuery($query);
            return $db->loadObject();
        }

        //
        return null;
    }

To make pop-up select in the back end, edit your component's config.xml

add addpath to the <params> elements

<!-- Add the path to content elements -->
<params addpath="/administrator/components/com_content/elements">
   <!-- Add Select Article param -->
   <param name="articleId" type="article" default="0" label="Select Article" description="" />

You will also need to add config button to the toolbar on the component's default view

// Add this code in the display() method of the view
// @todo change com_mycomponent to your component's name
JToolBarHelper::preferences('com_mycomponent')   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文