0

I created content plugin to extend category option. This plguin could add second image to category, it works well. But when I uninstall this plugin, the image is still in the database.

What I want is: the data is also removed when the plugin is uninstalled. How?

I created 3 files:

  1. categorydrawing.php
  2. categorydrawing.xml
  3. categorydrawingimage.xml

Code in categorydrawing.php

<?php
// no direct access
defined ('_JEXEC') or die;
class plgContentCategoryDrawing extends JPlugin
{
    /**
     * Load the language file on instantiation.
     * Note this is only available in Joomla 3.1 and higher.
     * If you want to support 3.0 series you must override the constructor
     *
     * @var boolean
     * @since <1.0>
     */
    protected $autoloadLanguage = true;
    /**
     * Prepare form and add my field.
     *
     * @param   JForm  $form  The form to be altered.
     * @param   mixed  $data  The associated data for the form.
     *
     * @return  boolean
     *
     * @since   <1.0>
     */
    function onContentPrepareForm($form, $data)
    {
        $app    = JFactory::getApplication();
        $option = $app->input->get('option');
        switch($option)
        {
            case 'com_categories' :
                if ($app->isClient('administrator'))
                {
                    JForm::addFormPath(__DIR__);
                    $form->loadFile('categorydrawingimage', false);
                }
                return true;
        }
        return true;
    }
}

?>

Codes in categorydrawing.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" group="content" method="upgrade">
    <name>Category Drawing</name>
    <description>To add drawing to category</description>
    <files>
        <filename plugin="categorydrawing">categorydrawing.php</filename>
        <filename>categorydrawingimage.xml</filename>
    </files>
</extension>

Codes in categorydrawingimage.xml

<?xml version="1.0" encoding="UTF-8"?>
<form>
<config>
    <fields name="params">
        <fieldset name="drawing_image_short_description" label="Drawing Image and Description">

                <field
                    name="drawing_image"
                    type="media"
                    label="Drawing Image"
                />

                <field
                    name="drawing_image_alt"
                    type="text"
                    label="Drawing Image Description (Alt Text)"
                />
        </fieldset>
    </fields>
    </config>
</form>
1
  • What codes I should put in script.php? Thanks
    – zhang
    Oct 10, 2023 at 3:05

1 Answer 1

1

You need to create an Installer script that is run when your Plugin is uninstalled that removes the extra data you have added with your plugin.

I couldn't find a Joomla 4 example but these articles for Joomla 3 should get you started, https://docs.joomla.org/J3.x:Creating_a_simple_module/Adding_an_install-uninstall-update_script_file/en and https://docs.joomla.org/Special:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_an_install-uninstall-update_script_file#helloworld.xml and https://docs.joomla.org/Extension_Installer/Installer_Hooks

The process involves you creating a script.php file that contains an uninstall method and within that method you provide the code to remove the extra fields from the params field of the #__category table. The code that you write would extract the JSON value from params, remove your drawing_image and drawing_image_alt entries and then write the the updated JSON value back to the params field.

To make sure the script.php will run when the Plugin is uninstalled you need to define the script to the manifest file of your plugin with the following element.

<scriptfile>script.php</scriptfile> 
1
  • Hi Irata, could you offer the codes in script.php? thanks
    – zhang
    Oct 10, 2023 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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