Jump to content

module update function


jploch
 Share

Recommended Posts

Hey folks.
Iam working on a module that creates some templates and fields.
The module creates these on install and deletes them on uninstall, works great.

Now Iam looking for a way to run the create function when the module is updated, but It's not doing anything.
I changed the version number and hit refreh on the module manager and a message indicated the update, but my function is not executed.
Not sure if Iam hooking the right function here or if there is an __update() function I can use?
Here is my code so far:

<?php namespace ProcessWire;

class BlockEditor extends WireData implements Module {

  public static function getModuleInfo() {
    return [
      'title' => 'Editor for PageGrid',
      'summary' => 'Installs Template and Fields for a PageGrid Block',
      'author' => 'Jan Ploch',
      'icon' => 'font',
      'autoload' => 'template=admin',
      'version' => 1
    ];
  }

  public function getBlockName() {
    return array(
      'name' => 'pg_editor',
      'label' => 'Editor',
    );
  }

  //function to create block
  public function createBlock() {
    $this->message("Create Block");
    $this->log->save("PageGridBlock", "Create Block");

    $fs = wire('fields');
    $t = wire('templates');
    $blockName = $this->getBlockName()['name'];
    $blockLabel = $this->getBlockName()['label'];

    if (!$fs->get($blockName)) {
      $f = new Field;
      $f->type = $this->modules->get("FieldtypeTextarea");
      $f->name = $blockName;
      $f->label = $this->_($blockLabel);
      $f->tags = 'pgrid';
      $f->save();
    }

    if (!$t->get($blockName)) {

      $f = $fs->get($blockName);
      $titleF = $fs->get('title');

      // fieldgroup for template
      $fg = new Fieldgroup();
      $fg->name = $blockName;
      $fg->add($titleF);
      $fg->add($f);
      $fg->save();

      $t = new Template();
      $t->name = $blockName;
      $t->fieldgroup = $fg;
      $t->icon = $this->getModuleInfo()['icon'];
      $t->save();
    }

    // Copy block files
    $copyFrom = $this->config->paths->BlockEditor . "blocks/";
    $copyTo =  $this->config->paths->templates . "blocks/";
    $this->files->copy($copyFrom, $copyTo);
  }

  //function to remove block
public function removeBlock() {
  $this->message("Remove Block");

    $blockName = $this->getBlockName()['name'];
    $t = $this->templates->get($blockName);
    $fg = $this->fieldgroups->get($blockName);
    $f = $this->fields->get($blockName);

    if ($t && $f && $t->getNumPages() > 0 && $f->getNumPages() > 0) {
      throw new WireException("Can't uninstall because template or fields been used by some pages.");
    } else {
      if ($t) {
        wire('templates')->delete($t);
      }
      if ($fg) {
        wire('fieldgroups')->delete($fg);
      }
      if ($f) {
        wire('fields')->delete($f);
      }
    }
}

  public function init() {
    // trigger  on module update
    $this->addHookAfter('Process::upgrade', function (HookEvent $event) {
      bd('update');
      $this->createBlock();
    });
  }

  public function ___install() {
    $this->createBlock();
  }

  public function ___uninstall() {
    $this->removeBlock();
  }
  
}

 

Link to comment
Share on other sites

@monollonom Thank you! Well what can I say. I swear I tried this and it was not working. I gave it another try and after uninstalling and reinstalling the module it suddenly worked. Sorry! Will read the docs more carefully before I post next time. At least my code may help someone.

  public function ___upgrade($fromVersion, $toVersion) {
    $this->createBlock();
  }

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...