We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Is there a tutorial on creating field types?

Development and Programming

fcb66's avatar
fcb66
11 posts
14 years ago
fcb66's avatar fcb66

Hi,

I’m using EE2 and am trying to create my own field type but the EE documentation is a little light on the subject. (At least the documentation I have managed to find is.)

I’m trying to create a checkbox that populates from the title of a channel and then on the publish page has a number of radio fields next to it so an item can be tagged and then an option chosen.

Does anyone know of a good resource to learn from? I’m mostly working through it via trial and error and the process is taking far too long with a deadline getting ever closer!!

Any help would be greatly appreciated.

Thanks.

[Mod Edit: Moved to the Development and Programming forum]

       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

Have you seen this: http://ellislab.com/expressionengine/user-guide/development/fieldtypes.html

       
fcb66's avatar
fcb66
11 posts
14 years ago
fcb66's avatar fcb66

I did find that early on in my searches but it doesn’t go into much detail for someone who has never built a field type before.

However having spent a morning messing around with code, reading it again it’s starting to make more sense.

Thank you, I’ll give it another bash and see how I get on.

       
fcb66's avatar
fcb66
11 posts
14 years ago
fcb66's avatar fcb66

Ok, having had some success with a fairly simple field type, I’m now working up to what I actually want to implement and I’ve got myself stuck again.

Take this, (very reduced!), code:

1 class my_field_type_ft extends EE_Fieldtype {
2   function display_field($data)
3    {
4        $this->settings['test_var'] = 'Testing';
5 
6       echo "settings field names: ".$this->settings['test_var']."";
7        return 'form html for publish page';
8    }
9    
10   // --------------------------------------------------------------------
11   
12    function save($data)
13    {
14        echo "test_var: ".$this->settings['test_var']."";
15        die;
16        //return 'Return string to save';
17    }
18   
19    // --------------------------------------------------------------------
20 }

display_field($data) is the function that shows the field type on the publish page, and save($data) saves the data when the submit button is clicked.

I want to store a value in $this->settings, (or some other variable), when the publish page is built that I can then test in the save function.

Line 4 sets a value, and line 6 will correctly write it to the screen on the publish page, but by line 14 it no longer exists.

It’s either a scope issue, or the fact that by the time the save function runs it is a different instance of the class so the variables are empty.

Is there any way to force $this->settings to be saved at the end of display_field, or is there another method I have not thought of yet? (I’ve been looking at this for a very long time, so could well be missing something glaringly obvious!)

Thanks.

       
fcb66's avatar
fcb66
11 posts
14 years ago
fcb66's avatar fcb66

Ok, I’ve managed to get this working in a different way involving more code than I was hoping for, but it’s working. I can refine it later when I have time.

I’m now doing some crunching through the $_POST data to work out what has been submitted and turn it into the data that will ultimately be saved in the DB. This logic is now in the validate function and I am no longer over-riding the save function.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.