Created
November 20, 2013 18:17
Processwire - example of how to use fields to automatically create page name and title, including date fields and page ID..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BuildUrl extends WireData implements Module { | |
/** | |
* Basic information about module | |
*/ | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'Build URL', | |
'summary' => 'Builds a URL on page save based on specific fields.', | |
'href' => '', | |
'version' => 001, | |
'permanent' => false, | |
'autoload' => true, | |
'singular' => true, | |
); | |
} | |
/** | |
* Populate the default config data | |
* | |
*/ | |
public function __construct() { | |
} | |
/** | |
* Initialize the module and setup hooks | |
*/ | |
public function init() { | |
$this->pages->addHookAfter('saveReady', $this, 'buildUrl'); | |
} | |
/** | |
* | |
* @param HookEvent $event | |
*/ | |
public function buildUrl(HookEvent $event) { | |
// Accesses the $page object | |
$page = $event->arguments[0]; | |
//if(!$page) return; | |
if($page->template != 'session') return; | |
if($page->semester == '') return; | |
// NAME | |
$pageName = ''; | |
// Semester, Class, Instructor | |
$pageName .= $page->semester->name . '-'; | |
$pageName .= $page->class->name . '-'; | |
if(count($page->session_instructors)) { | |
$pageName .= $page->session_instructors->first()->name . '-'; | |
} | |
// Days & Times | |
$dayName = $page->session_day->name; | |
$dateName = ''; | |
$sidName = ''; | |
if($page->sem_start && $page->sem_end) { | |
$dateName = '-' . date("m d y", $page->getUnformatted("sem_start")) . '-' . date("m d y", $page->getUnformatted("sem_end")) . '-'; | |
$sidName = '-id' . $page->id; | |
$dayName = ''; | |
} | |
if($page->date) { | |
$dateName = date("m d y", $page->getUnformatted("date")) . '-'; | |
$sidName = '-id' . $page->id; | |
$dayName = ''; | |
} | |
$pageName .= $dayName . $dateName . $page->time_start . $sidName ; | |
// TITLE | |
$pageTitle = ''; | |
// Semester, Class, Instructor | |
$pageTitle .= $page->semester->name . ' - '; | |
$pageTitle .= $page->class->title . ' - '; | |
if(count($page->session_instructors)) { | |
$pageTitle .= $page->session_instructors->first()->title . ' - '; | |
} | |
// Days & Times | |
$dayTitle = $page->session_day->title . ' '; | |
$dateTitle = ''; | |
$sidTitle = ''; | |
if($page->sem_start && $page->sem_end) { | |
$dateTitle = ' ' . date("m d y", $page->getUnformatted("sem_start")) . '-' . date("m d y", $page->getUnformatted("sem_end")) . ' | '; | |
$sidTitle = ' ID: ' . $page->id; | |
$dayTitle = ''; | |
} | |
if($page->date) { | |
$dateTitle = date("m d y", $page->getUnformatted("date")) . ' | '; | |
$sidTitle = ' ID: ' . $page->id; | |
$dayTitle = ''; | |
} | |
$pageTitle .= $dayTitle . $dateTitle . $page->time_start . $sidTitle ; | |
$page->name = $pageName; | |
$page->title = $pageTitle; | |
$this->message("slug changed to " . $page->name); | |
$this->message("title changed to " . $page->title); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment