r/programming • • Jan 05 '11

Drupal 7 released.

http://drupal.org/drupal-7.0
222 Upvotes

259 comments sorted by

View all comments

Show parent comments

3

u/UnConeD Jan 06 '11

The problem is really the node_form and how it shapes the code around it. The idea is that you take the entire $node, you turn it into a $form structure with fields, which is then shown to the user. Then the submitted form values are serialized back into a new $node (which is made secure through Form API access control on fields).

This is dumb, because it elevates the form fields from just a UI implementation aspect into meaningful structure. It also means you need a comprehensive bidirectional translation scheme between $node and $form, which in practice means people get lazy and make forms that look like airplane cockpits.

But where it gets really problematic is that many contrib modules (out of ignorance) don't even bother translating back from $form to $node, and hardcore their node_save() callbacks to accept only the $form values. Passing in a vanilla $node will simply not work and result in data loss, aggravated by silent failure and those crazy type casts PHP is famous for.

I actually made a flowchart of this process for Drupal 6 to explain this to a coworker: http://acko.net/dumpx/why-drupal-nodes-suck.pdf

As far as I'm aware, the only way this has changed in 7 is that the $form fields are now automagically combined with the original $node, so that values not present in the $form are not lost. But otherwise, module authors are still expected to grasp MVC-related concepts in a system that has neither the vocabulary nor the grammar to express these ideas fluently.

5

u/mikey_p5151 Jan 06 '11 edited Jan 06 '11

While this does perfectly describe the problems with previous versions of Drupal, I think quite a bit has changed since you have been around and committing. Any entity that properly implements field_attach_* hooks has a consistent data structure that can be loaded, modified in place, and saved without concern over whether the $form fields match the structure that the saving function (and ultimately field_attach_insert and field_attach_update) expect. It just works. Seamlessly.

As to mapping form field to entity fields, of course that will never match and any system that claims to integrate those items ends up being what Drupal was, not what it is now. field_attach_form and field_attach_form_submit are what glues these two items in a default way, but keeps the data structure of the object 100% separate from the values of the form itself. I'd suggest anyone that has fought this code in the past, write their own entity module that is fieldable, and use these functions to understand how they actually work and how the data structures work.

But where it gets really problematic is that many contrib modules (out of ignorance) don't even bother translating back from $form to $node, and hardcore their node_save() callbacks to accept only the $form values.

This is just silly. We aren't talking about 4.7 and 5.x contrib modules. We're talking about Drupal core, and it's changed alot. Not to mention nothing prevented users from writing Drupal 5.x or 6.x modules that DID NOT have this problem. Project module (for all it's cruft) actually does an awesome job of putting it's metadata in consistent, repeatable, and sane places that work whether it's getting a form value or a $node object.

Nothing is stopping (other than lack of time and motivation) fixing the node preview issue once and for all, the object I get back from field_attach_form_submit() has all the fields in the exact same place they are when they are returned by entity_load() and field_attach_load().

6

u/UnConeD Jan 06 '11

I've actually dug through quite a lot of D7 code. And noticed all the "Fix this in D8" comments that show the issues I've raised are still there.

Besides, you're focusing on the minor complaints and ignoring the big ones I started out with. But since you insist, take a look at node.pages.inc:90-99 ($node being stateful, in direct contradiction with how Form API is designed) and session.inc:106-127 (global $user is a fake) and tell me these are the hallmarks of a well-designed system. None of these things matter for vanilla stuff, it only comes out when you try to push the system into more advanced ways of interaction. You know, doing experimental development, pushing the edge, what Drupal used to pride itself on?

As for core vs contrib...as the rest of this thread shows, you can't look at Drupal core in isolation. In practice, the vast majority of people do rely on contrib modules, and Drupal.org does not make any effort to suggest doing otherwise. It is dishonest to pretend that the ultra strict principles of Drupal core, which only arise as a result of a strict vetting process, will spontaneously radiate outward to contrib modules and the sites that use them. It has never happened, and it won't change any time soon.

As for your accusations about "poor taste"... I'm posting technical arguments in response to a direct challenge about Drupal weaknesses, on a site that isn't Drupal.org. If that somehow offends you, tough luck.

1

u/mikey_p5151 Jan 06 '11

I should back up a bit and acknowledge that Node module is mess with it's revision handling and the fake 'hooks' of hook_insert, etc, that are really just named callbacks. And beyond that user.module is even worse with the fake user global, the abomination that is users.data and so on. Comment.module is even worse, and has led to some of the worse "WTF" moments I've ever seen in Drupal code.

Despite this, I do feel that serious changes are being made that facilitate good, clean separation between in new code (I've been working on new modules that define their own entities, and it's about at least 100x cleaner than node.module so far). This is the type of change that shows that Drupal is (slowly) moving in the right direction. While I didn't work personally on any of the issues you mentioned for Drupal 7, I can see that there is the possibility and groundwork laid for fixing them in a proper way in the future.

As far as core vs. contrib, I guess on some level I understand that frustration with crappy contrib, and there sure is plenty of it, but I don't see that Drupal core itself is responsible for it. In some ways with the node hooks sure, but if I want to define my own objects, write my HTML and form, and write my own SQL, nothing is stopping me. For a long time Views has been an excellent example of this, (the editing and saving of a view, not the function of views itself) separating CRUD from forms very well, and introducing exportables to the mix as well.

2

u/[deleted] Jan 06 '11

Thanks for the awesome flow chart and info. I've got to ask about one more thing you've brought up. I've long suspected the global $user object to be a bit, well, suspect. Though it hasn't bitten me in the ass yet, I feel like I'm just waiting for the other show to drop as our program gets more and more complex. Could you elaborate on any known problems or is there anything I should keep in mind when coding to save myself some sanity in regards to $user in the future?

2

u/silvercircle Jan 06 '11

Pre-Drupal-7, global $user is not fully loaded. See the description here

Changed in http://drupal.org/node/347250#comment-1357142

2

u/[deleted] Jan 06 '11

Much appreciated. So Drupal 7 introduces a robust caching system for $user kind of like with $node now?

2

u/davereid20 Jan 06 '11

That issue has nothing to do with the global $user object. It's still not a full-loaded user.

2

u/chu Jan 07 '11 edited Jan 07 '11

But otherwise, module authors are still expected to grasp MVC-related concepts in a system that has neither the vocabulary nor the grammar to express these ideas fluently.

It's no problem to do that kind of pseudo-MVC (as used in most web frameworks) in Drupal as it's effectively Hierarchical MVC/PAC in any case. Rich models containing all business rules implemented OO in include(s) with a clean API (able to use complex data structures, unlike being tied to an ActiveRecord-based framework), module/menu as controller/router, template files as view - job done. Nice part is that you get AOP baked right through into the bargain. Architecturally it's a hybrid but that doesn't make it a bad design.

1

u/fago Jan 06 '11

I'm with you, the node form workflow was a mess. However, the good news is that for d7 it got fixed to actually work as you describe in the pdf it should! :)