自定义文章字段未显示在joomla 3.2中

时间:2013-12-12 06:22:16

标签: php joomla joomla3.2

我一直在尝试在文章组件后端创建一个表单,这样我就可以为每篇文章添加一些内容属性。我从joomla网站文档中获得了此示例代码。 http://docs.joomla.org/Adding_custom_fields_to_the_article_component

我理解onContentPrepareForm方法是一个重要的函数,它在joomla文章后端添加了这个表单。但是,它似乎不适合我。我以某种方式检查了全局其他选项,并且我看到该表单作为单独的选项卡出现在全局文章选项中。但是,我想为每篇文章添加此表单。

我使用的joomla版本是3.2 ... 顺便说一下,我在后端启用了插件......

<?php
/** 
* @package      Joomla.Site
* @subpackage   plg_content_rating
* @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights   reserved.
* @license      GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('JPATH_BASE') or die;

jimport('joomla.utilities.date');

/**
 * An example custom profile plugin.
 *
 * @package     Joomla.Plugin
 * @subpackage  User.profile
 * @version     1.6
 */
class plgContentRating extends JPlugin
{
/**
 * Constructor
 *
 * @access      protected
 * @param       object  $subject The object to observe
 * @param       array   $config  An array that holds the plugin configuration
 * @since       2.5
 */
public function __construct(& $subject, $config)
{
    parent::__construct($subject, $config);
    $this->loadLanguage();
}

/**
 * @param   string  $context    The context for the data
 * @param   int     $data       The user id
 * @param   object
 *
 * @return  boolean
 * @since   2.5
 */
function onContentPrepareData($context, $data)
{
    if (is_object($data))
    {
        $articleId = isset($data->id) ? $data->id : 0;
        if (!isset($data->rating) and $articleId > 0)
        {
            // Load the profile data from the database.
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $query->select('profile_key, profile_value');
            $query->from('#__user_profiles');
            $query->where('user_id = ' . $db->Quote($articleId));
            $query->where('profile_key LIKE ' . $db->Quote('rating.%'));
            $query->order('ordering');
            $db->setQuery($query);
            $results = $db->loadRowList();

            // Check for a database error.
            if ($db->getErrorNum())
            {
                $this->_subject->setError($db->getErrorMsg());
                return false;
            }

            // Merge the profile data.
            $data->rating = array();

            foreach ($results as $v)
            {
                $k = str_replace('rating.', '', $v[0]);
                $data->rating[$k] = json_decode($v[1], true);
                if ($data->rating[$k] === null)
                {
                    $data->rating[$k] = $v[1];
                }
            }
        }
    }

    return true;
}

/**
 * @param   JForm   $form   The form to be altered.
 * @param   array   $data   The associated data for the form.
 *
 * @return  boolean
 * @since   2.5
 */
function onContentPrepareForm($form, $data)
{
    if (!($form instanceof JForm))
    {
        $this->_subject->setError('JERROR_NOT_A_FORM');
        return false;
    }

   /* if (!in_array($form->getName(), array('com_content.article'))) {
        return true;
    }*/

    // Add the extra fields to the form.
    // need a seperate directory for the installer not to consider the XML a package when "discovering"

    JForm::addFormPath(dirname(__FILE__) . '/rating');
    $form->loadFile('rating',false);

    return true;
}

/**
 * Example after save content method
 * Article is passed by reference, but after the save, so no changes will be saved.
 * Method is called right after the content is saved
 *
 * @param   string      The context of the content passed to the plugin (added in 1.6)
 * @param   object      A JTableContent object
 * @param   bool        If the content is just about to be created
 * @since   2.5
 */
public function onContentAfterSave($context, &$article, $isNew)
{
    $articleId  = $article->id;
    if ($articleId && isset($article->rating) && (count($article->rating)))
    {
        try
        {
            $db = JFactory::getDbo();

            $query = $db->getQuery(true);
            $query->delete('#__user_profiles');
            $query->where('user_id = ' . $db->Quote($articleId));
            $query->where('profile_key LIKE ' . $db->Quote('rating.%'));
            $db->setQuery($query);
            if (!$db->query()) {
                throw new Exception($db->getErrorMsg());
            }

            $query->clear();
            $query->insert('#__user_profiles');
            $order  = 1;
            foreach ($article->rating as $k => $v)
            {
                $query->values($articleId.', '.$db->quote('rating.'.$k).', '.$db->quote(json_encode($v)).', '.$order++);
            }
            $db->setQuery($query);

            if (!$db->query()) {
                throw new Exception($db->getErrorMsg());
            }
        }
        catch (JException $e)
        {
            $this->_subject->setError($e->getMessage());
            return false;
        }
    }

    return true;
}

/**
 * Finder after delete content method
 * Article is passed by reference, but after the save, so no changes will be saved.
 * Method is called right after the content is saved
 *
 * @param   string      The context of the content passed to the plugin (added in 1.6)
 * @param   object      A JTableContent object
 * @since   2.5
 */
public function onContentAfterDelete($context, $article)
{

    $articleId  = $article->id;
    if ($articleId)
    {
        try
        {
            $db = JFactory::getDbo();

            $query = $db->getQuery(true);
            $query->delete();
            $query->from('#__user_profiles');
            $query->where('user_id = ' . $db->Quote($articleId));
            $query->where('profile_key LIKE ' . $db->Quote('rating.%'));
            $db->setQuery($query);

            if (!$db->query())
            {
                throw new Exception($db->getErrorMsg());
            }
        }
        catch (JException $e)
        {
            $this->_subject->setError($e->getMessage());
            return false;
        }
    }

    return true;
}

public function onContentPrepare($context, &$article, &$params, $page = 0)
{
    if (!isset($article->rating) || !count($article->rating))
        return;

    // add extra css for table
    $doc = JFactory::getDocument();
    $doc->addStyleSheet(JURI::base(true).'/plugins/content/rating/rating/rating.css');

    // construct a result table on the fly  
    jimport('joomla.html.grid');
    $table = new JGrid();

    // Create columns
    $table->addColumn('attr')
        ->addColumn('value');   

    // populate
    $rownr = 0;
    foreach ($article->rating as $attr => $value) {
        $table->addRow(array('class' => 'row'.($rownr % 2)));
        $table->setRowCell('attr', $attr);
        $table->setRowCell('value', $value);
        $rownr++;
    }

    // wrap table in a classed <div>
    $suffix = $this->params->get('ratingclass_sfx', 'rating');
    $html = '<div class="'.$suffix.'">'.(string)$table.'</div>';

    $article->text = $html.$article->text;
}

}

编辑帖子: 我将此代码添加到/administrator/components/com_content/views/article/tmpl/edit.php

<?php
    $i = 0;
    $fieldSets = $this->form->getFieldsets();
    foreach ($fieldSets as $name => $fieldSet) :
        if($i <= 3){
            $i++;
            continue;
        }
        echo JHtml::_('bootstrap.addTab', 'myTab', $fieldSet->name, JText::_($fieldSet->label, true));
        ?>
        <div class="tab-pane" id="<?php echo $name;?>">
            <?php
            if (isset($fieldSet->description) && !empty($fieldSet->description)) :
                echo '<p class="tab-description">'.JText::_($fieldSet->description).'</p>';
            endif;
            foreach ($this->form->getFieldset($name) as $field):
                ?>
                <div class="control-group">
                    <?php if (!$field->hidden && $name != "permissions") : ?>
                        <div class="control-label">
                            <?php echo $field->label; ?>
                        </div>
                    <?php endif; ?>
                    <div class="<?php if ($name != "permissions") : ?>controls<?php endif; ?>">
                        <?php echo $field->input; ?>
                    </div>
                </div>
            <?php
            endforeach;
            ?>
        </div>
    <?php echo JHtml::_('bootstrap.endTab'); ?>
    <?php endforeach; ?>

2 个答案:

答案 0 :(得分:2)

正如您所提到的,核心tmpl文件不支持您的更改。

黑客核心文件(正如你所做的那样)是一个非常糟糕的主意,原因有几个,包括在下一个3.2.x安全补丁发布时可能会被覆盖(如即将发布的3.2.1)并且肯定会被覆盖将在3.5发布时发布。鉴于Joomla快速发布安全补丁以及他们定期的一键式主要和次要更新周期,您最不想担心的是您的更改被吹走了。

幸运的是Joomla允许您创建template overrides,admin的流程与前端模板的覆盖几乎相同。

幸运的是,修改是在正确的文件中,假设您使用的是默认管理模板Isis,只需复制

/administrator/components/com_content/views/article/tmpl/edit.php

为:

/administrator/templates/isis/html/com_content/article/edit.php

您可能希望还原原始文件,但如上所述,几乎可以肯定是在安全更新或版本更新中进行更新。

答案 1 :(得分:0)

在另一篇文章[{3}}中找到了解决问题(和我的问题)的解决方案,希望它有所帮助。看来示例插件包含错误