Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php joomla 3.2中未显示自定义文章字段_Php_Joomla_Joomla3.2 - Fatal编程技术网

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

Php joomla 3.2中未显示自定义文章字段,php,joomla,joomla3.2,Php,Joomla,Joomla3.2,我一直在尝试在文章组件后端创建一个表单,这样我就可以在每篇文章中添加一些东西属性。我从joomla网站文档中获得了这个示例代码 我知道ContentPrepareReform方法是在joomla文章后端添加此表单的一个重要功能。然而,它并不适合我。我不知何故检查了全局其他选项,我看到表单作为一个单独的选项卡出现在全局文章选项中。但是,我想为每篇文章添加此表单 我使用的joomla版本是3.2。。。 顺便说一句,我已经启用了后端插件 <?php /** * @package Jo

我一直在尝试在文章组件后端创建一个表单,这样我就可以在每篇文章中添加一些东西属性。我从joomla网站文档中获得了这个示例代码

我知道ContentPrepareReform方法是在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;
}
loadFile('rating',false);
返回true;
}
/**
*保存内容方法后的示例
*项目是通过引用传递的,但在保存之后,因此不会保存任何更改。
*方法在保存内容后立即调用
*
*@param string传递给插件的内容的上下文(在1.6中添加)
*@param对象一个JTableContent对象
*@param bool,如果内容即将创建
*@自2.5
*/
onContentAfterSave的公共函数($context,&$article,$isNew)
{
$articleId=$article->id;
如果($articleId&&isset($article->rating)&($count($article->rating)))
{
尝试
{
$db=JFactory::getDbo();
$query=$db->getQuery(true);
$query->delete(“#u_u用户_配置文件”);
$query->where('user_id='。$db->Quote($articleId));
$query->where('profile_key LIKE'.$db->Quote('rating.%');
$db->setQuery($query);
如果(!$db->query()){
抛出新异常($db->getErrorMsg());
}
$query->clear();
$query->insert(“#u_u用户_配置文件”);
$order=1;
foreach($article->评级为$k=>$v)
{
$query->values($articleId.','.$db->quote('rating.'.$k.'),'.$db->quote(json_encode($v)),'.$order++);
}
$db->setQuery($query);
如果(!$db->query()){
抛出新异常($db->getErrorMsg());
}
}
捕获(JEException$e)
{
$this->_subject->setError($e->getMessage());
返回false;
}
}
返回true;
}
/**
*删除内容后的查找器方法
*项目是通过引用传递的,但在保存之后,因此不会保存任何更改。
*方法在保存内容后立即调用
*
*@param string传递给插件的内容的上下文(在1.6中添加)
*@param对象一个JTableContent对象
*@自2.5
*/
公共函数onContentAfterDelete($context,$article)
{
$articleId=$article->id;
如果($articleId)
{
尝试
{
$db=JFactory::getDbo();
$query=$db->getQuery(true);
$query->delete();
$query->from(“#u_u用户_配置文件”);
$query->where('user_id='。$db->Quote($articleId));
$query->where('profile_key LIKE'.$db->Quote('rating.%');
$db->setQuery($query);
如果(!$db->query())
{
抛出新异常($db->getErrorMsg());
}
}
捕获(JEException$e)
{
$this->_subject->setError($e->getMessage());
返回false;
}
}
返回true;
}
公共函数onContentPrepare($context,&$article,&$params,$page=0)
{
如果(!isset($article->rating)| |!count($article->rating))
返回;
//为表添加额外的css
$doc=JFactory::getDocument();
$doc->addStyleSheet(JURI::base(true)。'/plugins/content/rating/rating/rating/rating.css');
//动态构建一个结果表
jimport('joomla.html.grid');
$table=newjgrid();
//创建列
$table->addColumn('attr')
->addColumn(“值”);
//填充
$rownr=0;
foreach($article->rating as$attr=>$value){
$table->addRow(数组('class'=>'row')($rownr%2));
$table->setRowCell($attr',$attr);
$table->setRowCell('value',$value);
$rownr++;
}
//把桌子包在一个分类的盒子里
$suffix=$this->params->get('ratingclass_sfx','rating');
$html=''(字符串)$table'';
$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; ?>


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

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

幸运的是,Joomla允许您创建,管理的过程与前端模板的覆盖几乎相同

幸运的是,如果您使用的是默认的管理模板
Isis
只需复制,那么修改就在正确的文件中

/administrator/components/com\u content/views/article/tmpl/edit.php

致:

/administrator/templates/isis/html/com\u content/article/edit.php


您可能希望还原原始文件,但正如前面提到的,它几乎肯定会在安全更新或版本更新中得到更新。

在另一篇文章中找到了解决您(和我)问题的方法,希望有所帮助。示例插件似乎包含一个错误

是否添加了模具;确定插件是否真的加载了