Jump to content

Module should load on the (any) page edit in the backend


Recommended Posts

Posted (edited)

I created this as a hook and it's working great, so I decided to turn it into a module to share. We have option in the backend to calculate number of characters but this script is calculating the width of the text as it should be displayed in Google's SERP for the meta title and meta description. However, it's not working for some reason (that I can't identify).

This (primitive) module should load on the (any) page edit in the backend. As you can see, it's supposed to load two files. I'm not sure why it's not functioning.

// Module: SeoTextWidth.module

class SeoTextWidth extends WireData implements Module {

  public static function getModuleInfo() {
    return [
      'title' => 'SEO field Text Width module',
      'summary' => 'This module is calculating width of text in the fields named seo and desc.',
      'version' => 1
    ];
  }

  public function __construct() {
    parent::__construct();
  }

  public function init() {
    $this->addHookAfter('ProcessPageEdit::render', [$this, 'addResources']);
  }

  public function addResources(HookArgs $args) {
    $output = $args->getOutput();
    $page = $args->wire('page');

    // Check if we're on the page edit view for a valid page
    if ($page && $page->editable()) {

      $jsUrl = $this->config->urls->scripts . 'SeoTextWidth.js';
      $output->append("<script src='$jsUrl'></script>");

      $cssUrl = $this->config->urls->styles . 'SeoTextWidth.css';
      $output->prepend("<link rel='stylesheet' href='$cssUrl' />");
    }

    return $output;
  }
}

 

Edited by Leftfield
I am sorry. Mods, please move this post in the Module/Plugin Development
Link to comment
Share on other sites

Resolved.

 

  public function init() {
    $this->addHookAfter('ProcessPageEdit::buildForm', $this, 'addResources');
  }

  public function addResources(HookEvent $event) {
	  $page = $event->object;   
	
	  $this->config->styles->add($this->config->urls->SeoTextWidth . "SeoTextWidth.css");
	  $this->config->scripts->add($this->config->urls->SeoTextWidth . "SeoTextWidth.js");
      
	  }

 

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...