Jump to content

eelkenet

Members
  • Posts

    115
  • Joined

  • Last visited

  • Days Won

    3

eelkenet last won the day on October 16 2019

eelkenet had the most liked content!

Profile Information

  • Gender
    Not Telling
  • Location
    Amsterdam

Recent Profile Visitors

3,133 profile views

eelkenet's Achievements

Sr. Member

Sr. Member (5/6)

123

Reputation

1

Community Answers

  1. Thanks @Robin S! That seems like a smart solution. I didn't know about renderReadyHook before.
  2. Update: I have, in a way, accomplished my goals! I have not been successful at adding any <optgroup>s, BUT I did figure out a way around my issue. Instead of adding the unsupported optgroups, I just inject extra, disabled <option>s. These options can get a label, so I can use them as sort of dividers between the rows of pages Currently I group them by template name, as the selector for the field has that as primary sorting anyway. Pretty simple.. as always with these things, once you figure it out <?php $wire->addHookAfter("InputfieldAsmSelect(name=linked)::render", function ($event) { $html = $event->return; // Get the input field in order to sanitize new values /** @var Inputfield $input */ $input = $event->object; // Create a DOMDocument object and load the html from the Inputfield $dom = new \DOMDocument; $dom->loadHTML($html); // Get the <select> and its childnodes $select = $dom->getElementsByTagName("select")->item(0); $options = $select->childNodes; // Keep track of the previous template name.. $prevTemplate = ''; //Loop through the childnodes for ($i = 0; $i < $options->length - 1; $i++) { $option = $options->item($i); if ($option && $option->attributes->getNamedItem('value')) { // Get the page onject $pageId = (int) $option->attributes->getNamedItem('value')->nodeValue; $page = $this->pages->get($pageId); // If a new template label is found.. if ($prevTemplate !== $page->template->label) { // Create a new <option> $newOption = $dom->createElement("option"); $newOption->setAttribute("disabled", true); $newOption->nodeValue = $input->entityEncode(mb_strtoupper($page->template->label)); // And simply insert it above the current <option> $select->insertBefore($newOption, $option); $prevTemplate = $page->template->label; } } } // Return the new HTML and add a paragraph to double prove this bit of code do its work $event->return = $dom->saveHTML(); }); I'd love to be able to add things like icons, and some more formatting.. but for now: pretty decent.
  3. Thanks for your replies! After @bernhard's suggestion I tried returning a completely custom bit of <select><optgroup><option ...></option><optgroup></select>.. but I would not render at all. So after all of this, it seems that asmSelect completely ignores the returned <select> from the function and replaces it with its own version. So there I bumped into what @Robin S wrote: So yeah.. clearly my idea will never work, unless Ryan decides to suddenly merge a request from 7 years ago on a project which hasn't been updated in 10 years. For now I've decided to leave it, and use InputfieldTextTags instead, which seems a bit more friendly visually. No optgroups there either, and no idea on how I would implement that either. Bummer!
  4. I have a very long list of pages inside a InputfieldSelectMultiple (InputfieldAsmSelect) dropdown, and I want to group the pages inside <optgroup>s, according to their template and some other features. But I can't get it to work. First of all, I think I can't get this done by the available options inside the Inputfield configuration (would love to be wrong here!) so I have taken the route of hooking into the Inputfield's render option. There, I would need to loop through all the <option>s, wrap these in their relevant <optgroup>s and return the result. 1. So, I have done some testing, and made a proof of concept for myself on how to edit incoming HTML with a <select> in order to group its options inside <optgroup> tags. This stand-alone bit of code works perfectly fine: <?php // Create a snippet of HTML $html = "<p> <select> <option value='1'>Option #1</option> <option value='2'>Option #2</option> <option value='3'>Option #3</option> <option value='4'>Option #4</option> </select> </p>"; // Create a DOMDocument object, and load the snippet $dom = new \DOMDocument; $dom->loadHTML($html); // Select the first <select> element from the HTML $select = $dom->getElementsByTagName("select")->item(0); // Create two option groups $optgroupEven = $dom->createElement("optgroup"); $optgroupEven->setAttribute("label", "This is the even group"); $optgroupOdd = $dom->createElement("optgroup"); $optgroupOdd->setAttribute("label", "This is the odd group"); // List through all the childnodes of the select $options = $select->childNodes; for ($i = $options->length - 1; $i >= 0; $i--) { // Get the current <option> inside the <select> $option = $options->item($i); // Some validation if ($option && $option->attributes->getNamedItem('value')) { // Remove from <select> $select->removeChild($option); // Prepend to odd or even optgroup // (Prepend because we list through the children in reverse) if ((int) $option->attributes->getNamedItem("value")->nodeValue % 2 === 0) { $optgroupEven->prepend($option); } else { $optgroupOdd->prepend($option); } } } // Add the groups to the <select> $select->appendChild($optgroupOdd); $select->appendChild($optgroupEven); // Add another option to the <select> $opt = $dom->createElement("option"); $opt->nodeValue = "Option B"; $select->appendChild($opt); echo "<h2>Original:</h2>"; echo $html; echo "<h2>Grouped:</h2>"; echo $dom->saveHTML(); This results in what I would expect: Which proves the idea. 2. I also know I can edit the output inside the page editor by hooking into the Inputfield's render function: <?php $wire->addHookAfter("InputfieldAsmSelect(name=linked)::render", function ($event) { $html = $event->return; $event->return = $html . "<p>This gets added!</p>"; }); 3. Trying to combine the two in a simple way without using <optgroup> also works. With the following bit of code I can easily add a new option to the Inputfield's <select>: <?php namespace ProcessWire; $wire->addHookAfter("InputfieldAsmSelect(name=linked)::render", function ($event) { $html = $event->return; // Get the input field in order to sanitize new values /** @var Inputfield $input */ $input = $event->object; // Create a DOMDocument object and load the html from the Inputfield $dom = new \DOMDocument; $dom->loadHTML($html); // Get the <select> $select = $dom->getElementsByTagName("select")->item(0); // Create a new <option>, set label and value $opt = $dom->createElement("option"); $opt->nodeValue = $input->entityEncode("This is a test, let's add this `option` to the `select`"); $opt->setAttribute("value", 1); // Append to the <select> $select->appendChild($opt); // Return the new HTML and add a paragraph to double prove this bit of code do its work $event->return = $dom->saveHTML() . "<p>This gets added!</p>"; }); This yields the full <select> with my new <option> as the last element: 4. Now here is my issue: I can't get <optgroup>'s to show up in the select. When I adapt my code above just a little bit, to incorperate <optgroup>, it just does not show up. The final paragraph does till get added though! <?php namespace ProcessWire; $wire->addHookAfter("InputfieldAsmSelect(name=linked)::render", function ($event) { $html = $event->return; // Get the input field in order to sanitize new values /** @var Inputfield $input */ $input = $event->object; // Create a DOMDocument object and load the html from the Inputfield $dom = new \DOMDocument; $dom->loadHTML($html); // Get the <select> $select = $dom->getElementsByTagName("select")->item(0); // Create a new <option>, set label and value $opt = $dom->createElement("option"); $opt->nodeValue = $input->entityEncode("This is a test, let's add this `option` to the `select`"); $opt->setAttribute("value", 1); // Create an <optgroup> $optgroup = $dom->createElement("optgroup"); $optgroup->setAttribute("label", "This is the optgroup"); // Add the option to the optgroup $optgroup->prepend($opt); // Add the optgroup to the select $select->appendChild($optgroup); // Return the new HTML and add a paragraph to double prove this bit of code do its work $event->return = $dom->saveHTML() . "<p>This gets added!</p>"; }); A final screenshot would be useless, the whole optgroup and the option itself are both invisible now. Does anybody have an idea on how to get this to work? (Please note: I don't want to render a flat list with labels like {template} - {title}, that works technically but is problematic from a UX point of view).
  5. After enabling WebP support you may notice it can take a long time for ProcessWire to create WebP copies of all images and their variations. For instance, on a site I work on (with over 10k images), it was taking about 1 second per image, ie. more than 3 hours in total.. ? If you are comfortable around the command-line, you can use the cwebp program and this bash script to speed things up drastically. I have built upon this script and got things to work in combination with xargs and find, making it rather powerful for PW's purposes. 1. Save this script as 'convert-webp.sh' (or download the Gist from Github), and follow instructions ######################################################################################################### # # Fast Recursive Images to WebP converter # Customized for ProcessWire CMS/CMF <https://www.processwire.com> # # Author: Eelke Feenstra <dev@eelke.net> # Version: 001 # Based upon: https://github.com/onadrog/bash-webp-converter # # Quick & dirty script to add webp versions to all PNG / JPG / JPEG files inside your PW assets folder # # 1. Set this script to executable: # $ chmod +x convert-webp.sh # # 2. Check if cwebp is installed: # $ cwebp -version # If it is not, install: # $ brew install webp # Or follow instructions https://developers.google.com/speed/webp/download # and change $executable to cwebp's full path # # 3. Run the script directly on a folder: # $ ./convert-webp.sh /path/to/your/folder # ######################################################################################################### # Configuration executable="cwebp" # update this to reflect your installation! quality=90 # change to desired WebP quality ######################################################################################################### converted=0 skipped=0 echo "Entering $1" for file in $1/* do name="${file%.*}" echo "FILE: $file" echo "NAME: $name" # Skip the folder itself.. if [ "$name" = "./." ]; then echo "SKIP: $name" continue; fi if [[ $(file --mime-type -b $name.webp) == image/webp ]]; then echo "FOUND: $name.webp, skipping.." skipped=$((skipped+1)) elif [[ $(file --mime-type -b $file) == image/*g ]]; then echo "NOT FOUND: $name.webp" newfile(){ echo "$file" | sed -r 's/(\.[a-z0-9]*$)/.webp/' } $executable -q $quality "$file" -short -o "$(newfile)" converted=$((converted+1)) fi done echo "Converted $converted, Skipped $skipped" 2. Run to create webp versions of all jpg/jpeg/png images in a given folder (for example the site's homepage) $ ./convert-webp.sh /path/to/processwire/site/assets/files/1 3. And in order to create WebP copies of ALL images inside the /site/assets/files folder, you can run this script. It searches for all directories inside the files directory, and passes these to the convert-webp.sh script using xargs. $ find /path/to/processwire/site/assets/files -maxdepth 2 -type d | xargs -I '{}' ./convert-webp.sh '{}' Tested both on MacOS 12 and Debian
  6. Update: The solution is to upgrade (paid) to MAMP PRO 6, this version comes with WebP support. The upgrade costs a lot less than all the time I've spent chasing this bug.. ? Edit: it took really long to convert all images, and I figured out a faster way using the command line. Read more in the tutorial.
  7. @horst Oh my, I'm sorry for never replying to you! I think I have finally found the origin of my and @AndZyk's situation: MAMP PRO on MacOS (5.7) simply does not have WebP support enabled. I still don't know why this creates all the new copies, because it seems that all moving parts seem to be aware that webp is in fact not enabled. With this basic check: <?php namespace ProcessWire; include "index.php"; error_reporting(E_ALL); ?> <pre> <h3>Check directly using gd_info()</h3> <?php $gd_info = gd_info(); $webpSupport = isset($gd_info['WebP Support']) ? $gd_info['WebP Support'] : false; print_r($gd_info); echo $webpSupport ? "\nWebP is supported" : "\nWebP is NOT supported"; ?> <hr> <h3>ImageSizerEngineIMagick</h3> <?php $imagick = $modules->get("ImageSizerEngineIMagick"); $supported = $imagick->supported("webp"); echo $supported ? "WebP is supported" : "WebP is NOT supported"; echo "\nFormats: " . print_r($imagick->getSupportedFormats(), true); ?> <hr> <h3>ImageSizerEngineGD</h3> <?php $gd = new ImageSizerEngineGD(); $supported = $gd->supported("webp"); echo ($supported ? "WebP is supported" : "WebP is NOT supported"); echo "\nFormats: " . print_r($gd->getSupportedFormats(), true); ?> </pre> I get these results on my MAMP PRO machine: Check directly using gd_info() Array ( [GD Version] => bundled (2.1.0 compatible) [FreeType Support] => 1 [FreeType Linkage] => with freetype [GIF Read Support] => 1 [GIF Create Support] => 1 [JPEG Support] => 1 [PNG Support] => 1 [WBMP Support] => 1 [XPM Support] => [XBM Support] => 1 [WebP Support] => [BMP Support] => 1 [TGA Read Support] => 1 [JIS-mapped Japanese Font Support] => ) WebP is NOT supported ImageSizerEngineIMagick WebP is NOT supported Formats: Array ( [source] => Array ( [0] => JPG [1] => JPEG [2] => PNG24 [3] => PNG [4] => GIF [5] => GIF87 ) [target] => Array ( [0] => JPG [1] => JPEG [2] => PNG24 [3] => PNG [4] => GIF [5] => GIF87 ) ) ImageSizerEngineGD WebP is NOT supported Formats: Array ( [source] => Array ( [0] => JPG [1] => JPEG [2] => PNG [3] => GIF ) [target] => Array ( [0] => JPG [1] => JPEG [2] => PNG [3] => GIF ) ) And these on the online host: Check directly using gd_info() Array ( [GD Version] => 2.2.5 [FreeType Support] => 1 [FreeType Linkage] => with freetype [GIF Read Support] => 1 [GIF Create Support] => 1 [JPEG Support] => 1 [PNG Support] => 1 [WBMP Support] => 1 [XPM Support] => 1 [XBM Support] => 1 [WebP Support] => 1 [BMP Support] => 1 [TGA Read Support] => 1 [JIS-mapped Japanese Font Support] => ) WebP is supported ImageSizerEngineIMagick WebP is supported Formats: Array ( [source] => Array ( [0] => JPG [1] => JPEG [2] => PNG24 [3] => PNG [4] => GIF [5] => GIF87 ) [target] => Array ( [0] => JPG [1] => JPEG [2] => PNG24 [3] => PNG [4] => GIF [5] => GIF87 [6] => WEBP ) ) ImageSizerEngineGD WebP is supported Formats: Array ( [source] => Array ( [0] => JPG [1] => JPEG [2] => PNG [3] => GIF ) [target] => Array ( [0] => JPG [1] => JPEG [2] => PNG [3] => GIF [4] => WEBP ) ) I have reached out to the MAMP folks for info on how I could go about enabling it here. The only related issues on StackOverflow etc are pointing in the wrong direction it seems (homebrew etc).
  8. Hi @Pete, could the minimum character count for the search on the forum please be reduced to 3 instead of (what I suspect) 4? It's really impractical to not be able to search for words like 'api', or 'get', while you know for sure that there must be many results for those words.
  9. Note: if you want to force a download but don't want to change the downloaded filename, you should omit the value of the download attribute like so: <a href="yourfile.pdf" download>link</a>
  10. Hi @joshua, that works - sort of. When setting the user language inside a template we still only get the default language, as your hook gets called earlier. However, what does work is hooking into the same Page::render method with a lower priority number, so our code gets an earlier execution. Great! In ready.php we now have: <?php namespace ProcessWire; // Hook into Page::render, just before PrivacyWire's hook // Which uses the default priority of 100 wire()->addHookBefore('Page::render', function (HookEvent $evt) { // Only apply to front-end pages $page = $evt->object; if ( $page->template == 'admin' || // exclude admin pages $page->template == 'form-builder' // exclude from form-builder iframe ) { return; } $user = wire()->user; $languages = wire()->languages; // Get and sanitize the ?lang= get param $lang = strtolower(wire()->input->get("lang", "text")); // Set appropriate language if ($lang === "en") { $user->language = $languages->get("default"); } else { $user->language = $languages->get("de"); } }, ['priority' => 99]);
  11. Hi, I've got a site that uses multi-language URLs based on a query-param, ie. https://example.com/some/page?lang=de. The site exists of a Vue.js app on top of a very basic HTML skeleton generated by PW, and we don't use the LanguageSupportPageNames module at all (so no example.com/de & example.com/en ). In order to set the initial skeleton HTML (such as page title etc), which gets rendered before the Vue app has booted, I set my $user->language based on the $input->get('lang') value. After the Vue app has booted, it takes over control and loads the remaining data in the appropriate language through a multilingual API endpoint. Next to the Vue app, we also use PrivacyWire. However, due to the way PrivacyWire has been built, it looks at the $users language BEFORE any of this takes place (in its ready() function). As a result, I can't get the correct language content inside the PrivacyWire popups, it always shows the default language content instead. So probably I need to hook into the getPrivacyWireConfigObject method, and overrule the PrivacyWire::lang variable with the language that I get from my $input->get('lang')? Or would there be another, easier approach? Any pointers would be most welcome! Thanks.
  12. Today I again bumped into an issue I had last year with creating WebP images: Enabling WebP on another project by setting imageSizerOptions' webpAdd to true immediately resulted in the same behaviour as I had last year: the server simply keeps on creating new variations of images, while they already exist. WebP images are created correctly by the way. Seems to be a bug, can anybody else confirm? I am using PW Master (3.0.165) on MAMP Pro (MacOS 11.2, PHP 7.4.2)
  13. Great addition @ryan! Quick question: will ProCache be able to cache (the result from) these URL hooks?
  14. Hi @Sebi, I ran into a bug: Currently the Router::handleError() function incorrectly reports HTTP 500 errors that result from methods that should have remained silent, because they are called with the error control operator '@'. As the PHP documentation on error control operators puts it: I bumped into this while working on a project with some semi-broken images, where the APP API module kept throwing 500's from PW's ImageInspector class. Exif issues can be rather difficult to handle sometimes.. The solution is simple: just add a check error_reporting() before the handleError method, for instance public static function handleError($errNo, $errStr, $errFile, $errLine) { if (error_reporting()) { $return = new \StdClass(); $return->error = 'Internal Server Error'; $return->devmessage = [ 'message' => $errStr, 'location' => $errFile, 'line' => $errLine ]; self::displayOrLogError($return, 500); } }
  15. Something I'm missing in the new Modules page are the requirements for installing, such as PW version and other required modules.
×
×
  • Create New...