Jump to content
Robin S

Hanna Code: automatically create files for PHP tag code

Recommended Posts

When I create a new Hanna Code tag I am always creating a PHP tag (I don't think I've ever had a need to create a text or Javascript tag). And I prefer to edit my tag code in my IDE rather than in the code field within the Hanna Code module. Because of this my Hanna codes always consist of...

include $config->paths->templates . "hannas/{$hanna->name}.php";

...which just includes a file named the same as the Hanna tag from a "hannas" folder in /site/templates/

Always on the lookout for efficiencies, I had a go at automating the process of setting up new Hanna tags and come up with the following. Maybe it's useful to someone.

In /site/ready.php:

// Pre-fill code for new Hanna tags and create file
$wire->addHookBefore('ProcessHannaCode::executeEdit', function(HookEvent $event) {
    $id = (int) $this->input->get('id');
    // Include code for later use
    $file_include_code = '<?php
include $config->paths->templates . "hannas/{$hanna->name}.php";';
    if(!$id) {
        // A new Hanna tag is being added
        // Set type to PHP
        $this->addHookBefore('InputfieldRadios(name=hc_type)::render', function(HookEvent $event) {
            $inputfield = $event->object;
            $inputfield->value = 2;
        // Set code to include file of same name as tag
        $this->addHookBefore('InputfieldTextarea(name=hc_code)::render', function(HookEvent $event) use ($file_include_code) {
            $inputfield = $event->object;
            $inputfield->value = $file_include_code;
    } else {
        // An existing Hanna tag is being edited (the new tag has been saved)
        // Get the data for this tag
        /* @var \PDOStatement $query */
        $query = $this->database->prepare("SELECT name, type, code FROM hanna_code WHERE id=:id");
        $query->bindValue(':id', $id);
        if(!$query->rowCount()) throw new WireException("Unknown ID");
        list($name, $type, $code) = $query->fetch(\PDO::FETCH_NUM);
        // If it's a PHP tag and the tag code matches the include code...
        if($type == 2 && $code === $file_include_code) {
            $filename = $this->config->paths->templates . "hannas/{$name}.php";
            // Check if there is an existing file and if not...
            if(!file_exists($filename)) {
                // Define the contents of the file
                // Just the namespace and API variables for IDE code-completion
                // Some of this is PhpStorm-specific so adjust as needed
                $contents = '<?php namespace ProcessWire;
//<editor-fold desc="API variables">
 * @var Config $config
 * @var Fieldgroups $fieldgroups
 * @var Fields $fields
 * @var Languages $languages
 * @var Modules $modules
 * @var Page $page
 * @var Pages $pages
 * @var Paths $urls
 * @var Permissions $permissions
 * @var ProcessWire $wire
 * @var Roles $roles
 * @var Sanitizer $sanitizer
 * @var Session $session
 * @var Templates $templates
 * @var User $user
 * @var Users $users
 * @var WireCache $cache
 * @var WireDatabasePDO $database
 * @var WireDateTime $datetime
 * @var WireFileTools $files
 * @var WireInput $input
 * @var WireLog $log
 * @var WireMail $mail
 * @var \ProCache $procache
 * @var \FormBuilder $forms

                // Create a file and insert the contents above
                file_put_contents($filename, $contents);


  • Like 8

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...