TextformatterMultiValue by rolandtoth

Allows setting key = value pairs in a textarea or text field to use as variable groups in templates.

MultiValue Textformatter for ProcessWire

Set flexible data structures in "key = value1, value2, ..." format to use as variable groups in templates.

Great for general site settings, social links, menus, or just grouping items together without creating separate fields.

Features


  • create groups of variables using only a single textarea/text field
  • easy value retrieval
  • (optional) row headers
  • scalability: add as many rows and items as you need, or change their order
  • fallback to original field value
  • multilanguage ready through FieldtypeTextLanguage and FieldtypeTextareaLanguage field types

Usage


  1. Copy module files to "/site/modules/TextformatterMultiValue/" directory
  2. Go to Modules in the admin, Refresh and Install the module "MultiValue Textformatter"
  3. Create a field of type FieldtypeTextarea or FieldtypeText
  4. Apply "MultiValue Textformatter" textformatter on the field's "Details" tab
  5. Enter rows with keys and items (see syntax below)

Syntax


Rows

Rows consists of a "row key" (optional) and item(s). The key separator is the " = " and the items separator is ":::". Note: from version 1.3.1 the spaces in the key separator are mandatory.

Row key = Some value ::: Another value ::: Yet another value

Row key can be any string, it will be converted to lowercase and sanitized. For example the key "Row key" will be converted to "row_key". This key will be used for retrieval in template files.

No keys mode

Keys can be optionally left out which allows setting items simply by listing them:

https://player.vimeo.com/video/123456
https://player.vimeo.com/video/654321
https://player.vimeo.com/video/321645

Retrieval for the above:

foreach($page->video_links as $video_link) {
    echo '<iframe src="' . $video_link . '"></iframe>';
}

Row header

If the first line begins with the "@" character it indicates it contains row headers.

@ url ::: title ::: target
Facebook = https://facebook.com/mycompany ::: Follow us on Facebook ::: _blank
Linkedin = https://www.linkedin.com/mycompany ::: NULL ::: _blank
Email = #contact ::: Contact

Row headers are optional but thely will help you retrieving items. For example, to get the "title" item, use this:

echo $page->my_field->facebook->title
// result: "Follow us on Facebook"

echo $page->my_field->email->title
// result: "Contact"

If row headers are missing you can get the items using "valueX", where "X" is the index of the item (zero-based!):

echo $page->my_field->facebook->value1
// result: "Follow us on Facebook"

Number and order of items

You can use any number of items in a row, and rows doesn't need to contain the same number of items. However, the order of the item is important, so if an item is not needed, substitute it with the "NULL" placeholder value:

@ url ::: title ::: target
Linkedin = https://www.linkedin.com/mycompany ::: NULL ::: _blank

In the example above "$page->my_field->linkedin->title" will be an empty string.

Misc

  • when no key is set the unmodified field value is returned ($page->field)
  • get the number of rows using "$page->field->count"
  • get the number of items using "$page->field->key->count"
  • check if a key name is available using "$page->field->hasName"

Comments

Use "//" to comment out items.

// Info = this row will be skipped.

Cheat sheet


$page->field->key            // item value
$page->field->key->value     // item value
$page->field->key->value2    // third item value (if no headers set, value0, value1, ...)
$page->field->key->name      // human readable key name
$page->field->key->count     // number of row items
$page->field                 // original field value (no modification)
$page->field->count          // number of field rows
$page->field->hasName        // true if key name is set, false if not
$page->field->original       // original field value (no modification)

License


Licensed under the MIT license.

MultiValue textformatter is provided "as-is" without warranty of any kind, express, implied or otherwise, including without limitation, any warranty of merchantability or fitness for a particular purpose. In no event shall the author of this software be held liable for data loss, damages, loss of profits or any other kind of loss while using or misusing this software.

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Twitter updates

  • ProcessWire 3.0.171 dev branch core updates – More
    22 January 2021
  • Preliminary 2021 roadmap in progress in this week’s update— More
    8 January 2021
  • Happy New Year! Today I’ve bumped the version on the dev branch to 3.0.170, and it’s quite a lot of updates. This post covers most of them. In this post, there’s also a question for you: what would you like to see in ProcessWire in 2021? More
    1 January 2021

Latest news

  • ProcessWire Weekly #350
    In the 350th issue of ProcessWire Weekly we'll check out ProcessWire 3.0.171, highlight some recent forum threads and other ProcessWire resources, and introduce a brand new site of the week. Read on!
    Weekly.pw / 23 January 2021
  • ProcessWire 3.0.170 core updates
    Happy New Year! Today I’ve bumped the version on the dev branch to 3.0.170, and it’s quite a lot of updates. This post covers most of them. In this post, there’s also a question for you: what would you like to see in ProcessWire in 2021?
    Blog / 1 January 2021
  • Subscribe to weekly ProcessWire news

“…building with ProcessWire was a breeze, I really love all the flexibility the system provides. I can’t imagine using any other CMS in the future.” —Thomas Aull