[suggestion] News Module (2.9.3) RegisterRoute & prettyurls

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
adriangreen
New Member
New Member
Posts: 7
Joined: Thu Mar 05, 2009 11:43 am

[suggestion] News Module (2.9.3) RegisterRoute & prettyurls

Post by adriangreen »

Hi,
I recently had to "morph" the news module into a slightly different beast by renaming its core terminology. I wanted to have a master detail page heirarchy just like the News module but set it up using terminology related to the type of information entered by the client. (which wasn't news!)

To achieve this was quite simple by creating a /module_custom/News/lang/ folder and overriding the language entries (in my case en_US.php). So I had my renamed module appearing in the admin and all was good and clear. Good.

However, the urls being generated [mod_rewrite is being used] still contained the word "news". This wasn't going to cut it.  Clearly the news module was not inheriting my lovingly applied name changes deeply enough.

So, to get the job done I had to edit some of the module files directly, which I am loathe to do - but I'm not aware of an easy way to override core code otherwise - so now I can't update the module easily (lose my changes in the process).

My issue was this:
the url being generated still contained the term "news" ie:
  site/news/1/2/page-alias.html

To have the module use my new module name I had to update two things: the Registered Routes, which was hard coded with the News module name, and the generated pretty urls, also hardcoded. I updated the code to use the overriden name instead that is in the language file.

The code changes:
disclaimer - php is not my language! Still learning.

File News.Module.php
function SetParameters() around line 68

Code: Select all


/** REMOVED **
$this->RegisterRoute('/[nN]ews\/(?P<articleid>[0-9]+)\/(?P<returnid>[0-9]+)\/(?P<junk>.*?)\/d,(?P<detailtemplate>.*?)$/');
$this->RegisterRoute('/[nN]ews\/(?P<articleid>[0-9]+)\/(?P<returnid>[0-9]+)\/(?P<junk>.*?)$/');
$this->RegisterRoute('/[nN]ews\/(?P<articleid>[0-9]+)\/(?P<returnid>[0-9]+)$/');
$this->RegisterRoute('/[nN]ews\/(?P<articleid>[0-9]+)$/');
*/
	
/**ADDED **/
$modname=strtolower($this->Lang('news')); //the Name of the module, supplied by cms user
/* Create a regex fragment from the name, of the form [nN]ame */
$routename='['.strtolower(substr($modname,0,1)).strtoupper(substr($modname,0,1)).']'.substr($modname,1);
		
$this->RegisterRoute('/'.$routename.'\/(?P<articleid>[0-9]+)\/(?P<returnid>[0-9]+)\/(?P<junk>.*?)\/d,(?P<detailtemplate>.*?)$/');
$this->RegisterRoute('/'.$routename.'\/(?P<articleid>[0-9]+)\/(?P<returnid>[0-9]+)\/(?P<junk>.*?)$/');
$this->RegisterRoute('/'.$routename.'\/(?P<articleid>[0-9]+)\/(?P<returnid>[0-9]+)$/');
$this->RegisterRoute('/'.$routename.'\/(?P<articleid>[0-9]+)$/');

that enables the cms to know where to look.

File News.Module.php
function SearchResult() around line 340

Code: Select all

/** REMOVED **
  $prettyurl = 'news/' . $articleid.'/'.$returnid."/$aliased_title";
*/
/** ADDED ** /
$modname=strtolower($this->Lang('news'));
$prettyurl =  $modname.'/' . $articleid.'/'.$returnid."/$aliased_title";

that get cms to generate url with our potentially custom name too,
but finally we need to edit

File: action.default.php
about line 346

Code: Select all

/**REMOVED**
$prettyurl = 'news/'.$row['news_id'].'/'.($detailpage!=''?$detailpage:$returnid)."/$aliased_title";
*/
/**ADDED**/
$modname=strtolower($this->Lang('news'));
$prettyurl = $modname.'/'.$row['news_id'].'/'.($detailpage!=''?$detailpage:$returnid)."/$aliased_title";

and that is all.

This kind of basic flexibility could *maybe* added to this (or even other) module? In the SVN (hint hint!) :)

Adrian
[ADDENDUM:]
I have discovered a similar modification in the Tips and Tricks part of the forum. My mod could be a core change though as it would work out-of-the-box with the need to only edit the lang file(s).

http://forum.cmsmadesimple.org/index.php/topic,24968.0.html
My way of changing news pretty url's
Last edited by adriangreen on Thu Mar 05, 2009 9:47 pm, edited 1 time in total.
kendo451

Re: [suggestion] News Module (2.9.3) RegisterRoute & prettyurls

Post by kendo451 »

I tried this in News 2.9.2 and it broke the module - even without pretty urls turned on.
adriangreen
New Member
New Member
Posts: 7
Joined: Thu Mar 05, 2009 11:43 am

Re: [suggestion] News Module (2.9.3) RegisterRoute & prettyurls

Post by adriangreen »

Hi,

Sorry to hear that  :-\

Still works great for me. I've done this basic approach for some other modules too. Perhaps I omitted something?

(I've also created a basic re-useable system so that one may have multiple "instances" of a module installed, each with it's own "name", each with their own database table-set. It's not finished (no code to clean up after un-install yet) but it's really not that complicated to do.  Maybe if I ever have the spare time to make it presentable I'll post it somewhere one day...  otherwise - anyone with time and access to the svn could this kind of basic module function) ??? Or not?


Good luck!
Post Reply

Return to “Modules/Add-Ons”