-
Notifications
You must be signed in to change notification settings - Fork 40
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
Follow-up: PHP 8.2 support for taxonomy vocabularies #6197
Comments
Hm. Or maybe this class is supposed to have dynamic properties? There's hook_taxonomy_vocabulary_load() – similar to Entities, which currently isn't used anywhere in core. This doc example indicates, that dynamic properties should be allowed. If that's the case, we might consider extending stdClass instead of a standalone class. For comparison with Entity: In Drupal 7 vocabularies are handled like other entities (via entity_load), but in Backdrop they're just instances of the standalone class, which seems OK, as they're rather simple compared to the fully classed Entity in B. If my suspicion, that TaxonomyVocabulary should actually allow dynamic properties, turns out to be correct, the solution could be: |
I think we should have it also extend If we're allowing dynamic properties then checking for |
So, if I understand this correctly, we are discussing two problems here: (1) the fact that vocabularies don't allow dynamic properties, and (2), the fact that extraneous properties added to the vocabulary class are being saved to the file. My thoughts on (1) is that, I agree that TaxonomyVocabulary should extend As for (2), this is a common issues with config files. When vocabularies were entities in D7, they had schemas, and therefore the CRUD save action always used the schema to map the class property to a column, and basically ignored/didn't save all other properties. There isn't an equivalent mechanism for config files (that is, there is no way to define which properties should be saved). Using So, to me, there are two possible paths: (1) Create a way to define which TaxonomyVocabulary properties should be saved by creating the equivalent of a |
@herbdool I don't think so, no. The properties aren't defined in the class, but get attached on-the-fly. @argiepiano I'm not sure, if we have two problems. But let's figure out. (Nothing "nasty" gets saved to config, no worries.) As far as I understand it, the config for vocabs (or anything) is just settings in JSON format. So not everything, that goes to a config file is necessarily supposed to be part of the object (instance) created from that config. BTW: if we prevent TaxonomyVocabulary config to save anything but valid properties, then i18n_taxonomy will break. And if we introduce a hook to handle additional properties, it still requires a rewrite. Re property_exists() in taxonomy_form_vocabulary_submit(). I think, that deserves a second and closer look, now that we learned that dynamic properties might be necessary. We might eventually revert that check. If we extend stdClass, the reason why we used it there, is gone. |
@indigoxela thanks for clarifying.
Probably. If we get rid of that check, some "noise" will be added to the class. But that should be OK, since removing that check allows modules to alter the form to add "legitimate" new stuff that should be added to the class. |
@indigoxela, I'm testing I tried this: class TestClass extends stdClass {
public $declared = null;
}
$testObject = new TestClass;
$testObject->dynamic = 'asdf';
dpm(property_exists($testObject, "dynamic")); // Returns TRUE. |
@argiepiano after you added it, it's there. But not before. And we'd probably need a way to check, if it's valid to add it. |
class Test {};
$original = new Test();
$obj = new Test();
$obj->original = $original;
echo property_exists($obj, "original"), "\n"; |
Yes, after you (dynamically) added it, the property is there. But we can't check in advance. 😉 |
That is true for It is not true that |
@kiamlaluno at the risk of derailing this issue a bit 😉, I'll try to explain what we're trying to solve.
property_exists() works fine, after the property (dynamic or not) has been set, but we're trying to prevent arbitrary stuff (from config or form submissions) to get added to the object, if possible. And that's tricky when dynamic properties are allowed, for instance when we extend stdClass. So, if we switch to....
The nagging goes away, but we can't reliably tell, whether a property is valid, or just garbage from config or form submission. Very likely (based on discussion here so far) we'll allow dynamic properties for vocabs.
This is future-proof to prevent nagging or failure with dynamic properties, but we probably have to live with anything getting part of the object via TaxonomyVocabulary::__construct or later on. Example: taxonomy_vocabulary_load_multiple() reads all vocab configs and builds the objects from anything in there. But everything's fine now, no tests fail on 8.2? Yes, but only, because we currently don't have a functional test for hook_taxonomy_vocabulary_load yet. As soon as we add one (which I'm planning), it will fail on 8.2. 😉 |
@indigoxela I understood the issue purpose; my comment was for another comment. Using // Create a new TaxonomyVocabulary object which is not altered by other hooks/functions/methods.
$vocabulary = new TaxonomyVocabulary();
if (property_exists($vocabulary, 'property') {
// The property is not a dynamic one.
} |
Thanks for taking the time to expand, @indigoxela. I think we are in agreement that @kiamlaluno, no need to keep insisting on correcting my comment. It was wrong. I already edited it (yesterday) by crossing it out and putting an edit note. |
@argiepiano I was not correcting your comment for the second time; in fact, I only said my comment was for another comment. I was replying to another comment, posted by somebody else, which seemed to misunderstand my previous comment. In particular, I was replying to property_exists() works fine, after the property (dynamic or not) has been set, but we're trying to prevent arbitrary stuff (from config or form submissions) to get added to the object, if possible. That's all. |
Now I revisited taxonomy_form_vocabulary_submit() in which we recently added a property_exists() check on form submissions ( I think, that's still valid, and we should keep that. 👍
The i18n module doesn't rely on initially saved config values, but checks form_state in an additional submission handler to switch its specific settings in the vocab config file accordingly. |
Reviewed and tested LGTM and RTBC. Thanks for adding the automated test as well, @indigoxela! As a side comment, unrelated to this PR: it's too bad that vocabularies can't be "extended" easily in Backdrop. In D7, vocabularies were fieldable, or you could even add a column to their table and use hook_schema_alter() to add properties if needed - the controller would take care of saving that new property to its column automatically. I guess the best way for contrib to extend vocabs in Backdrop is to use a separate table or json configuration file to store the extensions, and use the taxonomy_vocabulary hooks to attach the new properties when loading the vocabulary, and save the new properties when saving it. |
@argiepiano many thanks for reviewing. 👍 Yes, compared to D7 the vocabularies feel a bit reduced in flexibility. But I think, that still meets more than 80% of use-cases. And extending is still possible (i18n_taxonomy does by hook_schema_alter() and grabbing info from the extended config directly). |
By @indigoxela, @argiepiano, @kiamlaluno, and @herbdool.
By @indigoxela, @argiepiano, @kiamlaluno, and @herbdool.
Thanks folks! Great discussion and implementation. I merged backdrop/backdrop#4497 into 1.x and 1.25.x. |
Description of the bug
EDIT: based on the discussion in this thread, we decided to allow dynamic properties for vocabularies.
In #5927 we added basic PHP 8.2 support for core, including creating vocabularies. To prevent all sorts of form items to get into the newly created TaxonomyVocabulary object, we check if the property exists in function taxonomy_form_vocabulary_submit(). However, that seems to be the wrong spot.
What about TaxonomyVocabulary::__construct() or taxonomy_vocabulary_load() to prevent nagging when loading a vocab?
I discovered that when checking compatibility with 8.2 in the i18n module, which adds items to the vocab form, which get saved to the config json. So every time a new instance of TaxonomyVocabulary gets created, those config items are also "stuffed" into the object. And that causes PHP nagging, of course.
It might eventually be necessary to allow dynamic properties for TaxonomyVocabulary for other contrib, but not for i18n. This is something to be determined. A quick check via GitHub search found no instance of hook_taxonomy_vocabulary_load in any contrib.
Additional information
Add any other information that could help, such as:
The text was updated successfully, but these errors were encountered: