Jump to content

creativejay

Members
  • Posts

    246
  • Joined

  • Last visited

Community Answers

  1. creativejay's post in New to Hooks, trying to wrap my head around the syntax was marked as the answer   
    "Swatches" is the correct page title that's associated with that field.
    I swear I had $p->blog_categories->title == 'Swatches' a couple revisions ago (then I changed it to $p->blog_categories == '1040' and must have changed it back without adding the fieldname back).
    Anyhow, it's working now! Thank you EVERYONE! I would be proud, but you guys seriously did most of the work. I really appreciate it!
    Below is the final content of the module file (which is installed ). I'm posting the entire thing in case someone else is in need of help with the entire context situation. I'll mark this one as solved though really it's the culmination of the entire thread. Thanks, everyone, for bearing with me.
    <?php /** * ProcessWire 'Rename Swatches' module by forum user creativejay * * Checks to see if a page uses the template "blog-post" and has a $p->blog_categories->title value of "Swatches" then redefines the value of $p->title and $p->name * * * ProcessWire 2.x * Copyright (C) 2014 by Ryan Cramer * Licensed under GNU/GPL v2, see LICENSE.TXT * * http://processwire.com * */ class RenameSwatches extends WireData implements Module { /** * getModuleInfo is a module required by all modules to tell ProcessWire about them * * @return array * */ public static function getModuleInfo() { return array( // The module's title, typically a little more descriptive than the class name 'title' => 'Rename Swatches', // version number 'version' => 4, // summary is brief description of what this module is 'summary' => 'Checks to see if a page uses the template blog-post and has a blog_categories->title value of Swatches then redefines the value of title and name', // Optional URL to more information about the module 'href' => 'https://processwire.com/talk/topic/8863-new-to-hooks-trying-to-wrap-my-head-around-the-syntax/', // singular=true: indicates that only one instance of the module is allowed. // This is usually what you want for modules that attach hooks. 'singular' => true, // autoload=true:indicates the module should be started with ProcessWire. // This is necessary for any modules that attach runtime hooks, otherwise those // hooks won't get attached unless some other code calls the module on it's own. // Note that autoload modules are almost always also 'singular' (seen above). 'autoload' => true, // Optional font-awesome icon name, minus the 'fa-' part 'icon' => 'eraser', ); } public function init() { $this->addHookAfter('Pages::saveReady', $this, 'renameBeforeSave'); } public function renameBeforeSave(HookEvent $event) { $p = $event->arguments[0]; if($p->template->name === "blog-post" && $p->blog_categories->title == 'Swatches'){ $concatenatedName = $p->createdUser->name; $concatenatedName .= '-' . $p->blog_brand->title; $concatenatedName .= '-' . $p->blog_name; $concatenatedTitle = $p->blog_brand->title; $concatenatedTitle .= ' ' . $p->blog_name; $p->title = $concatenatedTitle; $p->name = $this->sanitizer->pageName($concatenatedName, true); $this->message("Changed title according to the new module."); } } }
×
×
  • Create New...