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

New BC-safe API to support CMSMain refactoring #11461

Merged

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented Nov 11, 2024

DO NOT SQUASH. There are two commits here:

  1. Deprecate DBEnum::flushCache() in favour of DBEnum::reset()
  2. Introduce the new DataObject.class_description config and its associated methods (replacing SiteTree.description)

The CMS 6 PRs have additional context for these changes if you need it.

Issue

Comment on lines +953 to +957
* Get description for this class
* @return null|string
*/
public function classDescription()
{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using PHPDoc for this and i18n_classDescription()'s return types for BC. Since these methods already exist on SiteTree without strict return types, we can't add them here.

Comment on lines 976 to 980
// Fall back on the deprecated localisation key
$legacyI18n = _t(static::class . '.DESCRIPTION', $baseDescription);
if ($legacyI18n !== $baseDescription) {
Deprecation::notice(
'5.4.0',
'The ' . static::class . '.DESCRIPTION' . ' i18n localisation key is deprecated. Use '
. static::class . '.CLASS_DESCRIPTION' . ' instead.',
Deprecation::SCOPE_GLOBAL
);
return $legacyI18n;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BC protection for custom classes where devs have added localisations for the description. Will be removed in 6.

Comment on lines -4375 to +4423
static::class . '.SINGULARNAME' => $this->singular_name(),
static::class . '.CLASS_DESCRIPTION' => $this->classDescription(),
static::class . '.SINGULARNAME' => $singularName,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing $this->singular_name() with $singularName is unrelated tidyup, since that variable is defined above.

Adding CLASS_DESCRIPTION here ensures text collector knows how to collect the value for this key.

*/
public function i18n_classDescription()
{
$placeholder = 'PLACEHOLDER_DESCRIPTION';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable usage is weird, can't we just using an empty string default e.g. _t(static::class . '.CLASS_DESCRIPTION', ''); and then test to see if it's truthy?

Copy link
Member Author

@GuySartorelli GuySartorelli Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not, because _t() does a falsy check against $default and throws an error

if (!$default && i18n::config()->uninherited('missing_default_warning')) {
user_error("Missing default for localisation key $entity", E_USER_WARNING);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. $placeholder is a little confusing since it's being used to test there's no value, rather than as an actual placeholder.

Change it to $notDefined == 'NOT_DEFINED'; so that we can go if ($baseDescription === $notDefined) { later on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 979 to 984
Deprecation::notice(
'5.4.0',
'The ' . static::class . '.DESCRIPTION' . ' i18n localisation key is deprecated. Use '
. static::class . '.CLASS_DESCRIPTION' . ' instead.',
Deprecation::SCOPE_GLOBAL
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Deprecation::notice(
'5.4.0',
'The ' . static::class . '.DESCRIPTION' . ' i18n localisation key is deprecated. Use '
. static::class . '.CLASS_DESCRIPTION' . ' instead.',
Deprecation::SCOPE_GLOBAL
);
$descKey = static::class . '.DESCRIPTION';
$classDescKey = static::class . '.CLASS_DESCRIPTION';
Deprecation::notice(
'5.4.0',
"The $deskKey i18n localisation key is deprecated. Use $classDescKey instead."
);

Should use SCOPE_CLASS (the default) not SCOPE_GLOBAL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class isn't deprecated, so SCOPE_CLASS isn't appropriate. SCOPE_CLASS would mean the deprecation notice says something like "DataObject is deprecated" which just isn't true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also SCOPE_METHOD is the default - and is also not appropriate because the method isn't deprecated either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I don't think we should be deprecating translation keys even though this is a high use area. I don't think we've ever done this before as we'll simply stop using old translation keys. It seems like its setting a new precedent/policy which I don't think needs to be there.

The new keys are just duplicates of the old keys, so the only risk of something going wrong is that any new translation updates won't be received.

If you really want to keep this, then you'll need to update the CMS 6 PR to throw exceptions if the old keys are used otherwise there seems like there is very little point in doing this

Copy link
Member Author

@GuySartorelli GuySartorelli Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't throw exceptions when things are deprecated. We use deprecation notices. We clearly need to discuss this in person lol it seems like this is being blown out of proportion a bit.

Throwing an exception because of something that - as you've pointed out - is low risk is a really bad idea. We should instead log a warning. A warning that says to stop using the old thing. At that point we're just reinventing deprecation notices.

I don't think we've ever done this before as we'll simply stop using old translation keys.

We're going to stop using the old one and start using something else. I would like people to know when they're trying to use an old localisation key, so that they can update to use the new one.

Would you prefer if I emit a user_error() instead and remove the word "deprecation"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree offline to keep the fallback though remove the deprecation notices

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/ORM/DataObject.php Show resolved Hide resolved
* Get localised description for this class
* @return null|string
*/
public function i18n_classDescription()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function i18n_classDescription()
public function getI18nClassDescription()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mixing snake_case AND camelCase :-)

We shouldn't be adding new API that's not following modern PHP coding standards

Copy link
Member Author

@GuySartorelli GuySartorelli Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to change these names. I'm not adding new API, I'm just moving it up the class hierarchy. Renaming the methods which exist on SiteTree is not the intention here.

getI18nClassDescription is especially bad since it's then not going to pop up with I go $this->i18n to find the i18n methods like i18n_pluralise(). It should explicitly and intentionally start with i18n - or we should rename the other i18n methods which is not in scope for this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

This provides a description of what a class is intented to be used for,
which is useful for content authors picking a class to create in the CMS
UI.
@emteknetnz emteknetnz merged commit 3896ae7 into silverstripe:5 Nov 14, 2024
14 checks passed
@emteknetnz emteknetnz deleted the pulls/5/class-description branch November 14, 2024 02:03
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

Successfully merging this pull request may close these issues.

2 participants