Jump to content

Dynamic Page Fields module


diogo
 Share

Recommended Posts

Hey!

This is a very early/experimental/alpha not for production environments release of a new module I'm working on. Just testing the possibilities and the interest that might be for such a thing. The idea is to be able to quickly create fields while working inside the template files without having to go to the admin. For example, in an early stage of development when creating a layout, insteading of putting a placeholder headline and then going to the admin to create the headline field where the real content will be, you can create the field directly from the template file by doing:

<h2><?=$page->field("A nice headline", "headline", "text")?></h2>

This will create a new "headline" text field when it doesn't exist in the system and add it to the the template of the page you're working on and add "A nice headline" value to it. On a second run the module will recognize that the field already exists on that template and will simply return it's value.

The method accepts three string parameters in the order ( value, field name, field type ). Value and name are required but field type will assume "text" when omitted.
 

$page->field("A nice headline", "headline"); // creates a text field

$page->field("A nice paragraph", "body", "textarea"); // creates a textarea field

The method also accepts a boolean parameter in any position: TRUE forces the value to be saved into the field on that page, FALSE (or omitted) sets that value to the field when it adds it to the template, but leaves it how it is when the field already exists in the page. This is the only situation when this module can be destructive, and it's only concerning the value of the field on that page:

$page->field("Another nice headline", "headline", TRUE); // will create or add a text field and add the value to the page even if the field already exists there

 

The method also accepts an array as parameter. This array will be used as options for the creation of the field. The options array will override the string parameters when they clash.

$page->field("A nice headline", "headline", array(
		'name' => 'subtitle',
		'label' => 'Subtitle'
	)); // will ignore the name "headline" and use "subtitle" instead

 

As soon as the fields are created and added to the template, all the method will do is return the value, just like if using $page->headline, unless you use the TRUE parameter which will override the value of the field by the value in the first parameter.

--

With this module you can, for instance, convert an existing HTML template quickly, just by pasting the html into a template with no fields created yet, and going through all the code, replacing the content by calls to my field() method. In the end you should have a working page with all the fields created in the template. Now you just need to go to the fields and adjust their preferences.

So, just download it > install it > test it > tell me what you think!

dynamicPageFields.module.zip

 

--

Edit:

Multilanguage fields threw an error but they don't now. You can create a multi lang text by doing:

$page->field("A nice headline", "headline", "textlanguage");

that third parameter is just a case insensitive simplification of input field names to make it easier to write. You can still use the original input field name like "FieldtypeTextLanguage" if you prefer.

  • Like 16
Link to comment
Share on other sites

This looks really promising @diogo! I like it a lot and I'll sure try it on a next project.

One question, what about multilanguage fields, do you think it's possible or should we avoid it to not overcomplicate the module?

  • Like 1
Link to comment
Share on other sites

Thanks @Sérgio! Just some minutes ago I added the possibility to create multi language fields and uploaded a new zip file. This doesn't let you add the language values, it only allows you to define the field as multi language. I'm not sure how useful it would be to add the possibility to add the language values, since it can get really complicated in sites with lots of languages and I thought of this as a fast development tool. I would check how to support all major field types first, and then think of multi language.

  • Like 2
Link to comment
Share on other sites

3 minutes ago, diogo said:

Thanks @Sérgio! Just some minutes ago I added the possibility to create multi language fields and uploaded a new zip file. This doesn't let you add the language values, it only allows you to define the field as multi language. I'm not sure how useful it would be to add the possibility to add the language values, since it can get really complicated in sites with lots of languages and I thought of this as a fast development tool. I would check how to support all major field types first, and then think of multi language.

 

Great! You're right, it can be messy! Great work, keep it up! :D

Link to comment
Share on other sites

@Sérgio just to let you know. I did a small test, and without making any change, the module already has kind of multilanguage support because the value will be set in the language of the user visiting the page. So, if you set the TRUE parameter to force the value and visit the page with another language, it will actually set the values in that language to the values that are currently set in the value parameter. Not sure if that's more useful than editing the page in the admin though.

  • Like 1
Link to comment
Share on other sites

Great, that will work! 

I think it will be very useful to have the short texts translated right away when creating the template and after that we can go to the admin to translate the long ones.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

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

  • Recently Browsing   0 members

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