Soma I tested out here–nice job and congrats on your
first second module!
Just a few suggestions:
1. It's not safe to assume that PW admin is at /processwire/. For instance, the one I tested in is at /mysite/processwire/ (off a subdirectory), and someone else's might be at /admin/ or /my-private-url/ or something like that. As a result, you'll want to replace this:
<a href='/processwire/setup/field/edit?id={$id} ...
with this:
<a href='{$this->config->urls->admin}setup/field/edit?id={$id} ...
And likewise with the templates one.
2. I'm getting double field links with Page reference fields. This is because Inputfield::render actually gets called twice for a Page reference. It's a little different than the other fields since it lets you delegate the input to another Inputfield (like select, select multiple, checkboxes, radios, etc.). So InputfieldPage::render gets called first, and then the delegate Inputfield gets called second. The result is two of your links appear. As a result, I would suggest preventing this by either keeping a list of field names you've already populated, or just checking if your markup already appears in the output, like:
if(strpos($event->return, 'fieldEditLink')) return;
3. With hooks that get called multiple times, it's more efficient to move the if() before you add the hook, rather than have it within the hook. Or to word it differently, move this:
if($this->process == 'ProcessPageEdit' && $this->user->hasRole('superuser')){
…out of addShortcutLinks(), and into init(). The only problem is that init() gets called before the page is even loaded, so you can't determine if($page->process == 'ProcessPageEdit') yet. However, you can do this:
<?php
public function init() {
if(strpos($_SERVER['REQUEST_URI'], $this->config->urls->admin . 'page/edit/?id=') !== false && $this->user->hasRole('superuser')){
// add stylesheets
$this->config->styles->add($this->config->urls->HelperFieldLinks . "HelperFieldLinks.css");
// add a hook after each inputfield is rendered and modify the output
$this->addHookAfter('Inputfield::render', $this, 'addShortcutLinks');
}
}
The advantage of the approach above is that it significantly reduces the number of times your hook is called when it's not needed. There's nothing technically wrong with the way you were doing it before either, so I'm just mentioning this as an efficiency optimization.
Lastly, and perhaps more importantly, move your stylesheet loader into that if() statement as well (like in the code example above). No need to have your stylesheet loading when it's not going to be used.