Jump to content
Andi

[SOLVED] Show permanent message in admin when option is selected by user

Recommended Posts

Getting a little deeper into the ProcessWire state-of-mind here. I seriously think I wouldn't have come back to webdev if it wasn't for this wonderful little gem of a CMS.

I have an "Options" field added to all users on a site. If the user has anything other then "default" selected, I would like to show a permanent message in the admin like the one in the screenshot, only so that the user can't close it. As a friendly reminder that he changed that option from default to something crazy 🙂

I've read up on how to send messages to users, but where would I hook into to make this show up all the time in the backend?

https://processwire.com/api/ref/wire/message/

Thanks in advance!

2020-04-17 09_40_37-Seiten.png

Share this post


Link to post
Share on other sites

You could just put it into templates/admin.php. The file should already be there. Here is the entire default file plus a line for the notice:

<?php namespace ProcessWire;

/**
 * Admin template just loads the admin application controller, 
 * and admin is just an application built on top of ProcessWire. 
 *
 * This demonstrates how you can use ProcessWire as a front-end 
 * to another application. 
 *
 * Feel free to hook admin-specific functionality from this file, 
 * but remember to leave the require() statement below at the end.
 * 
 */
if ($user->isLoggedin() && $myCrazySetting) $this->message("This is the notice text");
require($config->paths->adminTemplates . 'controller.php'); 

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks a bunch @Jan Romero that was quick 🙂

Very elegant and simple. And works almost perfectly, but when I set the option from "crazy" back to "default", the message pops up one more time. When I refresh the backend or navigate to a different page in admin it's gone..

Going the other way around, from "default" to "crazy", the message pops up right away.

Nothing too major but I'm wondering how to work around that..

Share this post


Link to post
Share on other sites

Indeed, that’s because the the message is set before the admin controller actually runs and saves the the change.

Basically, every page in the admin backend executes a specific “Process”. You can hook into the base Process class to add your message only after a process has been executed:

$this->addHookAfter('Process::execute', function($event) {
    if ($this->wire('user')->isLoggedin() && $this->wire('config')->myCrazySetting)
        $this->message("Hope you know what you’re doing with that setting!");
});

This can be put in the same file, above the controller line.

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

Works a treat. Thanks for explaining, that was very helpful!

Here's the finished hook for future reference.

In templates/admin.php

// user template switch warning in backend
$this->addHookAfter('Process::execute', function($event) {
	// only works for logged in users that have a custom frontend template enabled
	if ($this->wire('user')->isLoggedin() && $this->wire('user')->template_switch->value != 'default') {
		$u = $this->wire('user')->name;
		$t = $this->wire('user')->template_switch->title;
		$p = $this->wire('config')->urls->admin . "profile";
		$this->wire->warning("Frontend der Seite wird für User $u aktuell mit dem Template \"$t\" ausgegeben. <small>Einstellung \"Frontend Template\"im <a href='$p'>Benutzer-Profil</a></small>", Notice::allowMarkup);
	}
});

 

  • Like 3

Share this post


Link to post
Share on other sites

As it seems you are quite new to PW and motivated I suggest to avoid if(this AND that AND that):

// user template switch warning in backend
$this->addHookAfter('Process::execute', function($event) {
	$user = $this->wire('user');

	// early exit if user is not logged in
	if(!$user->isLoggedin()) return;

	// early exit if template is default
	if($user->template_switch->value == 'default') return;

	// show message to change template
	$u = $user->name;
	$t = $user->template_switch->title;
	$p = $this->wire('config')->urls->admin . "profile";
	$this->wire->warning("...", Notice::allowMarkup);
});

Of course this is totally up to you or anybody but IMHO this improves readability a lot. 🙂 

  • Like 2

Share this post


Link to post
Share on other sites

Thanks @bernhard, it really does 😉

I'm in my 5th or so project with PW but haven't really used hooks so far. Looks like the next best thing to a love affair 🙂

Thank you both again for your help, greatly appreciated!

 

Share this post


Link to post
Share on other sites

Hey again,

following @bernhard's suggestion of making stuff a little more readable, and after some refactoring, here's the result:

In templates/admin.php

// user custom theme warning in backend
$this->addHookAfter('Process::execute', function($event) {
	// make stuff more readable
	$user = $this->wire('user');
	// early exit if user is not logged in
	if(!$user->isLoggedin()) return;
	// array of themes that will be excluded
	$x = array('', 'default');
	// early exit if current user theme is in excluded array
	if(in_array($user->custom_theme->value, $x)) return;
	// set up message
	$u = $user->name;
	$t = $user->custom_theme->title;
	// set up path for profile link
	$p = $this->wire('config')->urls->admin . "profile";
	// send message
	$this->wire->warning("Frontend wird für Benutzer \"$u\" aktuell mit dem Theme \"$t\" ausgegeben.
						<small>Einstellung \"Frontend Theme\"im <a href=\"$p\">Benutzer-Profil</a></small>", Notice::allowMarkup);
});

Also added an array of values to be excluded to make this more reusable in the future.

1042169837_2020-04-1807_54_22-ProfilProcessWirelocalhost.thumb.png.2769f4a1466aec1a26d444afcaadf63c.png

Looking neat, working as expected. Thanks again and servus from Bavaria!

 

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By picarica
      so i am trying to put CustomHooksForVariations.module, a custom module, i am placing it into site/modules direcotry yet my modules page in admin panel gives me errors
      so this is the screen show when i refresh modules, i dont know why the shole hook is written on top of the page :||

      and this next image is when i try to install it, i saw that it is not defiuned modules.php but it shouldnt need to be ?, any ways i dont want to edit site's core just to make one moulde work there has to be a way

    • By DanielKit
      Hi. I'm currently stuck at the login page in my project. Once I enter my admin username and my password and press login, nothing happens. The page just reloads. However, the URL changes from http://myipaddress/processwire to http://myipaddress/processwire/?login=1. I've checked all of my server settings, and to my knowledge, all seems to be fine there. I don't know where to go from here.. Thanks in advance!
    • By modifiedcontent
      Small annoyance: I get a horizontal scrollbar in UIkit admin area - or actually Admin Theme Boss based on Uikit 3. I tried to fix it with CSS, but had trouble isolating/targeting it and don't want to mess with module or core files. I think this issue has been reported before. Is there a recommended fix? 
    • By codevark
      The Minimal Site works fine, but if I try to login as admin, I have no idea what the credentials are.
      I did not do the "install" process myself. The OVA came with everything set up, but there's no mention of the PW admin credentials being set to some initial value.
      Wondering if anyone else has tried the Bitnami OVA and if there's a simple answer.
      --Pete
    • By vwatson
      I don't have any fontawesome icons in the admin section. Not sure what's wrong. They used to appear. I've tried all the user profiles and they're gone in all of them.
      Using the browser inspector tool, I see this message:
      Access to font at 'https://www.domain.com/wire/templates-admin/styles/font-awesome/fonts/fontawesome-webfont.ttf?v=4.7.0' from origin 'http://domain.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
      I'm not sure why this is occurring or how to fix it. Any help would be greatly appreciated. 
×
×
  • Create New...