1

In my Craft 2 plugin my initial init function looked like:

public function init()
{
    parent::init();

    if(!extension_loaded('imagick')) {
        PdfThumbnailerPlugin::log("imagick library not found - cannot make pdf thumbnails" , LogLevel::Warning);
        return;
    }

    craft()->on('entries.saveEntry', function(Event $event) {
         craft()->pdfThumbnailer_saveEntry->setThumb($event);
    });

    craft()->on('assets.saveAsset', function(Event $event) {
        PdfThumbnailerPlugin::log("on save asset");
        craft()->pdfThumbnailer_saveAsset->checkAsset($event);
    });

}

I cannot understand (due to lack of devchops) or find an example of how to call my functions (which were in service files) in Craft3.

I can see the constants used in the new style 'listener' but have no idea what or where these are referenced in the v3 docs.

    // Register our site routes
    Event::on(
        UrlManager::class,
        UrlManager::EVENT_REGISTER_SITE_URL_RULES,
        function (RegisterUrlRulesEvent $event) {
            $event->rules['siteActionTrigger1'] = 'pdfthumbnailer/save-entry';
            $event->rules['siteActionTrigger2'] = 'pdfthumbnailer/save-asset';
        }
    );

My plugin in craft 2 would create a thumbnail from the first page of a PDF asset.

UPDATE: SO i eventually made some progress with :

Pdfthumbnailer::$plugin->SaveEntry->setThumb($event);

Except this kept saying it could not find service method:

Getting unknown property: joomkit\pdfthumbnailer\Pdfthumbnailer::SaveEntry 

It turns out that the method 'SaveEntry' only was found when named 'saveEntry'. Why would this be the case? (excuse pun)

  • SaveEntry in your case is the name of a component. There is a function registerComponents you should check what parameters you pass to this function – Robin Schambach Jul 2 '18 at 11:39
  • Brilliant Robin. Its defined in composer extra ->components->saveEntry Is this the norm for service declaration now? – joomkit Jul 2 '18 at 11:53
  • I suggest you to remove that part during development – Robin Schambach Jul 2 '18 at 11:54
1

I can't answer the question fully without seeing your plugin code, but I'd suggest you generate your plugin scaffolding with pluginfactory.io to see how it all fits together.

It's also helpful to look at a existing plugin that works; here's one I wrote that's pretty simple, and should be easy to follow: FastCGI Cache Bust

Roughly, you'd do something like:

PluginClass::getInstance()->serviceName->methodName();

Or if you do it the pluginfactory.io way:

PluginClass::$plugin->serviceName->methodName();
  • Thanks andrew. Ironically I did use pluginfactory but was wondering what the on entry save event looks like in craft3. will dig in – joomkit Jul 2 '18 at 8:18

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.