Jump to content

Preset values for fields?


tires
 Share

Recommended Posts

for the admin you would need to create a module that would check that field, possibly on the buildForm hook;

another possibility would be to use AdminCustomFiles and see if there is a way to check the field for empty using JS and then set the value.. but could be risky with JS...

Link to comment
Share on other sites

I would use some JS code that would add "placeholder" content to the inputfield elements (which would be easy, since they are all referenced by ID in the admin template) wherever I want default content. This way it would only be displayed if the field were empty, and shouldn't be particularly problematic from a security point of view either (I think...). Let me know if you want to see some example code.

Link to comment
Share on other sites

You could hook into ProcessPageEdit::loadPage and set the wanted fields.

class EmptyFields extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Empty Fields',
            'version' => 1,
            'summary' => 'Set default title if empty.',
            'singular' => true,
            'autoload' => 'template=admin',
        );
    }

    public function init() {
        $this->addHookAfter('ProcessPageEdit::loadPage', $this, 'afterLoadPage');
    }

    public function afterLoadPage(HookEvent $event) {
        /*

        Here you could make your own logic.
        I've done this ones with a table field.
        fields in the table were: template-name, field-name and value.
        Then you can get the right value for your page on the row where template and field-name match.

        */
        $page = $event->return;
        $page->title = $page->title == '' ? date("Y-m-d H:i:s") : $page->title;
    }
}

Edited by Martijn Geerts
added an example
Link to comment
Share on other sites

Thanks for your answers!

Ok, i see this is a little complicated and not intended to do for fields in pw.

I hoped that there is a possibility to set this preset text/code/file in the fieldsetting (or in the template field settings).

Is there a reason not to implement this feature in pw?

Link to comment
Share on other sites

  • 11 months later...

I came up with a workaround for this.

Let me know your thoughts

I have an event page set up with a body field to be used for info before the event. (Body_PRE_Event).

In the admin pages I created another field (Body_PRE_Event_DEFAULT). I populated this with default text.

I then created this php code to see if the event field was filled out, if not, it uses the default field.

<?php if ($page->Body_PRE_Event){
                echo "{$page->Body_PRE_Event}";
            }
            else{
                echo $pages->get("/site-content/event_page_defaults/")->Body_PRE_Event_DEFAULT;
            }
			?> 

It seems to work well, only issue is that the default info does not appear when creating the event.

Which is fine, as long as the editor knows default info will replace it.

Thanks

BG66

Link to comment
Share on other sites

It seems to work well, only issue is that the default info does not appear when creating the event.

Which is fine, as long as the editor knows default info will replace it.

Hi, If I understand the issue correctly, you might want to check out these modules, so that you can show the default:

http://modules.processwire.com/modules/template-notes/

http://modules.processwire.com/modules/fieldtype-runtime-markup/

http://modules.processwire.com/modules/inputfield-textarea-markup/

Link to comment
Share on other sites

  • 5 years later...

Hi Folks!

After a few years went by, i still ask myself if there is an easy way to set a default text/values for text/textarea fields?

What do you think?
Or am i the only one who needs something like that?

  • Like 1
Link to comment
Share on other sites

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