Jump to content

Module: FieldtypeConcatenate


ryan

Recommended Posts

ProcessWire Concatenate Fieldtype

Fieldtype that concatenates the values from one or more other fields at runtime. The value can contain additional formatting and/or words as needed, which you define in your Concat field settings.

Example

  • Problem: Your system has a first_name and last_name field, and you want to have a separate full_name field composed of first_name and last_name, without redundancy.
  • Solution: You would create a new Concat field, click the details tab, and enter "first_name last_name" (the fields you want to concatenate) in the settings.

Other Potential Uses

  • Having a field that combines the value of two or more others, without the redundancy of separately stored data.
  • Defining a custom “label field” for select boxes, like those used with the Page field.
  • Defining a custom label for your Page List that includes your own formatting.
  • Defining an alternate variation of a text field that uses a different text formatter.

Considerations

  • The value for this fieldtype is generated at runtime and thus no data is stored in the database. This is good because there is no duplication. However, it also means that you cannot directly query a Concat field from $pages->find(), for example.
  • If you happen to change the name of a field being used in a Concat field, you will have to update the name in your Concat field settings as well.
  • By design, Concat fields do not inherit the text formatters of the fields they concatenate. You define these separately with the Concat field.
  • Because this is a runtime-generated field, there is no Inputfield associated with it.

How to Install

  1. Install the module by placing FieldtypeConcat.module in /site/modules/.
  2. Check for new modules on the Modules screen in the ProcessWire admin.
  3. Click Install for the Concat Fieldtype.

How to Create a Concat Field

  1. Under Setup and Fields create a new field using type Concat.
  2. After entering the new field name and label, click Save.
  3. Click the Details tab and enter one or more field names. Separate them with whatever spacing and punctuation is appropriate.
  4. Optionally choose one or more Text Formatters. If you are not sure which, “HTML Entity Encoder” is a good default to use.
  5. Save.
  6. Add your new field to one or more Templates.

How to access the value of a Concat field

This is no different than accessing the value of any other field. If your Concat field has the

name “full_name” then you would output its value like this:

echo $page->full_name;

Download

  • Like 20
Link to comment
Share on other sites

Hi Ryan,

This is great! It seems to be working correctly for me if I use it to create a new field and use it in a template.

However, if I use it to set a new "label field" for a select — I'm still getting the default (title). Install is running 2.2.9.

Link to comment
Share on other sites

However, if I use it to set a new "label field" for a select — I'm still getting the default (title). Install is running 2.2.9.

I can't seem to duplicate here. Can you double check that your new Concat field is attached to the template used by your selectable pages?

Link to comment
Share on other sites

*slaps forehead*

Sorry Ryan — works fine.

I was testing with user info, and added the field to a custom user template I was testing instead of the system user template.

False alarm.

Thanks again for this, super useful.

Link to comment
Share on other sites

  • 4 weeks later...

I've updated it so that it can now support using formatted values before concatenation--like you would want with dates. Grab the new version (1.0.1). Then, in your format string, just specify an exclamation point after any field names you want to use the formatted version for. For example, "title, date!" rather than "title, date". 

  • Like 4
Link to comment
Share on other sites

@doolak, it's working for me here, but I found it may depend where you are displaying the value. If you are using it for something on the admin side (where output formatting is off by default) it may not have worked. I just updated it to force the output formatting. Try it again with the latest and let me know if it works now?

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
Can't find file: './pw_koulutus_pwire_fi/field_fullname.frm' (errno: 2)

I don't think this error is coming from the Concatenate fieldtype, because it does not deal with the file system. I'm also not familiar with what a ".frm" file is? It sounds like there must be some other 3rd party module installed that is triggering this error, but I don't recognize the error or filename format. What modules do you have installed that might be manipulating files?

Link to comment
Share on other sites

  • 4 months later...

Hi Ryan, this is a great and useful module.
 
I have some problems with it though:
 
Let's say my concat string looks like this
"title - somePagefield.title - someFancyHookedField"
the someFancyHookedField is a field on which I manipulate the output with a hook (like described here)
 
Everywhere else in my template files the output of that field works like it should but via the concat fieldtype it stays empty.
 
Maybe the module does not take hooks on fields in consideration?

edit: I did not read the description carefully, with an exclamation mark, the formatting of the field takes place and so does then the hook.
 
PS: I get the same error like apeisa when renaming the field, no prio though.

Link to comment
Share on other sites

  • 7 months later...

Does / could this work with arrays at all?

I've got a template that has a repeater with a page select of the days in a week and sometimes multiple days are selected - can I use this to display just the first title of an array? Right now I'm seeing it return "Array, {other field name}" and ideally it would show "Monday, {other field name}" if all days are checked.

Link to comment
Share on other sites

  • 4 months later...

@Ryan in PW 2.4.10 (from 2.4.3 atleast) this module stops working and throws an error "Class 'FieldtypeText' not found" on line 20

class FieldtypeConcat extends FieldtypeText

Please see: https://github.com/ryancramerdesign/FieldtypeConcat/issues/3

Changing the line to this, fixed the error, but doesnt seems format the output anymore.

class FieldtypeConcat extends Fieldtype
Link to comment
Share on other sites

Raymond, Can you try out the update I just pushed to FieldtypeConcat? I couldn't duplicate the issue you are seeing, but I can how why it's feasible to occur. I updated the module to identify it's requirements for ProcessWire 2.5+ dependencies and am wondering if this might resolve the issue. 

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...

This is exactly what I was looking for! Thank you :)

I'm using it to create a custom "Label Field". However, it shows the ID, versus the title. Is there something I need to do to get it working when concatenating fields of the Page fieldtype? Or does it only support certain fieldtypes, like Text?

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...