Jump to content

Add general information to specific template pages in admin


Recommended Posts

Hi guys,

I think what I try to achieve is quite simple, nevertheless I can’t find out how to do it.

On a specific template, I’d like to add some general description about the whole template.
It’s just some text that I’d like to insert at the beginning of the admin page when a page based on this template is edited.
I don’t need to create a whole new custom admin page.
Any clue?

Thanks

Link to comment
Share on other sites

Hi,
Using ProcessWire notice system you could do something like

<?php // In /site/ready.php
$wire->addHookAfter('ProcessPageEdit::execute', function (HookEvent $event) {
  $pageEdit = $event->object;
  $page = $pageEdit->getPage();
  if ($page->template == 'home') {
    $this->wire('notices')->add(new NoticeMessage("Hello World"));
  }
});

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

In addition to the great suggestion from @Nicolas, you can also do it by prepending a Markup textfield to the edit form for just that particular template.

Here's more code - I'd put this in site/templates/admin.php as this is only an admin feature.

$pages->addHookAfter("ProcessPageEdit::buildForm", function($event) {
    $form = $event->return;

    $id = (int) wire("input")->get("id");
    if (!$id) {
        return;
    }

    $editedPage = wire("pages")->get($id);
    if ($editedPage->template->name === "home") { // CHANGE TEMPLATE NAME HERE
        $f = wire()->modules->get('InputfieldMarkup');
        $f->label = "Notes";
        $f->value = "<p>Your HTML Notes in here.</p>";
        $form->prepend($f);
    }
});

image.png.3d9a2548ff50d80f81425d5ef90cc559.png

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Or when using Custom Page Classes + MagicPages you have your code where it belongs (if it's for the "home" template then it's in the HomePage.php file) and avoid hook hell in site/ready.php

// site/classes/HomePage.php

class HomePage extends DefaultPage
{
  use MagicPage;

  public function editForm($form): void
  {
    $f = new InputfieldMarkup();
    $f->label = 'Help';
    $f->value = 'Foo bar baz!';
    $form->prepend($f);
  }
}

zOfWYRN.png

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Thank you guys. You are amazing.

I prefer not to use modules when what I want can be achieved with a few lines (this is an old habit I got with WordMessPress).

I’ll try your suggestions. And I’ll investigate about this MagicPage. I read this name three times yesterday and I have no clue about what it is.

  • Like 2
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...