Skip to content

Instantly share code, notes, and snippets.

@mhmohona
Last active March 16, 2024 03:06
Show Gist options
  • Select an option

  • Save mhmohona/6d04d7913f1a478514bb23e6ff889cae to your computer and use it in GitHub Desktop.

Select an option

Save mhmohona/6d04d7913f1a478514bb23e6ff889cae to your computer and use it in GitHub Desktop.

Running Hooks in the New System

Updating extension.json

To start using the new hook system, you need to update your extension's extension.json file. Define a handler class for your hooks by adding a new section called hookHandlers to the file:

{
    "name": "MyExtension",
    "version": "1.0",
    "author": "Your Name",
    "hookHandlers": {
        "default": "MyExtensionHooks"
    },
    "AutoloadClasses": {
        "MyExtensionHooks": "includes/MyExtensionHooks.php"
    }
}

Converting Static to Instance Methods

Next, you'll need to convert your existing static methods into instance methods in the MyExtensionHooks class:

// MyExtensionHooks.php

class MyExtensionHooks {
    // Convert static method to instance method
    public function onArticleFromTitle(Title &$title, &$page, &$redirect, &$article) {
        // Your hook logic here
    }

    // Add more instance methods for other hooks as needed
}

Hook Invocation

In your extension's code, use the Hooks class to run the hooks:

// Inside your extension code

Hooks::run('ArticleFromTitle', [ &$title, &$page, &$redirect, &$article ]);

Dependency Injection and Configuration

Define constructor parameters in your handler class to receive dependencies:

// MyExtensionHooks.php

class MyExtensionHooks {
    private $config;

    // Constructor with dependency injection
    public function __construct( Config $config ) {
        $this->config = $config;
    }

    // Instance method using injected dependency
    public function onArticleFromTitle(Title &$title, &$page, &$redirect, &$article) {
        // Access configuration settings
        $setting = $this->config->get('MyExtensionSetting');
        
        // Your hook logic here
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment