Jump to content

Module that creates a page for a user with a specified role.


FireWire
 Share

Recommended Posts

Hi everyone. I just completed first draft work on a module that I wrote. It creates a page for a user with a specified role when a user admin page is saved. I'm sure it could use some more work and would like any feedback on implementation, code quality, and any better utilization of the PW API.

The problem I aimed to solve is that we are using the fantastic MarkupBlog module and wanted a way to easily specify an author for a post when the person who created the page is not the author we want shown on the post (example: one blog author ghostwriting for another blog author). This module allowed me to create a blog-author role and on user admin save create a page under the "Authors" page in the tree. After that each blog post gets a page select field that lists the Authors page children for selection.

More detail:

  • If a user is deleted from the CMS, their page remains in the page tree. This is to prevent problems if there are other pages that depend on that page.
  • A hidden field is added to the User admin page that holds the id for the page created.
  • When creating a page for a user, it checks if there is a page already created with that user's name- if it is, that page is used and the hidden field is updated with that page's ID.
  • If a user's page is deleted from the tree, a new page is created on user admin save and the hidden ID field is updated with the new ID.
  • Module installs and uninstalls properly including adding and removing the hidden ID field from the User system template.
  • The module settings allow for choosing more than one role to create pages.

Again since this is my first rodeo with ProcessWire module development I would love feedback and critique. The code is available here:

https://github.com/skylundy/AutoUserPages

Thanks!
 

 

Screen Shot 2018-03-22 at 2.21.36 PM.png

Edited by usualCommission
Changed module name and updated link to Github
Link to comment
Share on other sites

Thanks for sharing!

5 hours ago, usualCommission said:

The problem I aimed to solve is that we are using the fantastic MarkupBlog module and wanted a way to easily specify an author for a post when the person who created the page is not the author we want shown on the post (example: one blog author ghostwriting for another blog author).

I haven't used that blog module before so maybe there are different requirements there, but I have handled a similar situation like this...

You create a Page Reference field for Author, and as the selectable pages you allow users with a particular role or pages with the "guest_author" template. So "guest_author" is used for any authors who are not users. The selector string for selectable pages looks like this:

 2018-03-23_163018.png.822da61ffeb0058e81a5493fbf78e1b3.png

And if you want to easily add guest authors from the Page Reference field you can use the Page Field Edit Links module.

Link to comment
Share on other sites

That modal module looks great. I'll play around with that.

The method you described was my original approach as well- it would definitely fit within the blog use scenario. After I met with the team we decided that we wanted to have it both ways. More specifically- at my company we will have many people contributing to the blog so they do need access as blog authors to write posts, however there will be other people helping write more than others. There will be times that they will have to post as someone else so going by the creator of the page won't work since that can't be changed unless they are a superuser and that's not possible for our system. Basically, there's a site-manager role for a person who does a lot of copy writing but may need to contribute as someone else.

If a blog author contributes that's great, otherwise if a site-manager posts for them they can override the author since the page creator won't be the author we need.

My module tries to solve this by always having a page under authors like you had described while still allowing the authors to log in and blog as they need.

Link to comment
Share on other sites

34 minutes ago, usualCommission said:

There will be times that they will have to post as someone else so going by the creator of the page won't work since that can't be changed unless they are a superuser and that's not possible for our system.

With what I was proposing you don't use the created_users_id at all - the author is always identified via the Author field. You can make it a required field, and even have it default to the current user with a hook to Pages::added() if you think that would be useful (I can explain that more if needed).

One thing I'm not understanding is why you are creating extra pages for users when a user is a page already.

Regarding the module naming, it's best not to use "Process" in the class name because your module doesn't extend the Process class.

 

  • Like 1
Link to comment
Share on other sites

Creating the extra pages allows for a page select field to be generated so a specific user with a blog-author role can be chosen to override the blog post creator. I wanted this to be more static/persistent than drawing from the list of admin user pages directly in case at some point a person is removed completely from the CMS we won't be trying to reference someone that isn't there anymore and create an issue with blog posts that have been published before that.

A bonus to having a page generated from the user is the ability to have an author page available at say "blah.com/blog/authors/john-example" where I can show a page on the site displaying all the blog posts by John Example (and his ghost writer). Then let's say John Example leaves the company and his user is removed from PW completely, the page structure and all of the page references are intact with no user attached to them. We won't have guest authors per se, just real authors that need to write posts and sometimes be attributed to other posts.

I think I get your solution but I don't know if I'm making the connection well enough to understand if it satisfies my scenario. Absolutely interested in your explanation.

I also need to get more familiar with the selector string for page reference field. It's great!

Link to comment
Share on other sites

I'm not saying I think you definitely shouldn't do it the way you are - it's just that the extra page for users feels a bit redundant to me so my instinct is to try and avoid it where possible.

5 minutes ago, usualCommission said:

Creating the extra pages allows for a page select field to be generated so a specific user with a blog-author role can be chosen to override the blog post creator.

This part will work fine without the extra pages, just directly selecting the user.

6 minutes ago, usualCommission said:

in case at some point a person is removed completely from the CMS we won't be trying to reference someone that isn't there anymore and create an issue with blog posts that have been published before that

This is probably the most persuasive reason for the extra pages, but I think if it were me I would still avoid it and deactivate the ability for old staff members to log in rather than delete them. This can be done by giving them the "login-disabled" role: https://processwire.com/blog/posts/pw-3.0.86/

10 minutes ago, usualCommission said:

A bonus to having a page generated from the user is the ability to have an author page available at say "blah.com/blog/authors/john-example" where I can show a page on the site displaying all the blog posts by John Example (and his ghost writer).

This part I would do with URL segments on the "authors" template, matching the URL segment to a user name.

Link to comment
Share on other sites

After you had started to explain the use of the users directly rather than the pages I began thinking in that direction as far as the URL segments go and I just read about the login-disabled role a couple of days ago but didn't think of that use case until you mentioned it. That very well may have swayed me away from building this module. Unfortunately in my case we haven't updated our core yet and we're still back on a .6 version. We're upgrading to the latest with our next feature push so I'm pretty stoked about that.

Your method is really solid and I am very sure I'm going to run into places I can use that. Building page references with better selectors opens up a ton of possibilities. Thanks for the explanations and help- I really appreciate it!

  • Like 2
Link to comment
Share on other sites

5 hours ago, Robin S said:

haven't used that blog module before so maybe there are different requirements there, but I have handled a similar situation like this..

Just to clarify, there is no such requirement in the blog module.

4 hours ago, Robin S said:

This part I would do with URL segments on the "authors" template, matching the URL segment to a user name.

Yeah. That's how it's done in the the blog demo template files. Note though, that this is also not a requirement.

Edited by kongondo
link to author template file
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...