Jump to content

monollonom

Members
  • Posts

    358
  • Joined

  • Last visited

  • Days Won

    9

Everything posted by monollonom

  1. It's not exactly the case. {variable} will return anything matching the route and will populate the value $e->variable accordingly, whereas (variable:value) will make sure `variable` is equal to `value` (which can be a regex) before making it accessible as $e->variable. There's an example where there is /route/(variable1|variable2|variable3) but in this case it will be accessible through $e->arguments(1) and will have to match either of the three values. At first this confused me as well but re-reading I think we misunderstood what Ryan meant: he's not talking about the regex part of (variable:regex) but rather making the whole hook a regex, as in: $wire->addHook("#/(event|person)/([[:alnum:]]{15})#", function($e) { $type = $e->arguments(1); $id = $e->arguments(2); return "Looking up data for ID $id of type $type"; }); That's why he mentioned that "...PW converts your match path to a regular expression (if it isn't one already)..."
  2. Your regex looks strangely complicated for what you're trying to achieve. I got it working using: <?php namespace ProcessWire; $wire->addHook('/api/v1/offline-events/(eventId:[[:alnum:]]{15})', function($e) { $e->return = "Hello $e->eventId"; }); As for the 404s, if you're not returning anything it will result in a pageNotFound() call you could hook after to check the url and execute code accordingly: $wire->addHookAfter('ProcessPageView::pageNotFound', function($e) { $url = $e->arguments(1); if(strpos($url, "/api") === 0) { $e->return = "API 404"; } });
  3. It's a very interesting setup @FireWire but I was wondering how you're dealing with API keys when used by modules. Usually, like in your (excellent) Fluency module, API keys are stored in the db through the module configuration, which is convenient since it's local to the module but in your setup ends up scattering around your credentials. It's an open question but should there be a $config object that would contain api keys that modules could check first and if empty rely on the administrator to input these in the module configuration ?
  4. And it seems to work perfectly using $event->cancelHooks! Thanks @Robin S for the pointer and for clarifying about replace, reading again from the doc it does say that it replaces the hookable function but it doesn't mean it will stop the functions hooked after.
  5. Hi @ryan, is it something that is still relevant ? I'm trying to use this for a module I'm writing but it doesn't seem to work as the after hook is still executed, even with `replace` set to true. Here's a boiled down sample of my setup: public function init() { $this->addHookBefore("PageRender::renderPage", $this, "beforeRenderPage"); $this->addHookAfter("PageRender::renderPage", $this, "afterRenderPage"); } protected function beforeRenderPage($event) { $parentEvent = $event->arguments(0); $page = $parentEvent->object; $markup = $this->wire()->cache->getFor($this, "cache-$page->id"); if($markup) { $parentEvent->return = $markup; $event->replace = true; } } protected function afterRenderPage($event) { $parentEvent = $event->arguments(0); $page = $parentEvent->object; $this->wire()->cache->saveFor($this, "cache-$page->id", $parentEvent->return); } Thank you in advance for your help.
  6. I just published another update. Following a pull request / suggestion from a friend I added support for any text and URL field. Multi-languages support is still there and the field will output as many (non-empty) texts as there are languages. I haven't tested with the textarea field but I assume it will generate the QR code from the raw markup, before any textformatter is applied. I believe this won't be a common use-case, but if need be you can always use hooks to adjust or generate your own QR code. I also fixed an issue where the template-contextual settings weren't applied. Please let me know if you run into any issue.
  7. I went ahead and pushed the latest release. I tested uninstalling LanguageSupportPageNames while still having LanguageSupport and it works as expected, outputting only the default language URL. I also namespaced the QR code php generator and it should help solve your issue with TOTP. Let me know if you notice anything else @adrian and otherwise thanks for your feedbacks!
  8. Do you mean like relying on urlSegment instead ? Implementing your feature I actually came across a situation where it would output the same URL twice because the page name of the root page wasn’t defined in the non-default language. So what I did is to add a check to make sure to not output the same QR code twice (plus the default language will just be labeled “URL”). Does it sound like it would help in your situation ? Even if not checking for LanguageSupportPageNames.
  9. Re: this, it seems to be because constants declared using define() do not respect namespace unless explicitely specified. One solution though seems to be to replace define() by const declarations. I'll try to do that tomorrow (also what you suggested is done but I want to also fix this first).
  10. Is this still the case ? I changed the way I import the library by putting it in the __construct rather than outside the class. Also it's weird because in Ryan's case its class is namespaced "Processwire\TfaTotp" so there shouldn't be conflicts ? Or maybe I should use a namespace as well ? Thanks for your suggestion. I don't see any issue to adding this, I'll let you know once it's done. Maybe it's something to keep in mind to allow to say which language to output from the "source" config input, something like "httpUrl.default" (opening the way to other ways to narrow down what to output for files/images but I'm already getting too far ahead...)
  11. Annnnd I found a tiny bug where there was no output if a source was empty... new release published!
  12. I just published a new release of the Fieldtype and updated the original post with a copy of the README. I think it's pretty complete at this point: you can output several QR codes by specifying the sources in the field's config (limited to httpUrl, editUrl and PageFiles/PageImages fields), it supports multi-languages by outputting as many QR codes for httpUrl as there are languages (only those visible to the user when output in the front-end), there is now a static function to generate any QR code you want and there are several functions you can hook at to adjust according to your needs. Let me know if you notice anything strange or a bug!
  13. That's a good point, but I'll let @Robin S decide on that one. Thanks for pointing this out anyway, I wasn't sure but you're right the example might be confusing.
  14. It’s already the case. The reason I set off the output formatting in the example is in the case your image field is set to accepting only one file and thus returning the image or null if empty. Setting output formatting makes sure we get the Pageimages object to call the method from. (but it could be mentioned then I guess)
  15. Hi @Robin S, I made a pull request to add a new method `addFromUrl` to Pagefiles. In a project I'm working on I needed this function from the API. Coming across your module I extracted the bit I was interested in but then I thought it might be a good addition to your module. Hope it helps!
  16. I think there's no way around this issue except for the developers to adapt their code a bit, have their page working without javascript, or a full page reload. I tried to check if there was a way to somehow "reset" the javascript but it seems there's none. What I like about this is how it's somehow reminiscent of front-end editing's method D. Maybe it could be something to rely on for the live preview as well ?
  17. Is it really necessary to introduce a third-party dependency to do a comparison and updates the parts that were updated? Since the whole process won't be instant, couldn't it just be a matter of reloading an iframe showing the page? (which is what you're doing already with ProDrafts I assume) I feel you could (should?) come up with your own minimal implementation of SSE to signal an autosave to a "preview" tab and have the js simply reload the iframe. Reading the Mozilla page I guess you could have two EventSource : one signaling a global autosave for PW's "preview" tab to automatically reload and another one signaling specific changes (in JSON format?) that developers could eventually listen to on their own using htmx or what, which is what you're actually suggesting with ?change=body, my bad. I hope I'm not saying obvious things or misunderstanding what's been said, sorry if I did. A question though re the autosave feature: since this is partly meant as a way to preview changes on a page, what happens if one wants to roll back to the initial state of the page ? Could you provide a way of cancelling all of the auto-saved changes ? Actually now that I think of it and re-reading your post, how would you do if you have a published page that you want to edit but don't want the edits to be seen by a guest ?
  18. Okay so I boiled it down to this: Apparently there's no need to have markupValue() return anything else other than $value as it most likely calls wakeupValue() in any case. Also I went ahead and removed the part hooking into the render() function of the InputfieldMarkup. This removed the double call on the back-end while keeping the front-end call working. Thanks again, I'm pushing the update right now!
  19. Hi @adrian, Glad the module is of help and thank you for the feedback! I applied your suggested changes and at first got an empty output on the front-end. However, taking some cues from @Robin S's FieldtypeRuntimeOnly, I managed to apply the necessary changes for it to work properly. And as a nice side effect the ___getQRText hook now has access to the current $page! Thanks again.
  20. Hi, Thank you for your module. Just a suggestion : because I don't have a specific "description" field for the meta tag, I rely on an "about" field which is a CKEditor. Because of a <a> tag within it, there was an issue rendering one of the pages. So on my local copy I used $sanitizer->truncate() on line 134: $this->description = $this->description ?? $this->sanitizer->truncate($this->page->get($this->description_selector), 160, ['type' => 'sentence']); Maybe this could be a valuable addition ?
  21. Thank you for your detailed answer, I'll have a look and come back with the solution (or questions) I'll come up with !
  22. I just started using it on the client project I mentionned a while ago and it's working amazingly, thank you so much for your hard work ! I have one suggestion / request though: could there be a way to add a string at the end of the translation automatically ? Something like "(translated with DeepL)". Ideally as an option per text field ? I'm seeing from the source code that ___executeData() is hookable, would you suggest to go this way ? Editing the returned json ? Thank you in advance for your feedback / help.
  23. I thought my download link in the modules directory was working but apparently it didn't. Maybe because I forgot to create a release ? Anyway the download link should be working fine now.
  24. I made a small update where you can now output the QR Code on the front-end by calling the field directly (instead of rendering the inputfield) and I added the option to output the image in either .svg or .gif (if .svg, you'll have the additional ability to output the markup directly, instead of a base64).
  25. (once again I was surprised to see a work of mine pop up in the newsletter, this time without even listing the module on PW modules website ?. Thx @teppo !) FieldtypeQRCode Github: https://github.com/eprcstudio/FieldtypeQRCode Modules directory: https://processwire.com/modules/fieldtype-qrcode/ A simple fieldtype generating a QR Code from the public URL of the page, and more. Using the PHP library QR Code Generator by Kazuhiko Arase. Options In the field’s Details tab you can change between .gif or .svg formats. If you select .svg you will have the option to directly output the markup instead of a base64 image. SVG is the default. You can also change what is used to generate the QR code and even have several sources. The accepted sources (separated by a comma) are: httpUrl, editUrl, or the name of any text/URL/file/image field. If LanguageSupport is installed the compatible sources (httpUrl, text field, ...) will return as many QR codes as there are languages. Note however that when outputting on the front-end, only the languages visible to the user will be generated. Formatting Unformatted value When using $page->getUnformatted("qrcode_field") it returns an array with the following structure: [ [ "label" => string, // label used in the admin "qr" => string, // the qrcode image "raw" => string, // the raw qrcode image (in base64, except if svg+markup) "source" => string, // the source, as defined in the configuration "text" => string // and the text used to generate the qrcode ], ... ] Formatted value The formatted value is an <img>/<svg> (or several right next to each other). There is no other markup. Should you need the same markup as in the admin you could use: $field = $fields->get("qrcode_field"); $field->type->markupValue($page, $field, $page->getUnformatted("qrcode_field")); But it’s a bit cumbersome, plus you need to import the FieldtypeQRCode's css/js. Best is to make your own markup using the unformatted value. Static QR code generator You can call FieldtypeQRCode::generateQRCode to generate any QR code you want. Its arguments are: string $text bool $svg Generate the QR code as svg instead of gif ? (default=true) bool $markup If svg, output its markup instead of a base64 ? (default=false) Hooks Please have a look at the source code for more details about the hookable functions. Examples $wire->addHookAfter("FieldtypeQRCode::getQRText", function($event) { $page = $event->arguments("page"); $event->return = $page->title; // or could be: $event->return = "Your custom text"; }) $wire->addHookAfter("FieldtypeQRCode::generateQRCodes", function($event) { $qrcodes = $event->return; // keep everything except the QR codes generated from editUrl foreach($qrcodes as $key => &$qrcode) { if($qrcode["source"] === "editUrl") { unset($qrcodes[$key]); } } unset($qrcode); $event->return = $qrcodes; })
×
×
  • Create New...