Jump to content

How to bind a must-use name to a template?


pleini
 Share

Recommended Posts

Hi there,

how can I bind a name to a certain template?

E.g. if a new page is generated using the template "review", the page name (for the url) must be "review", the title as well "Review".

How can I reach this (I guess with hooks) and where should I add it?

Thank you for you help :)

Link to comment
Share on other sites

@diogo, Well, if he needs to bind the name and title with the template's name couldn't be done like this?

<?php

$p = new Page(); // create new page object
$p->template = 'review'; // set template
$p->parent = wire('pages')->get('/whatever/'); // set the parent 
$p->name = $template->name; // grab the name of the template and set page name with it
$p->title = $template->name; // same as above but with page title

$p->save(); 

Should work as expected I think...

Link to comment
Share on other sites

Well I'm not sure if understand the purpose. I mean you could create those pages manually because you can only create those pages with page-name=template-name only once (for any parent) anyway.

So could you may explain a little bit more of the context? Maybe there's a more elegant way to solve this.

Link to comment
Share on other sites

Thank you for your help!

I want to use the new PageTable module adding new pages with the backend.

Using several templates, I cannot use the given template naming since it's designed just for one template.

Link to comment
Share on other sites

What I would do: Give them all the same template and add a "switch" into the template file. Like if your template is called "page" put something like the following stuff into "page.php":

<?php

switch($page->name) {
	case 'apage':
		include 'page-apage.php';
	break;
	case 'anotherpage':
		include 'page-anotherpage.php';
	break;
	case 'maybeagallery':
		include 'gallery.php';
	break;
}
Link to comment
Share on other sites

Sorry for my bad explanations.

I create a new parent page (template movie, name star-wars, url /star-wars)

There are a couple of subtemplates now but each one should appear max 1 time:

/star-wars/review (Template movie_review)

/star-wars/comments (Template movie_comments)

/star-wars/actors (Template movie_actors)

...

I repeat it with a new movie:

/lord-of-the-rings/review (Template movie_review)

/lord-of-the-rings/comments (Template movie_comments)

/lord-of-the-rings/actors (Template movie_actors)

Inside the template movie I can create using the new module pagetables new pages with those subtemplates.

I have to make sure, that each move there is

- one review

- one or no comments

- one or no actors

page.

 
How can I achieve it?
Link to comment
Share on other sites

I think I understand it now. I would go with a custom module hooking into ProcessPageAdd::execute and check if this template already exists. If this is the case throw an error. Or alternatively check if the template is already set and if hide it in the drop down select.

Link to comment
Share on other sites

Edit: Not sure if it works with PageTable but it works with the traditional way.

Should work:

<?php

class SingleUseTemplate extends WireData implements Module {

	static public function getModuleInfo() {
		return array(
			'title'   => 'Single use template.',
			'summary' => __('Single use template.'),
			'version' => '100',
			'author'  => 'Nico Knoll',
			'singular' => true, 
			'autoload' => 'template=admin'
		);
	}

	public function init() {
		$this->addHookBefore('ProcessPageAdd::execute', $this, 'validateTemplate'); 
	}


	public function ___validateTemplate(HookEvent $event) {
		if($this->input->post->submit_save) {
			$chosenTemplate = $this->config->input->post->template;
				
			$parent = $this->pages->get($this->input->post->parent_id);
			
			// enter the template for the "parent" page. Like in your example "movie"
			if($parent->template->name != 'movie') return false;	
				
			// check if a sibling (same depth level) already uses this template
			foreach($parent->children as $child) {
				if((string)$child->template->id == $chosenTemplate) {
					throw new WireException('Template already used. Chose another one.');
				}
			}
		}
	}



} 

SingleUseTemplate.module

Edited by Nico Knoll
  • Like 1
Link to comment
Share on other sites

Another way to do this would be to pack all the infos in one template and use url-segments. Could be sorted in four tabs (main / review / comments / actor) with a checkbox to enable comments and/or actors. The code would be quite similar to nico's switch example. So you don't need to check, that there aren't duplicates, because it's all one page in the backend.

<?php

switch($input->urlSegment(1)) {
	case 'review':
		include 'page-apage.php';
	break;
	case 'comments':
		if($page->comments_enabled) include 'page-anotherpage.php';
	break;
	case 'actors':
		if($page->actors_enabled) include 'gallery.php';
	break;
}
  • Like 2
Link to comment
Share on other sites

I'm neebie here but like diogo said - is this possible with the setting....

1.page movies with subpages for main moviepage

  •  starwars
  •  indiana jones

2. page reviews (can only create reviews)

  • starwars
  • indiana jones

3. page actors

  • starwars
  • indiana jones

For fielddependencies this would be no problem choose the movie from a pageselection and bind the review to it - but:

- is there a chance to get the url to be /moviename/review with this separation of content with parentpages and subpages?

 (may combine this contentsetup with LostKobrakai's code with URL segments but manage content with different parents as cathegories??)

- you have to make adminpages for problems regarding missing content (show me all movies without a review....and so on)

- may combine this with nicos module to setup no double create a actors page for the movie starwars...???

For 2-4 Contentblocks the Tabsolution with url segments from LostKobrakai would be the best....but in the first posts there was mentioned that it could be much more

So you want to have like 20 templates and 20 pages? Why don't you just do it manually then?

it would have limitations - for growing content, too!

regards mr-fan

interesting usecase here...many questions.. ;)

  • Like 1
Link to comment
Share on other sites

@Nico thank you for your code example!
It looks like using hooks is the best solution for processwire.

@LostKobrakai, @diogo, @mr-fan

It depends on how many fields you have and in my opinion it makes more sense to have

a review template for the review page so you pack things together.

If you review not just movies (e.g. books) it even makes more sense.

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