Jump to content

Recommended Posts

Posted

FieldtypeFlex

Structured content in one ProcessWire field, edited with native Inputfields.


Hi everyone,

This is the field I’ve wanted in ProcessWire for years. I first shared an early version here, and I’m proud of what it’s become. I wanted to give editors a page that makes sense to them without having to create a new ProcessWire field for every little property or a separate page for every content block.

For me, 1.0 is feature-complete: it covers what I set out to build, and I’m already using it in production. I plan to maintain it for the long term. That doesn’t mean the feature list is closed. If there’s something you’d like to build with Flex and can’t, I’d love to hear what’s missing.

One field, familiar inputs

You define the structure in code. Editors work in the regular ProcessWire page editor. Keep definitions in Git alongside the templates that render them, and use inputs like Date, AsmSelect, Checkboxes, TinyMCE, Page references, multilingual text, and native File and Image editors.

  • Objects are fixed groups for things like a contact card or display settings.
  • Rows are repeatable blocks. Different block types can sit next to each other, and a block can contain its own Rows.
  • Tables give each row the same columns. Simple values can be edited in place, while richer inputs open below the row.

01 / A page with text and gallery blocks

Create a field called content of type Flex, add it to your template, and save the following as site/templates/flex-fields/content.php. Enable InputfieldTinyMCE for this example.

<?php namespace ProcessWire;

return [
    'blocks' => [
        'type' => 'rows',
        'label' => 'Content blocks',
        'types' => [
            'text' => [
                'label' => 'Text',
                'rowTitle' => '{title|"Untitled text"}',
                'children' => [
                    'title' => ['type' => 'text', 'label' => 'Section title'],
                    'body' => ['type' => 'TinyMCE', 'label' => 'Body'],
                ],
            ],
            'gallery' => [
                'label' => 'Gallery',
                'children' => [
                    'images' => ['type' => 'image', 'label' => 'Images', 'maxFiles' => 6],
                ],
            ],
        ],
    ],
];

In Page Edit, choose Text or Gallery each time you add a block. You can mix them, drag them into order, hide a block, or copy it to a compatible Flex collection.

On the frontend, your templates stay in charge. For example, render rich text or use native image resizing:

foreach($page->content->blocks as $block) {
    if($block->rowType() === 'text') {
        echo '<h2>' . $sanitizer->entities($block->title) . '</h2>';
        echo $block->body;
    } elseif($block->rowType() === 'gallery') {
        foreach($block->images as $image) {
            $thumb = $image->size(800, 600);
            $alt = $sanitizer->entities($image->description);
            echo "<img src='{$thumb->url}' alt='$alt'>";
        }
    }
}

$matches = $pages->find('content.blocks.text.title*=Architecture');

02 / An editable service price list

For a table, each row has the same columns. Here’s the complete definition for a second Flex field called services, saved as site/templates/flex-fields/services.php.

<?php namespace ProcessWire;

return [
    'prices' => [
        'type' => 'table',
        'label' => 'Service price list',
        'editableHeaders' => true,
        'columns' => [
            'service' => ['type' => 'text', 'label' => 'Service'],
            'price' => ['type' => 'float', 'label' => 'Price (€)'],
            'details' => ['type' => 'TinyMCE', 'label' => 'Details'],
        ],
    ],
];

Names and prices are edited right in the cells. Click Details to open TinyMCE below the row. Images and files can use a detail editor too. Leave out editableHeaders if you want to keep the column headings fixed in code.

Watch it in Page Edit

MIT licensed. Requires ProcessWire 3.0.259+, PHP 8.1+, and MySQL 8.0+ or MariaDB 10.11+ with InnoDB. The official GitHub README is the best place to check for current setup instructions and examples.

 

Edit: I’ve updated this first post for 1.0.0. The original 0.4.0 preview announcement is preserved below for context.

Spoiler

Hi everyone,

I'd like to introduce FieldtypeFlex, a new ProcessWire fieldtype I've been working on. It's now available as a 0.4.0 pre-release, and I'd be interested in feedback from the community.

The idea is to define and manage structured content within a single ProcessWire field, without having to create separate ProcessWire fields for every property.

FieldtypeFlex uses native ProcessWire Inputfields for editing, so things like TinyMCE, images, Page references, multilingual inputs and conditional fields can be used within the same structure.

Main features

  • Code-defined structures: Define properties using PHP, JSON or native Inputfield objects.
  • Objects: Group related properties into fixed, nested structures.
  • Repeatable Rows: Create typed content blocks that editors can add, duplicate, reorder and hide. Rows can also be nested.
  • Native Inputfields: Reuse familiar ProcessWire inputs, settings and validation.
  • Files and images: Work with ProcessWire-compatible Pagefile and Pageimage objects, including image resizing and custom file metadata.
  • Selectors: Query pages by properties inside Objects and Rows.
  • Schema evolution: Unknown properties are preserved during ordinary saves, with tools for renaming and cleaning up obsolete properties.

Flex fields can also be used inside native ProcessWire Repeaters.

A small example

A field definition can look like this:

return [
    'heading' => ['type' => 'text'],
    'blocks' => [
        'type' => 'rows',
        'types' => [
            'text' => [
                'children' => [
                    'body' => ['type' => 'TinyMCE'],
                ],
            ],
            'gallery' => [
                'children' => [
                    'images' => [
                        'type' => 'image',
                        'maxFiles' => 10,
                    ],
                ],
            ],
        ],
    ],
];

The content is then accessible through the regular ProcessWire API:

echo $page->content->heading;

foreach($page->content->blocks as $block) {
    if($block->rowType() === 'text') {
        echo $block->body;
    } elseif($block->rowType() === 'gallery') {
        foreach($block->images as $image) {
            echo $image->size(800, 600)->url;
        }
    }
}

Properties inside Flex fields can also be queried using ProcessWire selectors:

$matches = $pages->find(
    'content.blocks.text.body*=ProcessWire'
);

Editor preview

image.thumb.jpeg.66ea987961242d5b9a31cb82693fb958.jpeg

Watch the short editor demo

Requirements

  • ProcessWire 3.0.259+
  • PHP 8.1+
  • MySQL 8.0+ or MariaDB 10.11+
  • InnoDB

The repository includes installation instructions, a quick start, configuration examples and an API reference.

GitHub: phlppschrr/FieldtypeFlex

I'd appreciate any feedback, bug reports or suggestions, particularly from people interested in trying it with different ProcessWire setups.

Thanks for taking a look!

 

 

  • Like 10
  • Thanks 1
  • interrobang changed the title to FieldtypeFlex – Structured content with native ProcessWire Inputfields
Posted

Thanks! There's definitely quite a bit of overlap.

Mystique and ProFields Custom Fields cover many of the same basics.
FieldtypeFlex adds nested Objects, repeatable Rows (similar to ReapeaterMatrix), File/Image fields and more advanced selectors if you need them. 

ProFields Custom Fields has the advantage of ListerPro integration (not yet tested with FieldtypeFlex) and ProFields support from Ryan. Once repeatable properties and File/Image support are added, I'd probably prefer it over my own module. 

I built FieldtypeFlex mainly because I wanted those features now, while still using native ProcessWire Inputfields. It has a pretty extensive test suite, although it's still a pre-release.

 

  • Like 6
Posted

Congratulations @interrobang

I haven't updated the Mystique module in a long time, so it's great that you developed a free alternative module and included file/image support in it. Since not everyone has the Pro modules, this serves as a good alternative. From what I see in its features, your module, FieldtypeFlex, looks like it will be an alternative to FieldtypeMystique and FieldtypeCustom.

In fact, ProcessWire should come with a field like this in the core by default. The experience we gain drives us to build modules based on our needs, and when you look at it, I think modules that can be used across most areas and projects should reside in the core. Perhaps a discussion and a poll should be held for each module on whether it belongs in the core or not. Looking at what we currently have, there are modules in the core that are rarely used and only needed in exceptional cases.
Another structure I consider lacking is the concept of everything being a page in ProcessWire. Additionally, I think it needs an entity layer for data management. Two fundamental entity types—Single and Collection—would provide the infrastructure on the ProcessWire side to handle most tasks for users without needing extra modules. This entity structure could then be used both by modules and by site-specific templates.

Because I didn't want to clutter this forum topic any further regarding the ProcessWire Entity layer, I created another topic.

 

  • Like 4
Posted

Thanks @ukyo! I really appreciate your feedback, especially coming from the author of Mystique!

I agree that a fieldtype like this would be a good addition to the ProcessWire core. For now, though, I also enjoy developing FieldtypeFlex independently. It gives me the freedom to experiment and refine things without having to worry too much about backwards compatibility at this stage.

Regarding the entity layer, I haven't personally run into major limitations with ProcessWire's "everything is a Page" approach so far, so I don't really have a strong opinion on that. But WireEntries definitely sounds like an interesting project, and I'm curious to see how it develops!

Thanks again for sharing your thoughts!

  • Like 3
  • 2 weeks later...
Posted

Hi everyone,

A quick update since the 0.4.0 preview. FieldtypeFlex is now at 0.7.0, and the main addition is a simple table editor for things like price lists and product comparisons. You define the columns in the field definition, and editors can add, duplicate, reorder or hide rows. The cells use native ProcessWire inputs, and you can let editors change the column headings and add row labels. 

image.thumb.png.d5197e281fef460d1fa0a953deb41429.png

Here’s the PHP field definition for that price list.

return [
    'prices' => [
        'type' => 'table',
        'label' => 'Service price list',
        'addRowLabel' => 'Add service',
        'columns' => [
            'service' => ['type' => 'text', 'label' => 'Service', 'width' => '15rem'],
            'duration' => ['type' => 'text', 'label' => 'Duration', 'width' => '8rem'],
            'price' => ['type' => 'float', 'label' => 'Price (USD)', 'width' => '8rem'],
            'booking' => [
                'type' => 'select',
                'label' => 'Booking',
                'width' => '10rem',
                'options' => [
                    'open' => 'Available',
                    'request' => 'On request',
                ],
            ],
        ],
    ],
];

Table columns currently only support a subset of ProcessWire’s Inputfields. You can use Text, Email, URL, Integer, Float, plain Textarea, Checkbox, Toggle, Select, Radios, Checkboxes, SelectMultiple, AsmSelect, TextTags and Datetime. For Page references, you can use Select, SelectMultiple, AsmSelect or PageAutocomplete.

There are a few other changes since the preview:

  • FieldtypeFlex now supports AdminThemeBridge
  • support for language alternate fields such as `image_de`.
  • The field settings can generate PHPDoc for your Flex definition, which is handy for IDE autocompletion.
  • The tools for renaming and cleaning up stored properties now live in the field’s Actions tab.


 

  • Like 6
Posted

Hi everyone,

FieldtypeFlex 1.0.0 is here. When I posted the 0.7.0 update, the big news was a simple table editor for things like price lists. Using it on a live site now, alongside native inputs and nested content blocks, feels like a good moment to call it 1.0. It has become the field I’ve wanted in ProcessWire for years.

  • Flex content works with ProcessWire’s native Pages Export/Import. You can move pages between installations using JSON, including nested rows, translations, and page references. File and image originals can be transferred from a publicly accessible HTTPS source. Flex media embedded in ZIP archives isn’t supported in 1.0.0.
  • Tables grew beyond simple cells. Text and numbers still edit inline. TinyMCE, images, files, and page-tree inputs open under the row. You can also sort supported columns by clicking their headings or set a sort order in the definition.
  • Rows are easier to work with at scale. Copy and paste one or several rows between compatible collections, even across ProcessWire installations. There are bulk actions and less work when adding a block to large, nested fields.
  • Images feel more like native ProcessWire images. Flex uses the native image actions. 
  • Removing a block type doesn’t remove its content. Existing rows and their media are kept, Page Edit remains usable, and you decide when to clean up the old data.
  • If you’re upgrading from 0.7.0, check your rowTitle definitions: use {heading} instead of heading, for example. The changelog has the details.

I’ve updated the first post with two configurations and kept the original announcement below it. I’m already using Flex in a production project, where it’s been working well and I haven’t run into any issues so far.

1.0 is feature-complete for what I set out to build, and I plan to maintain it for the long term. I’d still love to see what you build with it, what feels awkward, or what you’d like to see in a future version. Thanks to everyone who tried the previews and shared feedback.

  • Like 2
  • interrobang changed the title to FieldtypeFlex – Schema-defined structured content in a single ProcessWire field

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
×
×
  • Create New...