Jump to content

Any way to create multiple fields (of the same type) at one time?


n0sleeves
 Share

Recommended Posts

Instead of creating a field one by one, is there any way to create multiples at the same time? For instance, if I wanted 3 text fields, it would be so awesome if I could type 3 comma separated names, labels and descriptions thus reducing time and increasing efficiency. I would love this feature! 

Link to comment
Share on other sites

You could do what you describe very easily with a module. In fact, my Page field select creator takes a very similar comma separated approach, but for page creation. So you could build something along those lines, but I wonder if it might be better to extend the batcher module to handle field creation. Have a look at that and see if you think its approach to page creation would also work for field creation for you. You could potentially even select the template(s) that you want the field added to after it is created.

Link to comment
Share on other sites

You could do what you describe very easily with a module. In fact, my Page field select creator takes a very similar comma separated approach, but for page creation. So you could build something along those lines, but I wonder if it might be better to extend the batcher module to handle field creation. Have a look at that and see if you think its approach to page creation would also work for field creation for you. You could potentially even select the template(s) that you want the field added to after it is created.

That would be awesome! I wish I was that advanced in my PHP to be able to tackle such a task, however I am not. I am still in the beginner stages of how to use PW  :( If anyone would like to attempt this, please!!!!!  :rolleyes: I know many will appreciate it. 

I will definitely check out your module for page creation. Seems like that would help in that area as well! 

Link to comment
Share on other sites

Ok, I have quickly hacked together some enhancements to Wanze's Batcher module. There is now a Create Fields tab where you can enter the details (name, fieldtype, label & description) for as many fields as you want.

A couple of things to keep in mind - some field types require some key config options on the Details and Input tab, so in some cases I am not really sure how much time this will really save. Because of that I have currently limited the field types that can be created to simple text based ones (text, email, url, numeric etc), and left out the more complex ones like page, repeaters, datetime etc. Of course there is the potential to add some of these settings to each item in the create field batcher, but we wouldn't want things to get overly complex.

Also, I am not really sure of the etiquette here - I have modified Wanze's module and he may not want to see this functionality in his module (which would be totally fair). So what I have done for now is just include a screenshot of what I have created and I'll wait to hear from him to get his thoughts before posting the code.

post-985-0-87256400-1381978350_thumb.png

  • Like 7
Link to comment
Share on other sites

also i think there are some code examples in the forum for how to add fields using the API; you would just paste that code in a template file and run it and all your fields/templates would get created... will have to search tomorrow...getting a bit late here

  • Like 1
Link to comment
Share on other sites

Macrura has a good suggestion. You could something like this:

<?php

$fieldtype = "FieldtypeText";
$ts = array("home", "basic-page");

$fields = 
"test1, Test 1, This is a first test creation
test2, Test 2, This is a second test creation";

foreach(explode("\n", $fields) as $field){ 
    $f = new Field();
    $components = explode(",",trim($field));
    $f->name = $components[0];
    $f->type = $fieldtype;
    $f->label = $components[1];
    $f->description = $components[2];
    $f->save();
    
    foreach($ts as $t){
        $template = $templates->get($t);
        $template->fields->add($f);
        $template->fields->save();
    }
}

You can place this is a template file, or to make life easier, you can simply paste it into the Code Tester module or the Hanna code module (using the code test functionality), which makes it super quick and easy to run the code whenever you want.

All you need to do is worry about the editing the $fieldtype, $ts (templates) and $fields variables - you should leave the rest untouched.

Each line in the $fields string is a new field with the components being: name, label, description

This will create all the defined fields and automatically add them to all the templates you specify in the $ts array.

Hope that helps - let us know if you need any help with it.

  • Like 4
Link to comment
Share on other sites

  • 2 years later...

Maybe not as convinient, but if you are comfortable in editing json, you can export a text field with built-in PW field export feature, paste the generated code to text editor, duplicate it as many times as you want, edit nearly all the field's properties and import it back into your site.

  • Like 2
Link to comment
Share on other sites

Maybe not as convinient, but if you are comfortable in editing json, you can export a text field with built-in PW field export feature, paste the generated code to text editor, duplicate it as many times as you want, edit nearly all the field's properties and import it back into your site.

Thanks, seems like a good approach. Was unsure at first whether the "id" value in the JSON export needed to be incremented when duplicating for import, but after testing it looks like PW ignores the id value on import and assigns a new id, so no need to worry about that.

  • 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...