Jump to content

FieldtypeTextWithLength - FieldtypeText extension with .length selector


BitPoet
 Share

Recommended Posts

Sometimes, you want to search a text field by the length of its content. FieldtypeText doesn't provide this functionality (yet). It's possible to add the necessary SQL to a query by hooking into PageFinder::getQuery like @bernhard pointed out here, but that's of course not really straight forward. There's also an open feature request for FieldtypeText in processwire-requests.

FieldtypeTextWithLength is an extension of FieldtypeText, so you can switch your field's type between the two without loosing any information.

After switching your field to "Text with .length Selector", you can search by the (character, not bytes!) length of the content:

$pagesWithShortHeadlines = $pages->find('headline.length<10');

You will also find a new "Length" subfield when using Pages -> Find in the PW backend:

fttwl.thumb.png.bdfa461eedc704cb971ac8eb71e4fb5c.png

  • Like 6
Link to comment
Share on other sites

Thanks for sharing this @BitPoet I read about it in the newsletter. I love that people publish simple useful solutions like this because it is a great learning aid as someone who still considers themself new to ProcessWire.

On looking at your code, I had these observations (which I'm only sharing in case they are interesting!)

  • Wow, how cool that PW lets you invent new 'subfields' as a way to integrate potentially complex SQL. I wonder what I might do with that!
  • Hmmm. Why escape what must only be an integer as a string? I then wondered how MariaDB would handle the comparison since you're requiring an implicit coercion by comparing an int (output of CHAR_LENGTH) to a string. Because '2' > '100' if you're comparing string values, which is not what we want. It seems from my tests that MariaDB picks integer as the shared type for the comparison, which is lucky.
  • I assume the operator does not need validating because we're assuming PW core has already done this and there's no codepath that would allow this function to be called with user input for the operator.
SELECT '2' > '100', CHAR_LENGTH('12') > '100', '100' < CHAR_LENGTH('12');
+-------------+---------------------------+---------------------------+
| '2' > '100' | CHAR_LENGTH('12') > '100' | '100' < CHAR_LENGTH('12') |
+-------------+---------------------------+---------------------------+
|           1 |                         0 |                         0 |
+-------------+---------------------------+---------------------------+

Anyway, thanks for sharing the code, I'm not sure if I'll need a length search any time soon, but it's helpful to have learnt that it's possible and that similar such extensions are possible.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...