【问题标题】:Joomla insert article into componentJoomla 将文章插入组件
【发布时间】:2011-04-20 18:24:07
【问题描述】:

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

文章将从后端选择。

【问题讨论】:

    标签: joomla insert components joomla-article


    【解决方案1】:

    在后端您将选择文章,文章的 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 添加到<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')   
    

    【讨论】:

    • 谢谢! :) 它正在工作,但更进一步如何使弹出选择表单在后端选择文章?
    • 这是一个很好的例子,但是我希望在相同的设置页面上不使用参数和首选项。只是带有按钮的简单表单
    • 你可以做到。最好发布一个新问题,我会回答新问题。有问题就跑题了。
    猜你喜欢
    • 1970-01-01
    • 2015-03-27
    • 2013-02-07
    • 2012-08-15
    • 2011-04-21
    • 2011-08-28
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    相关资源
    最近更新 更多