Jump to content

New to Hooks, trying to wrap my head around the syntax


creativejay
 Share

Recommended Posts

Here's the latest version, which still does not effect the change when I save a page.

   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;
            $concatenatedName .= '-' . $p->blog_brand->title;
            $concatenatedName .= '-' . $p->blog_name;
            
            $concatenatedTitle = $p->blog_brand->title;
            $concatenatedTitle .= ' ' . $p->blog_name;
        
            $p->title = $concatenatedTitle;
            $this->sanitizer($concatenatedName, true);
            $this->message("Changed title according to the new module.");
        }
    }

Link to comment
Share on other sites

Also...unless you actually changed the template name from 'blog-post' to 'blog_post'...then this line is also wrong...

if($p->template->name === 'blog_post' && $p->blog_categories->title == 'Swatches'){

And, you could even shorten it to

if($p->template == 'blog-post' && $p->blog_categories->title == 'Swatches'){
Edited by kongondo
Link to comment
Share on other sites

Too funny, ESRCH, yes, I did install it. But it's an even dumber question if you don't ask.

And kongondo, fantastic catch on the blog-post, you're right about that (obviously, or my blog would've broken).  Unfortunately there's another flaw in there somewhere.

I added an echo within the if statement and it's not firing, so I assume the problem is with...


        if($p->template->name === 'blog-post' && $p->blog_categories == 'Swatches'){
Link to comment
Share on other sites

"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.");
        }
    }
}
Link to comment
Share on other sites

Glad it's working, but I would suggest adding the "true" back into the pageName sanitizer. From the docs:

"Sanitizes a value for a URL friendly Page name and cleans out leading or trailing dashes, and converts double dashes to single dashes. Use this if you are passing in a headline to convert to a page name (for example)."

Link to comment
Share on other sites

The following line is wrong

$this->sanitizer($concatenatedName, true);

It should be

$p->name = $this->sanitizer->pageName($concatenatedName);

This is where it came out. I inferred that the "true" was as inappropriate as the rest of my call. I've added it back, and tested it with a title that ends in an exclamation point, and it works. Thanks!

Link to comment
Share on other sites

Naming is also a little bit weird:

You hook after, but you call the method with before in the name.

That was my doing Martijn - since it's hooking into SaveReady, I thought that "Before" was actually appropriate in this case - it's certainly not renaming after save, otherwise we'd have to save again - right?

I inferred that the "true" was as inappropriate as the rest of my call.

I certainly don't see any harm in using the true option and I think it should generally be safer for page names and how you are generating them.

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

×
×
  • Create New...