简体   繁体   English

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

[英]onSave method for Editor Plugin in Joomla 3.6

I try to create a plugin which will display some text after creation of article in the editor. 我尝试创建一个在编辑器中创建文章后将显示一些文本的插件。

/editors/materialwords/materialwords.xml: /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: /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. 最好的问候,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. 实际上,编辑器插件是可以在joomla后台中使用的编辑器类型,如果您进入系统-后台中的配置,对于默认编辑器,您可以选择插件,因为它是一个编辑器插件。

If you want to perform some action after save an article you'll have to write a content plugin with method onContentAfterSave(). 如果要在保存文章后执行某些操作,则必须使用onContentAfterSave()方法编写内容插件。 This method takes 3 arguments : 此方法采用3个参数:

  1. $context, in your case it should be com_content.article, so test it before soing anything else $ context,在您的情况下应为com_content.article,因此请先测试一下
  2. $article, the instance of the article you are saving $ article,您要保存的文章的实例
  3. $isNew, a boolean, true if it is a new article, false if it is an udpate $ isNew,一个布尔值,如果是新文章,则为true,如果是udpate,则为false

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 插件覆盖保存在javascript中

With previous method you can not add javascript in this page. 使用以前的方法,您不能在此页面中添加javascript。 If you want to do such you have to add a onBeforeRender method. 如果要这样做,则必须添加onBeforeRender方法。 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). 单击管理按钮时,将调用javascript方法Joomla.submitbutton,因此您可以覆盖它(请务必先保存它,以便可以在处理后调用它)。

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 onExtensionBeforeSave

  2. onExtensionAfterSave onExtensionAfterSave

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM