Jump to content

Relationship module


MuchDev
 Share

Recommended Posts

I am sure that there is probably more preferable methods of implementing this functionality but there is my first try at a module so I figured I would post it for fun. I have added a lot of sub sections to all the items on my site and because of that I want to be able to search for items based on their hierarchy automatically.  If an item is an artwork inside of an artists folder then it is assumed that a user would want to search by the artist to find the item. Because this site has so many darn combo boxes and page fields I figured I would just write a module to push some more knowledge into my brain and in the pursuit add a really handy feature. 

I currently have a maintenance script crawling the site every night but I would like this relationship to update itself any time that it might change. I am thinking I will set this to page save and page move. Here is what I have so far, haven't even tested it yet but I'm happy to hear your thoughts. Looks like I need to do some thinking about how this will work. This will somehow need to trigger the page's cache field to resave...uggh modules are tricky :).

I've done some testing and realized I copied my code out of my script and my variable names were wrong, but this seems to work perfect so far. On to finding how to regenerate that cache field. 

<?php

class ProcessRelateParent extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Relate Parents',
            'version' => 001,
            'summary' => "This will automatically relate an item to it's parent folder. This is used for the parent field that the search engine looks for.",
            'href' => 'http://www.muchdevelopment.com',
            'singular' => true,
            'autoload' => true
            );
    }

    public function init() {
        $this->addHookBefore('Pages::save', $this, 'relateParent');
	    $this->addHookAfter('Pages::sort', $this, 'relateParent');	
		$this->addHookAfter('Pages::moved', $this, 'relateParent');
    }

    public function relateParent($event) {
        $page = $event->arguments[0];
		
		if($page->template == "items"){
	
			if($page->parent->parent->template == "base"){
				$fieldData = $page->parent->title;
			}elseif($page->parent->parent->parent->template == "base")
			{
				$fieldData = $page->parent->title;
			}
						
			$page->parentSection = $fieldData;	
			
			$event->return = $page;
		}
	}	
}

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