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

Fix Plugin extendModel #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

ndunks
Copy link

@ndunks ndunks commented Jul 26, 2024

Fix property translatable is not merged when $translatePlugin->extendModel is called in multiple times.

I need to add custom field on CMS Page with translation too (in my plugin). I was using backend.form.extendFields event to add meta_keywords field with type mltext. This code to add translation to it.

/** @var \Winter\Translate\Plugin */
$translatePlugin =  PluginManager::instance()->findByIdentifier("Winter.Translate", true);

 /** @param \Cms\Classes\Page $model */
Page::extend(function ($model) use ($translatePlugin, $translatable) {
    $translatePlugin->extendModel($model, 'page', ['meta_keywords']);
});

But it remove translation to other fields title, url, etc.. After I debug it, I found that exetendModel function is not merging the translatable attributes. Current code:

public function extendModel($model, string $type, array $translatableAttributes = [])
{
    if (!$model->propertyExists('translatable')) {
        $model->addDynamicProperty('translatable', $translatableAttributes);
    } else {
        $model->translatable = array_merge($model->translatable, $translatableAttributes);
    }

if (dynamic) property already exists, because was called in my plugin. Then this code fail to merge the attributes.
Because the getter function will prioritize the attribute from dynamic properties, but the setter will set on instance attributes directly.

image

Leaving condition like this:
image

Theres 2 property, in instance attributes and in dynamic properties with different values.

This PR fixt that, with setting the attributes in dynamic property

Fix property `translatable` is not merged when `$translatePlugin->extendModel` is called in multiple times.
@mjauvin
Copy link
Member

mjauvin commented Jul 26, 2024

this method is not meant to be used elsewhere, it's just a helper in the Plugin class to avoid repeating ourselves... just extend the Page model in the usual way in your plugin.

@ndunks
Copy link
Author

ndunks commented Jul 27, 2024

How to extend "translatable" property in usual way? Can you give me example?

@mjauvin
Copy link
Member

mjauvin commented Jul 27, 2024

What version of WinterCms do you use?

@mjauvin mjauvin reopened this Jul 27, 2024
@ndunks
Copy link
Author

ndunks commented Jul 27, 2024

I checked by command: php artisan winter:version

This the result:

*** Detecting Winter CMS build...
*** Detected Winter CMS build 1.2.6.

@mjauvin
Copy link
Member

mjauvin commented Jul 27, 2024

There were changes in winter 1.2.5 that affects the way dynamic properties get created because of php 8.2+ deprecation (https://php.watch/versions/8.2/dynamic-properties-deprecated)

So we're now using winter dynamic properties but there may still be unexpected issues related to this.

Are you adding translatable items before or after the translatable property has been added to the CMS page by the translate plugin?

@mjauvin
Copy link
Member

mjauvin commented Jul 27, 2024

Here are the commits that changed this:

wintercms/storm@dce5c40

wintercms/storm@e03b13c

@ndunks
Copy link
Author

ndunks commented Jul 27, 2024

Page:extend callback in my plugin called before translatable plugin.

@ndunks
Copy link
Author

ndunks commented Jul 27, 2024

I think we need to add another test cases for dynamic attribute, such as merging array values

@mjauvin
Copy link
Member

mjauvin commented Jul 27, 2024

I think we need to add another test cases for dynamic attribute, such as merging array values

you're right. if you can do this as part of this PR, that would be awesome.

@ndunks
Copy link
Author

ndunks commented Jul 27, 2024

I will try it, but I can start it on the next week, probably in the monday (GMT+7).

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

Successfully merging this pull request may close these issues.

2 participants