About Auto Template Stubs

Automatically creates stub files for templates when fields or fieldgroups are saved.

Category Development Tools
Modules that have more to do with assisting development of a site rather than managing its content.
Release StateBeta
Close to stable, but users are advised to be cautious and test thoroughly.*
Authorrobin-s
Module Version0.2.3
Class NameAutoTemplateStubs
Compatibility3.0
Date AddedJanuary 10, 2020
Last UpdatedAugust 12, 2020
Recommended ByNew recommendations may take up to 1 day to appear.

Instructions

This module's files should be placed in /site/modules/AutoTemplateStubs/
How to install or uninstall modules

README

Auto Template Stubs

A module for ProcessWire CMS/CMF. Automatically creates stub files for templates when fields or fieldgroups are saved.

Stub files are useful if you are using an IDE (e.g. PhpStorm) that provides code assistance - the stub files let the IDE know what fields exist in each template and what data type each field returns. Depending on your IDE's features you get benefits such as code completion for field names as you type, type inference, inspection, documentation, etc.

Installation

Install the Auto Template Stubs module.

Configuration

  • If you're using custom Page classes and want code assistance for methods in the custom Page class as well as field names you can tick the "Name stub classes for compatibility with custom Page classes" checkbox. This names the template stub classes using the same format as for custom Page class names. A side-effect of this is that your IDE may warn you that multiple definitions exist for your custom Page classes.

  • If you're not using the setting above you have the option to change the class name prefix. It's good to use a class name prefix because it reduces the chance that the class name will clash with an existing class name.

  • The directory path used to store the stub files is configurable.

  • There is a checkbox to manually trigger the regeneration of all stub files if needed.

Usage

Add a line near the top of each of your template files to tell your IDE what stub class name to associate with the $page variable within the template file. For example, with the default class name prefix you would add the following line at the top of the home.php template file:

/** @var tpl_home $page */

Or if you're using the "Name stub classes for compatibility with custom Page classes" setting then it would be:

/** @var HomePage $page */

Now enjoy code completion, etc, in your IDE.

stubs

Adding data types for non-core Fieldtype modules

The module includes the data types returned by all the core Fieldtype modules. If you want to add data types returned by one or more non-core Fieldtype modules then you can hook the AutoTemplateStubs::getReturnTypes() method. For example, in /site/ready.php:

// Add data types for some non-core Fieldtype modules
$wire->addHookAfter('AutoTemplateStubs::getReturnTypes', function(HookEvent $event) {
    $extra_types = [
        'FieldtypeDecimal' => 'string',
        'FieldtypeLeafletMapMarker' => 'LeafletMapMarker',
        'FieldtypeRepeaterMatrix' => 'RepeaterMatrixPageArray',
        'FieldtypeTable' => 'TableRows',
    ];
    $event->return = $event->return + $extra_types;
});

Credits

Inspired by and much credit to the Template Stubs module by mindplay.dk.