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.

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);
                }
            ');

        }

    }
}

if you want a plugin before save or after save event

  1. onExtensionBeforeSave

  2. onExtensionAfterSave