Jump to content

Default Value for Fields (in Admin)


SwimToWin
 Share

Recommended Posts

Is there a way to set a default value in the editing interface for a Field? For instance, for a Text, Integer or Page field used in a Template.

For example, lets say I have a template called Songs with the field Length. Length has default value 3:00 (minutes). When I create a new Song Page, the Length field should appear pre-populated in the editing interface with the "3:00" default value.

Link to comment
Share on other sites

Hi SwimToWin,

don't know the answer but have found this in Wishlist: http://processwire.com/talk/topic/394-default-field-value/

EDIT: in this thread are a good discussion about the pro and cons of default values (redundant data and more)

http://processwire.com/talk/topic/1593-how-can-i-specify-default-value-for-certain-input-field/

---

BTW, the forum search isn't very good, better you use google with site:processwire.com  or site:processwire.com/talk

plus your search terms

https://www.google.de/search?q=site:processwire.com/talk+field+default+value

Edited by horst
Link to comment
Share on other sites

This Module works in part as a way to set default values for fields (not in Admin, but using a Module). The Module can set default values for simple fields such as Text and Integers on new Pages. It does not work with Page fields and does not work on existing pages.

<?php
# /site/modules/FieldDefaultValues.module

class FieldDefaultValues extends WireData implements Module {
    public static function getModuleInfo() {
        return array(
            'title' => 'Field Default Values',
            'summary' => 'Set default value for specified fields when creating a new Page.',
            'version' => true,
            'autoload' => true,
        );
    }

    public function init() {
        $this->addHookBefore("Inputfield::render", $this, "setFieldDefaultValueBefore");
        #$this->addHookAfter("Inputfield::render", $this, "setFieldDefaultValueAfter");
    }

    // Loads every time a page is loaded (whether new or not)
    // Works for new items only.
    public function setFieldDefaultValueBefore(HookEvent $event) {
        // Default values
        $defaults = array(
            "foo" => "3:33",
            "bar" => "2:33",
            "baz" => "1:33",
        );

        $obj = $event->object;
        foreach ($defaults as $fld=>$def) {
            if($fld == $obj->name) {
                // Set Default values if a value does not exist
                $obj->value = ($obj->value) ? $obj->value : $def;
            }
        }
    }

    // Fails (values are not set)
    public function setFieldDefaultValueAfter(HookEvent $event) {
        // Default values
        $defaults = array(
            "foo" => "3:33",
            "bar" => "2:33",
            "baz" => "1:33",
        );

        $obj = $event->object;
        foreach ($defaults as $fld=>$def) {
            if($fld == $obj->name) {
                // Set Default values if a value does not exist
                $obj->value = ($obj->value) ? $obj->value : $def;
            }
        }
    }

} 
  • Like 1
Link to comment
Share on other sites

For your inspiration: When defining Contenttypes in Bolt CMS, you can set Field definitions in YAML (really nice! Field definitions are fully portable between sites). In Bolt, default values are set using an attribute called "default".

I am writing this here to inspire a similar implementation in ProcessWire -- Spyc may come in handy as a way to implement YAML configuration files for Field definitions and other configuration in ProcessWire.

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