Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$this->isFresh($element) seems to not be working #4586

Closed
lewisjenkins opened this issue Jul 16, 2019 · 4 comments
Closed

$this->isFresh($element) seems to not be working #4586

lewisjenkins opened this issue Jul 16, 2019 · 4 comments

Comments

@lewisjenkins
Copy link

Description

When checking $this->isFresh($element) in my plugins, it's suddenly never true, even for brand new entries.

For example:

public function getInputHtml($value, ElementInterface $element = null): string
{
    if ($this->isFresh($element) {
        // Never true, even on brand new entries.
    }
}

Steps to reproduce

The quickest way to test is with a standard Number field, which uses $this->isFresh($element) to check if the default value should be used.

  1. Create a new number field
  2. Give it a default value
  3. Add it to a section
  4. Create a new entry. The number field is empty.

Additional info

  • Craft version: 3.2.3
@brandonkelly
Copy link
Member

As of Craft 3.2, entries are now created in an ephemeral “unsaved draft” state up until the first time you click “Save Entry”. So by the time getInputHtml() is called, the entry will already exist on some level, so it’s expected that isFresh() would always return false. It will still return true when that unsaved draft is initially created though, if you were to call it from normalizeValue() for example.

@lewisjenkins
Copy link
Author

Thanks for the explanation. What will happen with Craft's built-in Number Field, which has a Default Value setting that gets applied for new elements? If it's no longer working as documented then I guess it's a bug? Should I create a new issue?

@brandonkelly
Copy link
Member

Ah, thanks for pointing that out, this issue is fine. I’ll fix…

@brandonkelly
Copy link
Member

Fixed for the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants