Jump to content

Default Field value


Recommended Posts

The core has support for it, but it's not been fully implemented in the modules. The reason is for this is that it's simple to implement with some fieldtypes, and not so simple with others. I wanted to wait until I could get to the point of providing consistent support for it in all the fieldtypes where it makes sense.

Link to comment
Share on other sites

  • 5 months later...

Hi ryan

I've just come across a need for this in an autocomplete field. Would it be easy enough to do simply by checking for a field and overriding that module or is it a bit more involved than that?

My thinking is to somehow override the current module, check for a specific field name and insert a default value if no values are stored for that field, but I'm not sure how possible that is.

It's not a big issue, I just have a scenario whereby people will be posting news and it would be better to have a field that automatically stores their user ID as the author rather than making them select it themselves. The reason for the autocomplete is because there are already a large number of writers, plus just to make it even trickier more than one writer can be attributed to an article :)

Link to comment
Share on other sites

Pete, I think the simplest thing would be to add a hook before InputfieldPageAutocomplete::render, retrieve the 'value' attribute and add the default value you want when it's blank. Let me know if I can provide a more code-oriented description/example.

Link to comment
Share on other sites

Thanks for that ryan - I can see that I can access the array in a function from that - I added a hook that ran a function called 'test' just to drill down to the values:

public function test(HookEvent $event) {
	echo "<pre>";
	echo "</pre>";

This works fine and I can see if a field has a default value, but unfortunately I don't seem to be able to add a value - could you point me in the right direction for that please? I'm not very good with OOPHP, so I tried the following to add page 1306 to the value array and it didn't work:


Aside from that I can easily see how to check the field name to make sure it's the one I want (useful as the template has 3 autocomplete fields ;)) so I'm halfway there.

EDIT: Ah, hang on, I found setAttribute in the autocomplete module so I'm nearly there. Will post the whole thing up shortly

Link to comment
Share on other sites

Thanks guys - Antti's was closest since this field allows multiple values.

The goal for this was to pre-populate the author field with the current user's ID if the author field is empty (something that would only be the case on new articles). The check for empty ensures that editing old articles doesn't end up appending my name to them all ;)

In the module's init() function we have this:

$this->addHookBefore('InputfieldPageAutocomplete::render', $this, 'addUserOnEmptyAuthor');

and the function is simply this:

public function addUserOnEmptyAuthor(HookEvent $event) {
$autocomfield = $event->object;
if ($autocomfield->attr('name') == 'authors' && !$autocomfield->attr('value') && !in_array(wire('user')->id, $autocomfield->attr('value'))) {
  	 $autocomfield->attr('value', wire('user')->id);

So it first checks the field is called "authors", then checks it's empty before adding the current user ID.

I also left in another check (that's not required, since the check for empty is enough), but might be useful for others thinking of doing something similar, which simply checks if the current user's ID is in the list of values already. I know that even if it did appear twice in the editor, PW only saves it once, but it would look a bit odd to have the same person's name twice so there's a check for that if anyone needs it.

And now I also know a bit more about OOPHP too as well as how to set default values for other fields ;)

Thanks all!

  • Like 2
Link to comment
Share on other sites

  • 6 years later...

As now, is it possible to define a default value for a field? 

It would be very handy for a use case. However I have read that this could be a design issue. 

My use case is the following:

Pages have a subpage text. The application allows customization on the text based on a entity. So by default the entity is "Default" (thus a default in the admin panel), but sometimes it could be entity1, entity2. So I have a page with the fields Type and text.

I already have considered just having a text and for that text again subpages for entity1, entity2, … But I am not sure if it is really better.


Edited by AndreasWeinzierl
Link to comment
Share on other sites

5 hours ago, AndreasWeinzierl said:

As now, is it possible to define a default value for a field?

I do not remember becoming it a reality, the most recent discussion seems to be this one:

There are some other links in it pointing to other related topics.

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

  • Recently Browsing   0 members

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