Adding new field in article manager in frontend of Joomla 3.0

▼魔方 西西 提交于 2019-12-13 02:19:24

问题


I am trying to add new field in new article in frontend of joomla as described here: http://docs.joomla.org/Adding_custom_fields_to_the_article_component

But the fields are not shown on the form. Can anybody explain me the reason?

Below is my code written in plugin file:

function onContentPrepareForm($form, $data)
{
    if (!($form instanceof JForm))
    {
        $this->_subject->setError('JERROR_NOT_A_FORM');
        return false;
    }

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

The one thing I observed is that, in my com_content folder inside component, the fixed code is written and that is the reason, why my fields are not visible. Is it ok if I change the file: \components\com_content\views\form\tmpl


回答1:


I found out that there is another bug in the tutorial, at least concerning my version of Joomla which is 2.5.14.

In you /rating/rating.xml (the form description, not the manifest) you need to change "

<fields name="rating">

to

<fields name="attribs">

There is something special about using the "attribs" name in order for the plugin to work.




回答2:


Based on the conversation in the comments above, I think you are subject to a poorly built tutorial. Well the document never states this, you actually have to wrap this function in a class for the CMS to call the function properly.

Even without the class wrapper, the file will be loaded into the CMS, so if you put a die or exit statement outside of the function, you should see it work. If you put it in the function, it won't be called, because your function will never be called.

You can download the full plugin from the tutorial that you reference at the bottom of the tutorial or here: http://joomlacode.org/gf/download/trackeritem/28771/75013/plg_content_rating-2.5.0.zip. If you look at this, you will see the extra pieces that are needed for this to actually function. I'm including it as well below.

Any of the other functions would also have to go in the class. Also, if you have changed the name of the plugin at all, the class name would also need to be updated. This can be pretty tricky, since the naming convention is very strict and requires both the plugin name (rating) and the plugin group (content) to be correct.

Code:

class plgContentRating extends JPlugin
{
    function onContentPrepareForm($form, $data)
    {
        if (!($form instanceof JForm))
        {
            $this->_subject->setError('JERROR_NOT_A_FORM');
            return false;
        }

        // 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');
        if (!$form->loadFile('rating', false)) {
            die('No Form');
        }
        return true;
    }
}

*UPDATE: Ok, so it wasn't just the missing class, but I'm leaving that just in case it helps someone else.

I updated the above code to do a check on the loadFile to see if it is working.



来源:https://stackoverflow.com/questions/18698985/adding-new-field-in-article-manager-in-frontend-of-joomla-3-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!