0

I have created a plugin that adds a radio selection and text fields to back-end Article creation (and I thought edit as well) but the functionality behind those custom inputs only fires on creation. I need the functionality to apply on edit saves as well since the article in question will be used dozens of times. Is there a way to make this happen?

plugin XML:

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.4" type="plugin" group="content" method="upgrade">
    <name>plg_content_articlepublishemail</name>
    <author>---</author>
    <creationDate>July 2016</creationDate>
    <copyright>(C) 2005 - 2016 Open Source Matters. All rights reserved.</copyright>
    <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
    <authorEmail>---</authorEmail>
    <authorUrl>---</authorUrl>
    <version>1.0</version>
    <description>PLG_CONTENT_ARTICLEPUBLISHEMAIL_XML_DESCRIPTION</description>
    <files>
        <folder>language</folder>
        <folder>forms</folder>
        <filename plugin="articlepublishemail">articlepublishemail.php</filename>
    </files>
    <languages>
        <language tag="en-GB">en-GB.plg_content_articlepublishemail.ini</language>
    </languages>      
    <config>
        <fields name="params">
        </fields>
    </config>
</extension>

content XML:

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fields name="attribs">
        <fieldset name="articlepublishemail" label="Emailer">
            <field
                name="email_send"
                type="radio"
                default="0"
                label="PLG_CONTENT_ARTICLEPUBLISHEMAIL_EMAIL_SEND_LABEL">
                <option name="sendoff" id="sendoff" value="0">JOFF</option>
                <option name="sendon" id="sendon" value="1">JON</option>
            </field>
            <field
                name="first_email_sendto"
                type="text"
                label="PLG_CONTENT_ARTICLEPUBLISHEMAIL_FIRST_EMAIL_SENDTO_LABEL"
            />
            <field
                name="second_email_sendto"
                type="text"
                label="PLG_CONTENT_ARTICLEPUBLISHEMAIL_SECOND_EMAIL_SENDTO_LABEL"
            />
            <field
                name="third_email_sendto"
                type="text"
                label="PLG_CONTENT_ARTICLEPUBLISHEMAIL_THIRD_EMAIL_SENDTO_LABEL"
            />
        </fieldset>
    </fields>
</form>

PHP:

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

class plgContentArticlepublishemail extends JPlugin
{
    protected $autoloadLanguage = true;

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

        $option = $app->input->get('option');

        switch($option)
        {
            case 'com_content' :
                if ($app->isAdmin())
                {
                    JForm::addFormPath(__DIR__ . '/forms');
                    $form->loadFile('content',false);
                }
                return true;
        }
        return true;
    }

    function onContentBeforeSave($context, $article, $isNew)
    {
        $id = $article->id ;

        $app = JFactory::getApplication();

        $attribs = json_decode($article->attribs);

        $confirmed_to_send = "" . $attribs->email_send ;

        if($confirmed_to_send == '1')
        {
            // do some stuff with the sendto fields
        }
        return true;
    }
}
?>
0

Through waarnemer's suggestion on the Joomla forums, simply by switching the plugin event from onContentBeforeSave to onContentAfterSave, functionality remained after initial save. This seems to have been a blunder on my part in that I did not understand exactly what the Plugin/Events/Content tutorial was trying to convey for those two events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.