doutuobao9736 2016-12-15 07:51
浏览 80

Joomla 3.6中编辑器插件的onSave方法

I try to create a plugin which will display some text after creation of article in the editor.

/editors/materialwords/materialwords.xml:

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="editors">
    <name>Editor Material Words Count Plugin</name>
    <creationDate>December 2016</creationDate>
    <author>Aleksandr Lapenko</author>
    <authorEmail>lapenkoak@gmail.com</authorEmail>
    <authorUrl>vk.com/web_rider</authorUrl>
    <copyright>Copyright</copyright>
    <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
    <version>1.0.0</version>
    <description>Calculate articles words count</description>
    <files>
        <filename plugin="materialwords">materialwords.php</filename>
    </files>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field name="displayCount" type="text"
                       label="Display Count"
                       description="Words display count"
                       required="true"
                       size="10"
                       class="inputbox" />
            </fieldset>
        </fields>
    </config>
</extension>

/editors/materialwords/materialwords.php:

<?php
defined('_JEXEC') or die;

class PlgEditorMaterialwords extends JPlugin
{
    public function onSave($id)
    {
        return 'alert("' . $id . '");';
    }
}

I install plugin and enable it. But something wrong (nothing when I save article in editor). Please, help. Best regards, Aleksandr.

  • 写回答

2条回答 默认 最新

  • duanchanguo7603 2016-12-15 09:02
    关注

    Actually editors plugins are type of editors that you can use in joomla back office, if you go in system - configuration in back office, for default editor you'll can choose your plugin because it is an editors plugin.

    If you want to perform some action after save an article you'll have to write a content plugin with method onContentAfterSave(). This method takes 3 arguments :

    1. $context, in your case it should be com_content.article, so test it before soing anything else
    2. $article, the instance of the article you are saving
    3. $isNew, a boolean, true if it is a new article, false if it is an udpate

    Plugin code

    class PlgContentMaterialwords extends JPlugin {
    
        public function onContentAfterSave($context, $article, $isNew){
    
            if ($context != 'com_content.content'){
                return true;
            }
    
            // do stuff
    
        }
    
    }
    

    Plugin declaration

    <?xml version="1.0" encoding="utf-8"?>
    <extension version="3.1" type="plugin" group="content">
        <name>Editor Material Words Count Plugin</name>
        <creationDate>December 2016</creationDate>
        <author>Aleksandr Lapenko</author>
        <authorEmail>lapenkoak@gmail.com</authorEmail>
        <authorUrl>vk.com/web_rider</authorUrl>
        <copyright>Copyright</copyright>
        <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
        <version>1.0.0</version>
        <description>Calculate articles words count</description>
        <files>
            <filename plugin="materialwords">materialwords.php</filename>
        </files>
        <config>
            <fields name="params">
                <fieldset name="basic">
                    <field name="displayCount" type="text"
                           label="Display Count"
                           description="Words display count"
                           required="true"
                           size="10"
                           class="inputbox" />
                </fieldset>
            </fields>
        </config>
    </extension>
    

    Plugin to override save in javascript

    With previous method you can not add javascript in this page. If you want to do such you have to add a onBeforeRender method. When clicking on an admin button, javascript method Joomla.submitbutton is called, so you can override it (take care of saving it before so you can call it after your process).

    class PlgContentMaterialwords extends JPlugin
    {
        public function onBeforeRender()
        {
    
            if ( JFactory::getApplication()->isAdmin() ){
    
                $document = JFactory::getDocument();
                $document->addScriptDeclaration('
                    var Myvar = {};
                    Myvar.submitbutton = Joomla.submitbutton;
                    Joomla.submitbutton = function(task) {
                        if ( task == "article.save" || task == "article.apply" || task == "article.save2new" ){
                            alert("foo");
    
                        }
                        Myvar.submitbutton(task);
                    }
                ');
    
            }
    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题