Critical because it blocks a fully functioning form builder for D7 (core or contrib).

Almost all the core modules that affect the node form also need to be
updated so that their forms are separate from their implementations of
hook_form_alter(). Form builder needs to be able to render these
fields (such as Menu settings, Book settings, Taxonomy vocabularies,
etc.) separately from their parent hook_form_alter() implementations,
so this usually just means copying the existing form to a separate
function. This task can be done separately from all the other work on
Form Builder. There are dozens of examples and TODOs marked in the
current CVS code for Form Builder in each modules example
implementations (see the taxonomy.inc, menu.inc, upload.inc, node.inc,
etc.)

As an example, take path_form_alter() http://api.drupal.org/api/function/path_form_alter/7

copy and paste the code from there into a helper function, and just have path_form_alter() use that function rather than doing all the work itself.

The path example is also necessary for minor usability patches like #476290: Add path alias to taxonomy term edit so makes sense in itself.

Comments

damien tournoud’s picture

I believe that we should go one step further, and make all those elements form_altered into the node form into proper FAPI #types.

gábor hojtsy’s picture

Maybe not all cases would be logical as their own form elements, would they? Do we have a good list?

catch’s picture

Taxonomy vocabularies we should just move into a helper function on the assumption that #491190: Provide a taxonomy term field type and #412518: Convert taxonomy_node_* related code to use field API + upgrade path get in.

Upload similarly because we have #391330: File Field for Core.

Path, menu and book probably make sense as elements - can't see those turning into fields any time soon, and at least menu and path are things which can/should appear in other places.

Can't think of any others which affect the node form off the top of my head.

seutje’s picture

Quick list of form_alter functions in core that affect the node form:

so I understand we're not touching the hook_form_node_type_form_alter right now, which are

and what should these helper functions be called? modulename_node_form_element?

gábor hojtsy’s picture

seutje’s picture

StatusFileSize
new2.91 KB

ok, how's this?

not sure if the Adds the book fieldset to the node form. is a proper description for this

teezee’s picture

StatusFileSize
new6.13 KB

Moved contents of comment_form_alter() for extending node forms to separate function comment_node_form_element().
Will that do?

teezee’s picture

StatusFileSize
new5.96 KB

Moved code in menu_form_alter() to separate function menu_node_form_element().

seutje’s picture

StatusFileSize
new2.08 KB

Moved code in locale_form_alter() to separate function locale_node_form_element()

noticed that locale_form_alter takes $form_state as reference, I left it as it was, but I'm still unclear on why -> #503892: hook_form_alter() and hook_form_FORM_ID_alter() take $form_state as reference

seutje’s picture

StatusFileSize
new3.12 KB

Moved code in path_form_alter() to separate function path_node_form_element()

seutje’s picture

StatusFileSize
new5.5 KB

Moved code in translation_form_alter() to separate function translate_node_form_element()

same weirdness as described in [#503829]

teezee’s picture

StatusFileSize
new6.92 KB

Moved most of the code in taxonomy_form_alter() to (new function) taxonomy_node_form_element().

seutje’s picture

StatusFileSize
new5.47 KB

Moved the node form related code to separate function upload_node_form_element() and noticed that the node_type form code wasn't extracted to the upload_form_node_type_form_alter() yet, so I also did that as per #287178: Use hook_form_FORM_ID_alter() where possible and added some doxygen comments

seutje’s picture

Issue tags: +d7uxsprint
catch’s picture

Status: Active » Needs work

Could one of you roll these into a single patch?

cweagans’s picture

StatusFileSize
new52.98 KB

Attached.

btw: patches should be made from the root of your Drupal tree -- not from the module folder that you are working on. A number of these were made from the modules/___ directory.

cweagans’s picture

Status: Needs work » Needs review

Woops.

catch’s picture

Status: Needs review » Needs work

This patch looks like it introduces a new hook - hook_node_form_element() - but all these functions are called directly from hook_form_alter(), it's not a new hook at all.

That makes me a bit worried about the standardized naming as well in case it looks too much like a hook, probably it's OK though.

seutje’s picture

I understood that these will eventually be reusable hooks, but (at least right now) they also need to be called from the regular hook_form_alter, because the hook isn't invoked yet, but could easily be done

personally, I'm not that excited about the naming either, mainly because the name suggests that it only adds 1 element, where most core modules add more than 1 element so maybe hook_node_form_elements() would be better than hook_node_form_element()

damien tournoud’s picture

I think this is still pretty much important, but I'm not sure about the way forward. I would like to make those proper form elements, so that you can do:

 function comment_form_alter(&$form, $form_state, $form_id) {
   if (!empty($form['#node_edit_form'])) {
    // Add elements to the node form.
    $form['comment_settings'] = array(
      '#type' => 'comment_node_settings',
      '#node' => $node,
      '#weight' => 30,
      '#access' => user_access('administer comments'),
    );
  }
}

Those elements would be expanded with a proper #process function and could be used as-is by the form builder. What do everyone think?

sun.core’s picture

Version: 7.x-dev » 8.x-dev
Category: bug » task

We definitely need a longer API clean-up phase for D8.

catch’s picture

Priority: Critical » Major

Downgrading all D8 criticals to major per http://drupal.org/node/45111

sun’s picture

Component: base system » node system

How much of this issue/patch is still relevant today?

xjm’s picture

Okay, looking at the form alters listed in #4 and in most cases locating their D7 equivalents:

However, it's not clear to me why this is necessary. Clearly we have a working form builder in D7, contrary to the original post. Can anyone clarify?

berdir’s picture

Yes, I don't understand either why this is necessary.

@sun, can you clarify?

My guess is that you are referring to http://drupal.org/project/form_builder?

xjm’s picture

Priority: Major » Normal
Issue tags: -Novice, -d7uxsprint +Needs issue summary update

Berdir and I agree that this is at most a nice refactor for reusability and code cleanliness, but it really doesn't seem to be major. Downgrading and tagging for a summary so we can get something more accurate than "Forms won't work in D7!" which is a bit anachronistic. :)

xjm’s picture

Issue tags: +Novice

Oh, it's still a good novice task though. :)

sun’s picture

Status: Needs work » Closed (won't fix)

Merely refactoring this code into individual functions instead of pure Form API callbacks isn't going to be sufficient and I don't see how that is going to improve the situation in any way. It's also not really clarified in this issue.

Instead, we should have a proper discussion for how to handle those entity form widget additions (of which some aren't really field_extra_fields) into a general pattern - ideally re-using generic (field/property) widgets and formatters, and possibly also upcoming theme components.

Given that form_builder seems to work without this, let's won't fix this issue.