3

I am trying to write a plugin to use Techjoomla's api component on my Joomla 3 site.

Their documentation gives sample code which I am altering to fit my needs. I am trying to make a very basic API which I can improve later.

I want to send:

https://mydomainname.co.uk/index.php?option=com_api&app=WDTOptions&resource=options

and get back:

id = 6, title=WDTOptions Test as part of the JSON response.

I have a public article on my site that I wish to access later. It has the same id and title. For now though I just want to get a response from the API component.

I have the following manifest file.

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.9.23" type="plugin" group="api" method="upgrade">
    <name>WDTOptions</name>
    <version>0.1</version>
    <creationDate>07/01/2021</creationDate>
    <author>Huw Evans</author>
    <description></description>
    <files>
        <filename plugin="WDTOptions">WDTOptions.php</filename>
        <folder>WDTOptions</folder>
    </files>
    <languages>
        <language tag="en-GB">/language/en-GB/en-GB.mod_articles_category.ini</language>
        <language tag="en-GB">/language/en-GB/en-GB.mod_articles_category.sys.ini</language>
    </languages>
</extension>

The file WDTOptions.php contains the following code

<?php

// Class structure example
class PlgAPIOptions extends ApiPlugin
{
    public function __construct(&$subject, $config = array())
    {
        parent::__construct($subject, $config = array());

        $lang = JFactory::getLanguage();
        $lang->load('com_content', JPATH_ADMINISTRATOR, '', true);

        // Set the options resource to be public
        $this->setResourceAccess('options', 'public', 'get');
    }
}

the file WDTOptions/options contains

<?php

class WDTOptionsApiResourceOptions extends ApiResource
{

    public function get()
    {
        $result       = new \stdClass;
        $result->name = "WDTOptions Test";
        $result->id = 6;

        $this->plugin->setResponse( $result );
    }
}

The only response I can get from the API is

{"err_msg":"WDTOptions API Plugin not found, WDTOptions is probably not installed","err_code":400,"response_id":"","api":"","version":"","data":{}}
10
  • 1
    Please include (by editing your question) the Joomla documentation pages, Stack Overflow/ Joomla Stack Exchange pages, and Joomla Forum threads that you found (which were relevant to your challenge) that did not help you. This will prevent our volunteers from suggesting pages that you already visited. Jan 8, 2021 at 13:12
  • And please if you can share your plugin codes, files, we might be able to spot the real problem, since language files cannot be real problems of the plugins... please also share more details what the plugin should do and what you really get, so on... thank you!
    – Zollie
    Jan 8, 2021 at 18:30
  • @Zollie Should I just put the whole lot in this same question? Or should I write a new one? I have also managed to establish over the course of the day that language files are not the problem.
    – Huw Evans
    Jan 8, 2021 at 18:49
  • I think you have to show us the xml manifest file of your plugin and the main plugin file’s code first. You should also turn development mode and error reporting maximum in your joomla global configuration in the admin, to see if you can see any error message on the page...
    – Zollie
    Jan 8, 2021 at 19:29
  • Please edit this question with the above info...
    – Zollie
    Jan 8, 2021 at 19:30

1 Answer 1

3

Besides the class name of the plugin as it should be formed like ([Plg][Plugin group name][Plugin name]), thus:

class PlgAPIWDTOptions extends ApiPlugin

It could also make your life easier if you just used small letters in your plugin's name instead of capital letters. So I would call your plugin: plg_api_wdtoptions and your XML correctly should look like this:

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.9.23" type="plugin" group="api" method="upgrade">
    <name>plg_api_wdtoptions</name>
    <version>0.1</version>
    <creationDate>07/01/2021</creationDate>
    <author>Huw Evans</author>
    <description></description>
    <files>
        <filename plugin="wdtoptions">wdtoptions.php</filename>
        <folder>wdtoptions</folder>
    </files>
    <languages>
    </languages>
</extension>

And your plugin main class finally would be:

class PlgAPIWdtoptions extends ApiPlugin

Later you will place two language files into your plugin if you need (but you really do not need them if you do not have admin control for this api plugin). You can study the official Joomla docs here about standard Joomla plugins for future reference: https://docs.joomla.org/Plugin

Your main plugin file should then look like this correctly:

<?php

defined('_JEXEC') or die;    
  
// Class structure example
class PlgAPIWdtoptions extends ApiPlugin
{
    public function __construct(&$subject, $config = array())
    {
        parent::__construct($subject, $config = array());

        // Set resource path
        ApiResource::addIncludePath(dirname(__FILE__) . '/wdtoptions');

        // $lang = JFactory::getLanguage();
        // $lang->load('com_content', JPATH_ADMINISTRATOR, '', true);

        // Set the options resource to be public
        $this->setResourceAccess('options', 'public', 'get');
    }
}

I changed the Class name to small letter version, so Wdtoptions (not WDT). It does not matter too much I just saw that easier to handle.

Please also note one line in the code and this is the most important in your case, the line in the above file which was missing from your main plugin file and that is why the com_api component has not found your Resource file (the path had to be included). The missing line was:

// Set resource path
ApiResource::addIncludePath(dirname(__FILE__) . '/wdtoptions');

Your plugin folder and file structure should be this:

wdtoptions.php (plugin entry file)
wdtoptions.xml (plugin manifest file)
wdtoptions (**Resources* folder having resource file, keep its name the same as your plugin name)*    
    options.php (Resource file)

The above is tested and working.

3
  • Thank you, Will test shortly to confirm.
    – Huw Evans
    Jan 9, 2021 at 17:23
  • 1
    Yes, working the way described in the question. Now for me to work out how to actually do something useful with it. :)
    – Huw Evans
    Jan 9, 2021 at 18:19
  • @HuwEvans - Great! I wish you the bests for developing your plugin further.
    – Zollie
    Jan 9, 2021 at 18:26

Your Answer

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

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