API page: http://api.drupal.org/api/drupal/core%21includes%21form.inc/function/dru...

The documentation page, right before "Related topics," shows the following text:

would be called via drupal_form_submit() as follows:

  $form_state['values'] = $my_form_values;
  $form_state['build_info']['args'] = array(&$object);
  drupal_form_submit('mymodule_form', $form_state);

For example:

After the last line, I guess there should be an example, but the page doesn't show any example. It seems the example has been given, and the page should contain the following text.

would be called via drupal_form_submit() as follows, for example:

  $form_state['values'] = $my_form_values;
  $form_state['build_info']['args'] = array(&$object);
  drupal_form_submit('mymodule_form', $form_state);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

longwave’s picture

The missing example appears to be the "register a new user" code block, which instead appears before Parameters.

apaderno’s picture

It seems not. The topic in the last snippet shown in that page is "passing arguments that need to be passed by reference to the menu callback." This is the context.

Arguments that need to be passed by reference should not be included here, but rather placed directly in the $form_state build info array so that the reference can be preserved. For example, a form builder function with the following signature:

  function mymodule_form($form, &$form_state, &$object) {
  }

would be called via drupal_form_submit() as follows:

  $form_state['values'] = $my_form_values;
  $form_state['build_info']['args'] = array(&$object);
  drupal_form_submit('mymodule_form', $form_state);
apaderno’s picture

Effectively, it seems a problem with how the part about the parameters is extracted from the comment.

/**
 * @param $form_id
 * …
 * @param ...
 *   Any additional arguments are passed on to the functions called by
 *   drupal_form_submit(), including the unique form constructor function.
 *   For example, the node_edit form requires that a node object be passed
 *   in here when it is called. Arguments that need to be passed by reference
 *   should not be included here, but rather placed directly in the $form_state
 *   build info array so that the reference can be preserved. For example, a
 *   form builder function with the following signature:
 *   @code
 *   function mymodule_form($form, &$form_state, &$object) {
 *   }
 *   @endcode
 *   would be called via drupal_form_submit() as follows:
 *   @code
 *   $form_state['values'] = $my_form_values;
 *   $form_state['build_info']['args'] = array(&$object);
 *   drupal_form_submit('mymodule_form', $form_state);
 *   @endcode
 * For example:
 * @code
 * // register a new user
 * $form_state = array();
 * $form_state['values']['name'] = 'robo-user';
 * $form_state['values']['mail'] = 'robouser@example.com';
 * $form_state['values']['pass']['pass1'] = 'password';
 * $form_state['values']['pass']['pass2'] = 'password';
 * $form_state['values']['op'] = t('Create new account');
 * drupal_form_submit('user_register_form', $form_state);
 * @endcode
 */

The "For example:" line is considered part of the @param block, while it is not. To resolve the issue, it should be enough to move the example code before the @param block.

apaderno’s picture

Status: Active » Needs review
FileSize
1.74 KB

This is a first tentative to resolve the issue.

apaderno’s picture

As alternative, the example code is simply moved.

jhodgdon’s picture

Status: Needs review » Needs work

Please do not put the "do not test" suffix on patches that you think might need to be committed to core. The maintainers (speaking for myself anyway) who make commits like to have all patches tested, just to make sure there isn't a syntax error that has crept into the patch. Only use "do not test" if you are attaching an invalid patch (such as a 7.x or 6.x patch on an 8.x issue).

Anyway, that aside... The patch in #5 -- I don't like it, because it just seems to put some code into the documentation with no explanation.

The patch in #4 is also not quite right -- there shouldn't be a blank line between "For example..." and the @code that provides that example. Instead, that "for example" line should end in : and the blank line should be removed.

But... I'm confused about this anyway. There is already one example in the documentation. Why do we even need this second example? Choose one or the other, whichever provides the best example.

apaderno’s picture

It's not a different example: It's the same example that is moved to solve an issue with the rendering.

The comment for that function is the following one.

/**
 * Retrieves, populates, and processes a form.
 *
 * This function allows you to supply values for form elements and submit a
 * form for processing. Compare to drupal_get_form(), which also builds and
 * processes a form, but does not allow you to supply values.
 *
 * There is no return value, but you can check to see if there are errors
 * by calling form_get_errors().
 *
 * @param $form_id
 *   The unique string identifying the desired form. If a function
 *   with that name exists, it is called to build the form array.
 *   Modules that need to generate the same form (or very similar forms)
 *   using different $form_ids can implement hook_forms(), which maps
 *   different $form_id values to the proper form constructor function. Examples
 *   may be found in node_forms() and search_forms().
 * @param $form_state
 *   A keyed array containing the current state of the form. Most important is
 *   the $form_state['values'] collection, a tree of data used to simulate the
 *   incoming $_POST information from a user's form submission. If a key is not
 *   filled in $form_state['values'], then the default value of the respective
 *   element is used. To submit an unchecked checkbox or other control that
 *   browsers submit by not having a $_POST entry, include the key, but set the
 *   value to NULL.
 * @param ...
 *   Any additional arguments are passed on to the functions called by
 *   drupal_form_submit(), including the unique form constructor function.
 *   For example, the node_edit form requires that a node object be passed
 *   in here when it is called. Arguments that need to be passed by reference
 *   should not be included here, but rather placed directly in the $form_state
 *   build info array so that the reference can be preserved. For example, a
 *   form builder function with the following signature:
 *   @code
 *   function mymodule_form($form, &$form_state, &$object) {
 *   }
 *   @endcode
 *   would be called via drupal_form_submit() as follows:
 *   @code
 *   $form_state['values'] = $my_form_values;
 *   $form_state['build_info']['args'] = array(&$object);
 *   drupal_form_submit('mymodule_form', $form_state);
 *   @endcode
 * For example:
 * @code
 * // register a new user
 * $form_state = array();
 * $form_state['values']['name'] = 'robo-user';
 * $form_state['values']['mail'] = 'robouser@example.com';
 * $form_state['values']['pass']['pass1'] = 'password';
 * $form_state['values']['pass']['pass2'] = 'password';
 * $form_state['values']['op'] = t('Create new account');
 * drupal_form_submit('user_register_form', $form_state);
 * @endcode
 */

It is rendered as in the following screenshots.

Using "For example:" before the example doesn't seem to make sense, as that phrase would follow the following sentence:

There is no return value, but you can check to see if there are errors by calling form_get_errors().

drupal_static() uses just "Example:", before the examples.

Looking at the comment used for that function, this is what it contains.

/*
 * Example:
 * @code
 * function user_access($string, $account = NULL) {
 *   // Use the advanced drupal_static() pattern, since this is called very often.
 *   static $drupal_static_fast;
 *   if (!isset($drupal_static_fast)) {
 *     $drupal_static_fast['perm'] = &drupal_static(__FUNCTION__);
 *   }
 *   $perm = &$drupal_static_fast['perm'];
 *   ...
 * }
 * @endcode
 *
 * @param $name
 *   Globally unique name for the variable. For a function with only one static,
 *   variable, the function name (e.g. via the PHP magic __FUNCTION__ constant)
 *   is recommended. For a function with multiple static variables add a
 *   distinguishing suffix to the function name for each one.
 */

The examples are before the first @param directive.

apaderno’s picture

Status: Needs work » Needs review
FileSize
1.68 KB
jhodgdon’s picture

What I was trying to say is that there is one example:

*   form builder function with the following signature:
*   @code
*   function mymodule_form($form, &$form_state, &$object) {
*   }
*   @endcode
*   would be called via drupal_form_submit() as follows:
*   @code
*   $form_state['values'] = $my_form_values;
*   $form_state['build_info']['args'] = array(&$object);
*   drupal_form_submit('mymodule_form', $form_state);
*   @endcode

And then there is this other example:

* For example:
* @code
* // register a new user
* $form_state = array();
* $form_state['values']['name'] = 'robo-user';
* $form_state['values']['mail'] = 'robouser@example.com';
* $form_state['values']['pass']['pass1'] = 'password';
* $form_state['values']['pass']['pass2'] = 'password';
* $form_state['values']['op'] = t('Create new account');
* drupal_form_submit('user_register_form', $form_state);
* @endcode

I do not understand why we need both. It doesn't seem to me that the second example adds much of anything to the discussion, and actually I'm not sure the first one does either. We have the "N functions call this one" to provide examples anyway -- normally we don't put them in the function's doc block?

apaderno’s picture

The first example you are referring is about passing arguments that need to be passed by reference; it is not how the function is normally used, and I think it's worth pointing out how the function should be called in that case.
The second example is showing code that is not used in any of the functions calling drupal_form_submit() uses, and that code has a specific purpose.

Most of the Drupal functions are called from Drupal itself. IMO, that doesn't mean the documentation of a function should not evidence something that is relevant for calling the function. Differently, the documentation for drupal_static() should not report the advanced drupal_static() pattern, nor suggest using the PHP magic __FUNCTION__ constant, as that information can be obtained from Drupal code.

jhodgdon’s picture

Status: Needs review » Needs work

OK... Sorry for being dense. I don't always have as much familiarity with the issue as the person who made the patch. :)

So, I applied the patch and looked carefully at the drupal_form_submit() documentation that resulted. I agree with what you're saying about needing both the examples, and I think the patch is mostly OK. I would like to see a few things cleaned up though (when fixing up function docs in a patch, I usually try to get the whole function cleaned up if possible):

a)

 * @code
 * // register a new user
 * $form_state = array();
 

This violates our coding standards for in-line comments (should be sentences).

b) Wrapping in the first @param should be closer to 80 characters... But really, we shouldn't have all that documentation there about hook_forms() -- it isn't all that relevant to drupal_submit_form(). Instead, maybe just say "The unique identifier for the form. See drupal_build_form() for more information.".

c) The ... parameter doc is kind of a mess, and it references a form "node_edit" which (as far as I can tell) does not actually exist in Drupal 7/8. Maybe we could reword it a bit... something like:

Any additional arguments are passed on to the functions called by drupal_form_submit(), including the form constructor function (for example, the node_form() constructor function requires a node object as its third argument). However, if the constructor function has arguments that need to be passed by reference, the arguments must be placed in $form_state['build_array']['args'] instead of passing them to drupal_form_submit() as separate arguments. For example, a form builder function with the following signature:
@code
function mymodule_form($form, &$form_state, &$object) {
}
@endcode
would be called via drupal_form_submit() as follows:
@code
$form_state['values'] = $my_form_values;
$form_state['build_info']['args'] = array(&$object);
drupal_form_submit('mymodule_form', $form_state);
@endcode

jhodgdon’s picture

Bump. This still needs a new patch, issue still exists.

priya.chat’s picture

Assigned: Unassigned » priya.chat
Issue summary: View changes
apaderno’s picture

Version: 8.0.x-dev » 7.x-dev

Drupal 8 doesn't have that function anymore, so I guess it doesn't have that documentation page either. Yet, Drupal 7 has that function, and the documentation page has that "bug."

David_Rothstein’s picture

Title: The documentation for drupal_form_submit() seems truncated » The documentation for drupal_form_submit() / FormBuilderInterface::submitForm() seems truncated
Version: 7.x-dev » 8.0.x-dev
Issue tags: +Needs backport to D7
priya.chat’s picture

Assigned: priya.chat » Unassigned

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

apaderno’s picture

Version: 8.4.x-dev » 8.5.x-dev
Status: Needs work » Needs review
Issue tags: -Needs backport to D7
FileSize
2.19 KB

I remove the reference to the node_edit form, since it doesn't exist a form with that ID, in Drupal 8. I also removed the example using the user registration form.

apaderno’s picture

Title: The documentation for drupal_form_submit() / FormBuilderInterface::submitForm() seems truncated » Remove any reference to not existing forms in FormBuilderInterface::submitForm() documentation
Category: Bug report » Task

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

apaderno’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

manishsaharan’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
162 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Nikhil_110’s picture

Patch Try with 10.1.x

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.