Jump to content

Recommended Posts

Hello,


I still did not solve my problem about Hebrew letters. In fact, it is ok for Russian for example to have a transliteration of characters (one to one) but in languages like Hebrew, Arabic, it is better to slugify with phonetic like here :

use EasySlugger\Utf8Slugger;

$slug = Utf8Slugger::slugify('日本語');  // slug = ri-ben-yu
$slug = Utf8Slugger::slugify('العَرَبِيةُ‎‎');    // slug = alrbyt
$slug = Utf8Slugger::slugify('עברית');  // slug = bryt

So I am planning to insert https://github.com/javiereguiluz/EasySlugger

Should I create a module or just add a hook ?
 

I am a PW newbie.

Thanks for your help

 
  • Like 3

Share this post


Link to post
Share on other sites
On 7/1/2018 at 9:14 PM, Sten said:

Should I create a module or just add a hook ?

I think you should create a module and share it in the modules directory. 🙂
It sounds like it would be very useful for a number of different languages.

If you only want to set the page name once when the page is first created you would hook after Pages::added() and pass the page title to Utf8Slugger::slugify() to get a name. Or if you want the page name to update whenever the title changes you would hook after Pages::saveReady(). For more advanced usages relating to the latter you might like to take a look at the code in @adrian's Page Rename Options module.

  • Like 1

Share this post


Link to post
Share on other sites

Thank you Robin for your help, I will follow your advice and see the Adrian's code to create this module this month.
 

Share this post


Link to post
Share on other sites

Hello,
I began by creating a simple module to slugify the page name.
 

<?php
include $_SERVER['DOCUMENT_ROOT']."/vendor/javiereguiluz/easyslugger/src/SluggerInterface.php";
include $_SERVER['DOCUMENT_ROOT']."/vendor/javiereguiluz/easyslugger/src/Utf8Slugger.php";
use EasySlugger\Utf8Slugger;
class SlugifyExplicit extends WireData implements Module {
public function init() {
    $this->addHookAfter("Pages::added", $this, "slugifyExplicit");
}
public function ___slugifyExplicit($event) {
$pages = $event->object;
$page = $event->arguments(0);
$page->name= Utf8Slugger::slugify($page->title);
$event->return = $page;
}
}

The name is OK but is is not saved in database. Someone has an idea ?

Share this post


Link to post
Share on other sites

@Sten, your module looks like it is missing the required getModuleInfo() method. Check the Module docs for what is required in a module. The Hello World module is a good one to learn from too. And Tracy Debugger is a must-have for debugging modules or anything else (the Tracy bd() method is all you need to learn to begin with).

Also, when you want to make changes to a Page in a Pages::added hook you need to do a $page->save() in order to save the changes.

Not wanting to hijack your module or anything but when you first posted this topic I was interested in EasySlugger and put together a simple module to test it out. I've added a couple of comments to the code that might help you.

<?php namespace ProcessWire;

class Slugger extends WireData implements Module {

    /**
     * Module information
     */
    public static function getModuleInfo() {
        return array(
            'title' => 'Slugger',
            'version' => '0.1.0',
            'autoload' => true,
        );
    }

    /**
     * Ready
     */
    public function ready() {
        // You would generate the slug/name after saveReady or added, but not both
        $this->addHookAfter('Pages::added', $this, 'afterAdded');
        // $this->addHookAfter('Pages::saveReady', $this, 'afterSaveReady');
    }

    /**
     * After Pages::added
     * Set the page name using EasySlugger only once when the page is first added
     *
     * @param HookEvent $event
     */
    protected function afterAdded(HookEvent $event) {
        require_once __DIR__ . '/EasySlugger/SluggerInterface.php';
        require_once __DIR__ . '/EasySlugger/Utf8Slugger.php';
        $page = $event->arguments(0);
        $page->of(false);
        $page->name = \EasySlugger\Utf8Slugger::slugify($page->title);
        $page->save();
    }

    /**
     * After Pages::saveReady
     * Set the page name using EasySlugger every time the page is saved, in case the Title was changed
     * You could add a condition to set the name only if the Title changed if you like
     *
     * @param HookEvent $event
     */
    protected function afterSaveReady(HookEvent $event) {
        require_once __DIR__ . '/EasySlugger/SluggerInterface.php';
        require_once __DIR__ . '/EasySlugger/Utf8Slugger.php';
        $page = $event->arguments(0);
        $page->name = \EasySlugger\Utf8Slugger::slugify($page->title);
    }

}

Slugger.zip

Note that there is a minor core issue with warning notices from ProcessPageAdd that will hopefully be fixed soon: https://github.com/processwire/processwire-issues/issues/648

  • Like 3

Share this post


Link to post
Share on other sites

@Robin S thank you, I tested your module, it works very well with Hebrew. I only put the Javier Reguiluz' app inside the vendor directory to be able to update it with composer. I'll test it more and tell you.

Quote

Note that there is a minor core issue with warning notices from ProcessPageAdd that will hopefully be fixed soon

Yes I saw it too.

I am grateful for your work.

  • Like 1

Share this post


Link to post
Share on other sites

@Sten, I was thinking that it would be good to have some configurable options in the module to determine when a page's name is set via EasySlugger. That is what the Page Rename Options module provides, so to avoid recreating all of that I've asked in the support thread if @adrian would consider adding a hookable method to his module.

I think it might be better to bundle EasySlugger in with your module because it will have a wider audience that way - a lot of people are on shared hosting that doesn't include Composer. You could use Composer locally to pull in EasySlugger updates (it actually hasn't received any updates in the last 3 years) and then push them out to your module repo.

Share this post


Link to post
Share on other sites

@Robin S

Quote

if @adrian would consider

Yes good idea.

Quote

I think it might be better to bundle EasySlugger in with your module

OK I understand why it is better haver easySlugger inside the module. Besides I have written to Javier Eguiluz to inform him about our work.

Edited by Sten

Share this post


Link to post
Share on other sites

I don't know much about this, but what about doing it via JS instead of PHP so that it works more seamlessly with PW's approach which is to slugify in realtime with JS. It would also mean automatic integration with the PageRenameOptions module.

Here is some stuff on doing this with JS: https://ourcodeworld.com/articles/read/255/creating-url-slugs-properly-in-javascript-including-transliteration-for-utf-8

On a side note - I guess this doesn't work for your needs, but don't forget that you can change the character replacements by editing the settings for the core InputfieldPageName module.

Share this post


Link to post
Share on other sites
17 hours ago, Sten said:

Yes good idea.

It's actually not as practical an idea as I first thought because PageRenameOptions mostly works via Javascript, as @adrian explained in the support thread. But for the purposes of your module you probably don't need all the config options that PageRenameOptions provides. The "initial differences protected" option would be the main one and I think it will be possible to achieve that in PHP with the API. I'll give it a try and post something later.

 

42 minutes ago, adrian said:

I don't know much about this, but what about doing it via JS instead of PHP so that it works more seamlessly with PW's approach which is to slugify in realtime with JS.

I see this the other way around - I think it would actually be better if PW moved to a PHP/API implementation for setting the page name. It's a bit of a problem IMO that there is no API method that provides identical naming to the JS naming in ProcessPageAdd. And I don't think the approach of requiring users of languages other than English to enter potentially thousands of character transliterations into the InputfieldPageName config is ideal.

Although I don't work on non-English sites myself I think it would be good to have something like EasySlugger in the PW core, with the InputfieldPageName config only used for custom overrides. So the user only enters a title in the first step of Page Add and the page name is derived from the title in PHP. The user can easily change to a custom page name after that if need be, so it's not much of a sacrifice in return for a more consistent naming system that requires less configuration.

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By teppo
      This module tracks changes, additions, removals etc. of public (as in "not under admin") pages of your site. Like it's name says, it doesn't attempt to be a version control system or anything like that - just a log of what's happened.
      At the moment it's still a work in progress and will most likely be a victim of many ruthless this-won't-work-let's-try-that-instead cycles, but I believe I've nailed basic functionality well enough to post it here.. so, once again, I'll be happy to hear any comments you folks can provide
      https://modules.processwire.com/modules/process-changelog/
      https://github.com/teppokoivula/ProcessChangelog
      How does it work?
      Exactly like it's (sort of) predecessor, Process Changelog actually consists of two modules: Process Changelog and Process Changelog Hooks. Hooks module exists only to serve main module by hooking into various functions within Pages class, collecting data of performed operations, refining it and keeping up a log of events in it's own custom database table (process_changelog.) Visible part is managed by Process Changelog, which provides users a (relatively) pretty view of the contents of said log table.
      How do you use it?
      When installed this module adds new page called Changelog under Admin > Setup which provides you with a table view of collected data and basic filtering tools See attached screenshots to get a general idea about what that page should look like after a while.
      For detailed installation instructions etc. see README.md.
       


    • By Macaco
      It's a bilingual site. There are two pages: "Artists" and "Events" each with a "Page Reference" field connecting each other.
      - Artists has a field where one can choose events available.
      - Events has a field where you can either choose artists available or create new ones.
      The problems: 
      - When I create an "Artist" page and select events from the list, it doesn't update the collection of participating artists on the "Event" page.
      - When I create an artist from the "Event" page. The field 'artist page > settings > language' is not "Active" for the second language.  When the artist page is created manually,"Active" is on by default.
      I know this all have to do with hooks, but I'm don't fully understand the logics.
    • By Gadgetto
      Status update links (inside this thread) for SnipWire development will be always posted here:
      2019-08-08
      2019-06-15
      2019-06-02
      2019-05-25
      If you are interested, you can test the current state of development:
      https://github.com/gadgetto/SnipWire
      Please note that the software is not yet intended for use in a production system (alpha version).
      If you like, you can also submit feature requests and suggestions for improvement. I also accept pull requests.
      ---- INITIAL POST FROM 2019-05-25 ----
      I wanted to let you know that I am currently working on a new ProcessWire module that fully integrates the Snipcart Shopping Cart System into ProcessWire. (this is a customer project, so I had to postpone the development of my other module GroupMailer).
      The new module SnipWire offers full integration of the Snipcart Shopping Cart System into ProcessWire.
      Here are some highlights:
      simple setup with (optional) pre-installed templates, product fields, sample products (quasi a complete shop system to get started immediately) store dashboard with all data from the snipcart system (no change to the snipcart dashboard itself required) Integrated REST API for controlling and querying snipcart data webhooks to trigger events from Snipcart (new order, new customer, etc.) multi currency support self-defined/configurable tax rates etc. Development is already well advanced and I plan to release the module in the next 2-3 months.
      I'm not sure yet if this will be a "Pro" module or if it will be made available for free.
      I would be grateful for suggestions and hints!
      (please have a look at the screenshots to get an idea what I'm talking about)
       




    • By apeisa
      Update 31.7.2019: AdminBar is now maintained by @teppo. Modules directory entry has been updated, as well as the "grab the code" link below.
      ***
      Latest screencast: http://www.screencas...73-ab3ba1fea30c
      Grab the code: https://github.com/teppokoivula/AdminBar
      ***
      I put this Adminbar thingy (from here: http://processwire.c...topic,50.0.html) to modules section and to it's own topic.
      I recorded quick and messy screencast (really, my first screencast ever) to show what I have made so far. You can see it from here: http://www.screencas...18-1bc0d49841b4
      When the modal goes off, I click on the "dark side". I make it so fast on screencast, so it might seem a little bit confusing. Current way is, that you can edit, go back to see the site (without saving anything), continue editing and save. After that you still have the edit window, but if you click "dark side" after saving, then the whole page will be reloaded and you see new edits live.
      I am not sure if that is best way: there are some strengths in this thinking, but it is probably better that after saving there shouldn't be a possibility to continue editing. It might confuse because then if you make edits, click on dark side -> *page refresh* -> You lose your edits.
      ***
      When I get my "starting module" from Ryan, I will turn this into real module. Now I had to make some little tweaks to ProcessPageEdit.module (to keep modal after form submits). These probably won't hurt anything:
      if($this->redirectUrl) $this->session->redirect($this->redirectUrl); if(!empty($_GET['modal'])) $this->session->redirect("./?id={$this->page->id}&modal=true"); // NEW LINE else $this->session->redirect("./?id={$this->page->id}");   and...
      if(!empty($_GET['modal'])) { $form->attr('action', './?id=' . $this->id . '&modal=true'); } else { $form->attr('action', './?id=' . $this->id); // OLD LINE }  
×
×
  • Create New...