Jump to content

CKE pwlink: pages without templates shouldn't be selectable


Klenkes
 Share

Recommended Posts

This is something that bothers me since a long time.

I often use pages without a template file just to output a teaser or headline in between a list of other pages. This is easy to understand for editors. Moving them up or down or create morte of them.

Then when you edit a CKE textfield of some page and create a link to another page and use either the autocomplete field or the page tree, the pages without template file show up as well(see screenshot), even though those pages wouldn't be viewable anyway and result in a 404.

You say: why don't you make them hidden?
I say: well... different users edit pages and they would be able to set them to show again and... well it didn't work.

There might be hooks to set them hidden again but I don't know them...

However, I think pages without templates shouldn't even show up there. It just doesn't make sense. (or am I missing something)

Are there ways around this problem? Hook? Something?
Something to make the template name show in the autocomplete suggestions after the path?

 

The blue bordered pages are some of those pages.

pw-link-1.png.0523a596cdb1ae0e90b411c182a13ed2.png

Link to comment
Share on other sites

There are different inputfields in ProcessPageEditLink and they need to be treated differently: Link to URL, Select Page, Select Child Page.

For Link to URL I agree that there is probably no common use case that needs pages without template files to be visible/selectable here.

For Select Page and Select Child Page, I think that pages without template files should appear there because those pages may have viewable child pages, and also I think it is easier for users to understand these inputfields if they show the same pages that they are familiar with from ProcessPageList where non-viewable pages are listed. But the pages without template files should not be selectable as links.

I suggest you raise a GitHub request or issue to ask for a change to this.

In the meantime here are a couple of workaround hooks for /site/ready.php:

// ProcessPageEditLink > Link to URL: exclude pages using templates without template file
$wire->addHookBefore('InputfieldPageAutocomplete(name=link_page_url)::render', function(HookEvent $event) {
	$inputfield = $event->object;
	$fileless_templates = [];
	foreach($this->wire()->templates as $template) {
		if(!$template->filenameExists()) $fileless_templates[] = $template->id;
	}
	$inputfield->findPagesSelector .= ', template!=' . implode('|', $fileless_templates);
});

// ProcessPageEditLink > Select Page / Select Child Page: prevent selection of pages using templates without template file
$wire->addHookAfter('ProcessPageEditLink::execute', function(HookEvent $event) {
	$css = '';
	foreach($this->wire()->templates as $template) {
		if(!$template->filenameExists()) $css .= ".PageListTemplate_{$template->name} .PageListActionSelect, ";
	}
	if($css) {
		$css = rtrim($css, ', ');
		$css .= ' { display:none !important; }';
		$event->return .= "<style>$css</style>";
	}
});

 

  • Like 3
Link to comment
Share on other sites

You are absolutely right. My request would only be applicable for "Page URLs".  For "Select Page" and similar fields where you select a page id it wouldn't be good.

I will make a GitHub request.

And thank you very much for the hooks! Working great! It would have taken me hours to figure this out.

  • Like 1
Link to comment
Share on other sites

On 11/5/2018 at 12:09 AM, Robin S said:

In the meantime here are a couple of workaround hooks for /site/ready.php:


// ProcessPageEditLink > Link to URL: exclude pages using templates without template file
$wire->addHookBefore('InputfieldPageAutocomplete(name=link_page_url)::render', function(HookEvent $event) {
	$inputfield = $event->object;
	$fileless_templates = [];
	foreach($this->wire()->templates as $template) {
		if(!$template->filenameExists()) $fileless_templates[] = $template->id;
	}
	$inputfield->findPagesSelector .= ', template!=' . implode('|', $fileless_templates);
});

// ProcessPageEditLink > Select Page / Select Child Page: prevent selection of pages using templates without template file
$wire->addHookAfter('ProcessPageEditLink::execute', function(HookEvent $event) {
	$css = '';
	foreach($this->wire()->templates as $template) {
		if(!$template->filenameExists()) $css .= ".PageListTemplate_{$template->name} .PageListActionSelect, ";
	}
	if($css) {
		$css = rtrim($css, ', ');
		$css .= ' { display:none !important; }';
		$event->return = $event->return . "<style>$css</style>";
	}
});

 

I tried your second hook and I'm getting the following errors:

Notice: Undefined variable: template in C:\xampp\htdocs\mysite\site\ready.php on line 131
Notice: Trying to get property of non-object in C:\xampp\htdocs\mysite\site\ready.php on line 131

Link to comment
Share on other sites

1 hour ago, PWaddict said:

I tried your second hook and I'm getting the following errors:

Notice: Undefined variable: template in C:\xampp\htdocs\mysite\site\ready.php on line 131
Notice: Trying to get property of non-object in C:\xampp\htdocs\mysite\site\ready.php on line 131

My guess is that some non-printable character got pasted inside your code. See the comments in this thread from here down:

If that's not it then you can also use Tracy Debugger to debug why $template is undefined for you. All that the relevant code inside the hook is doing is looping over the $templates API variable with $template representing each Template object, so I can't see where else that could be going wrong.

  • Like 1
Link to comment
Share on other sites

14 hours ago, PWaddict said:

How to prevent selection of Trash page

These are already excluded from the autocomplete as far as I can tell.

14 hours ago, PWaddict said:

and unpublished pages?

// ProcessPageEditLink > Link to URL: exclude pages using templates without template file and unpublished pages
$wire->addHookBefore('InputfieldPageAutocomplete(name=link_page_url)::render', function(HookEvent $event) {
	$inputfield = $event->object;
	$fileless_templates = [];
	foreach($this->wire()->templates as $template) {
		if(!$template->filenameExists()) $fileless_templates[] = $template->id;
	}
	$inputfield->findPagesSelector .= ', status!=unpublished, template!=' . implode('|', $fileless_templates);
});

// ProcessPageEditLink > Select Page / Select Child Page: prevent selection of pages using templates without template file and unpublished pages
$wire->addHookAfter('ProcessPageEditLink::execute', function(HookEvent $event) {
	$css = '.PageListStatusUnpublished .PageListActionSelect, ';
	foreach($this->wire()->templates as $template) {
		if(!$template->filenameExists()) $css .= ".PageListTemplate_{$template->name} .PageListActionSelect, ";
	}
	$css = rtrim($css, ', ');
	$css .= ' { display:none !important; }';
	$event->return .= "<style>$css</style>";
});

 

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

@Robin S The Trash page itself is selectable but I fixed it by adding .PageListStatusHidden in the hook so now non-superusers can't select the Trash page itself or any other hidden page:

$css = '.PageListStatusUnpublished .PageListActionSelect, ';
$css .= '.PageListStatusHidden .PageListActionSelect, ';

Now only 1 issue remains. The pages inside Trash are selectable.

Link to comment
Share on other sites

  • 4 weeks later...

Admin note: since this is clearly not a support thread for a module, I'm moving the whole thread to the General Support area of the forum. Please note that Modules / Plugins is only intended for dedicated module support threads. Thanks!

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