代码之家  ›  专栏  ›  技术社区  ›  miojamo

Joomla在组件中插入文章

  •  2
  • miojamo  · 技术社区  · 14 年前

    我想在我的组件中插入一篇文章,是否有人有一个如何做到这一点的例子?

    文章将从后端选择。

    1 回复  |  直到 4 年前
        1
  •  6
  •   Alex    14 年前

    在后端您将选择文章,文章的ID将存储在数据库中的component params中( config.xml

    在自定义组件中

    1. 查询数据库 #__content 具有项目ID的表
    2. 显示文章

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

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

    addpath <params> 元素

    <!-- 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')