hollyvalero Posted July 19, 2018 Share Posted July 19, 2018 Hi! I've got 3 page reference fields (multipage) used to tag database entries by audience, subject area, and resource type. These can be created on the fly, so you can ideally curate a special collection and have processwire generate pages, sorted displays, etc. Using Mixitup to create a nice sift and sort with multiple drop down menus. Mixitup doesn't like spaces, dashes, special characters - just letters. Rather than tell everyone, "don't use special characters!" I'd like to assume the worst and strip them out behind the scenes. The option value and card class are both invisible, so as long as it's just letters it should work whether it's upper or lower case. For the drop down menu I need to generate something like this: <option value='.ResumesJobs'>Résumés & Jobs</option> And then the actual card display below will contain a matching ID: <div class="mix ResumesJobs"> ---etc. </div> I've got the basics working... I can remove extra spaces. I can get rid of special characters. But I keep getting dashes and amp; in the displays. Tried the sanitizer->pageName which didn't get rid of the amp. Sample of my sad code below... gather all pages listed under subjects... create a variable $submix for the title... then try to strip out spaces and characters: foreach($subjects as $subject) { $submix = $subject->title; $submix = str_replace(' ', '', $submix); preg_replace('/[^A-Za-z0-9\-]/', '', $submix); echo "<option value='.$submix'>{$subject->title}</option>" ; } Is there a better single process? Sanitizer seems like overkill since this in internal stuff, not user input. Tried to html encode & back to & and then strip spaces, characters, etc. but bogging down in errors and unable to get rid of dashes. Would appreciate any insights. Link to comment Share on other sites More sharing options...
adrian Posted July 19, 2018 Share Posted July 19, 2018 I was all set to suggest that you do this: but apparently the Sanitizer::translate option forces lowercase (https://github.com/processwire/processwire-issues/issues/643). Here's another interesting thing I just found: https://github.com/processwire/processwire-issues/issues/644 which doesn't seem expected to me. If Ryan agrees that the first one is a bug, then the alpha sanitizer with translate should result in this: 3 Link to comment Share on other sites More sharing options...
hollyvalero Posted July 19, 2018 Author Share Posted July 19, 2018 The sample page name is my working "worst case scenario" but there's no way of guessing what page titles will be. I've got multiple versions of this foreach loop... $submix = $subject->title; (maps & geolocation, math, STEM, and a zillion others) $resmix = $resourcetype->title; (videos, games, magazines, etc.) $audmix = $audience->title; (kids, taxpayers, college bound, whatever) Assuming I might have to convert each title variable back to HTML, then remove "&" or all special characters... Then spaces... and dashes I think caps and lowercase will work fine, however. Link to comment Share on other sites More sharing options...
adrian Posted July 19, 2018 Share Posted July 19, 2018 If you don't care about keeping the uppercase, then why not use the alpha with translate option that I posted above? Doesn't that take care of everything you need? Link to comment Share on other sites More sharing options...
hollyvalero Posted July 19, 2018 Author Share Posted July 19, 2018 1 minute ago, adrian said: If you don't care about keeping the uppercase, then why not use the alpha with translate option that I posted above? Doesn't that take care of everything you need? It will translate to lowercase, but if someone puts in "Frick & Frack" ... what happens to the & and the space? Link to comment Share on other sites More sharing options...
adrian Posted July 19, 2018 Share Posted July 19, 2018 1 minute ago, hollyvalero said: It will translate to lowercase, but if someone puts in "Frick & Frack" ... what happens to the & and the space? They will be removed as you'll see in my example - isn't that what you want? Link to comment Share on other sites More sharing options...
hollyvalero Posted July 19, 2018 Author Share Posted July 19, 2018 Just now, adrian said: They will be removed as you'll see in my example - isn't that what you want? It is... just don't know how to code that... I haven't tried this... but something like this? foreach($subjects as $subject) { $submix = $subject->title; $submix = ($sanitizer->alpha('$submix', Sanitizer::translate) ) ; echo "<option value='.$submix'>{$subject->title}</option>" ; } Link to comment Share on other sites More sharing options...
adrian Posted July 19, 2018 Share Posted July 19, 2018 foreach($subjects as $subject) { echo "<option value='".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>"; } Check out the inspection of the select options and you can see it's working as expected. 2 Link to comment Share on other sites More sharing options...
hollyvalero Posted July 19, 2018 Author Share Posted July 19, 2018 ? looks so much better than my code. Thank you! 2 Link to comment Share on other sites More sharing options...
hollyvalero Posted July 23, 2018 Author Share Posted July 23, 2018 On 7/19/2018 at 4:43 PM, adrian said: foreach($subjects as $subject) { echo "<option value='".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>"; } Check out the inspection of the select options and you can see it's working as expected. So this code: $subjects = $pages->find("template=subject, limit=100, sort=title"); foreach($subjects as $subject) { echo "<option value='".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>"; } Does product the single name dropdown, lower case without the & or spaces. <option value='artandculture'>Art and Culture</option><option value='mapsampgeo'>Maps & Geo</option> <option value='Science'>Science</option> I thought it was working, but realized I need to include a leading PERIOD in the option value before the name in order for mixitup to work... :<option value='.mapsampgeo'>Maps & Geo</option> <option value='.artandculture'>Art and Culture</option> Is there a way using this sanitize string to add the "." back in or do I need to go back to 2-3 steps to launder stuff out? Link to comment Share on other sites More sharing options...
adrian Posted July 23, 2018 Share Posted July 23, 2018 3 minutes ago, hollyvalero said: Is there a way using this sanitize string to add the "." back in or do I need to go back to 2-3 steps to launder stuff out? Just add the period in: echo "<option value='.".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>"; This will result in: <option value='.mapsampgeo'>Maps & Geo</option> Is this what you are looking for? Link to comment Share on other sites More sharing options...
hollyvalero Posted July 23, 2018 Author Share Posted July 23, 2018 1 minute ago, adrian said: Just add the period in: echo "<option value='.".$sanitizer->alpha($subject->title, Sanitizer::translate)."'>{$subject->title}</option>"; This will result in: <option value='.mapsampgeo'>Maps & Geo</option> Is this what you are looking for? THAT'S IT! You're a genius!! Thank you!! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now