Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Issue tags: +Novice

What this issue is about: we now have standards (see link above) for documenting callback function signatures.

So what we need to do is look at the Batch API functions in Drupal 8 and 7, and see if they require the caller to pass in functions with a particular signature; if so, define them as callback_batch_foo() in system.api.php and then amend the batch function documentation to refer to them (see standards link above).

Might be a good novice project?

joachim’s picture

Given batch_set() lives in form.inc, this should logically go in the form.api.php file that is about to be created by the issue on documenting formAPI callbacks.

Here's a fun problem: callback_batch_process() has a variable number of params, but the variability is at the *start*: no matter what params are defined, &$context is always the *last* param.

How should that be represented in the @param items?

jhodgdon’s picture

You can do something like:

@param ...
   Additional parameters specific to the batch.
@param $context
joachim’s picture

Ok, will do. I'll work on a patch for this once #2094145: document Form API #process callback in form.api.php is in and the form.api.php file exists.

joachim’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
4.73 KB

The patch for form #process callbacks is just taking forever, so here's a patch which clashes with it :p

I can reroll the patch at #2094145: document Form API #process callback in form.api.php when this gets in.

I'm also not going to go round updating all the instances of this callback. My experience at #2094145: document Form API #process callback in form.api.php shows that this is much better left for a follow-up.

Status: Needs review » Needs work

The last submitted patch, 5: 1948566.drupal.batch-api-callbacks-docs.patch, failed testing.

joachim’s picture

Status: Needs work » Needs review

Setting back to needs review -- api.php files shouldn't be getting parsed!

jhodgdon’s picture

Um, yes. They need to have valid PHP in them, or the API module will not be able to parse them.

joachim’s picture

Ah. I didn't realise the grammar parser parsed it by interpreting the PHP.

In which case, this line is a problem:

+++ b/core/modules/system/form.api.php
@@ -0,0 +1,112 @@
+ */
+function callback_batch_operation(..., &$context) {
+  if (!isset($context['sandbox']['progress'])) {

How should this be handled?

jhodgdon’s picture

How about something like this:

/**
...
 * @param $params
 *   Each specific callback can have zero or more parameters specific to
 *   the operation. These are specified in the array passed to batch_set(), and
 *   in the example function below, are represented by $params, although it
 *   could be any number of individual parameters.
...
function callback_batch_operation($params, &$context) {
jhodgdon’s picture

Status: Needs review » Needs work
joachim’s picture

Status: Needs work » Needs review
FileSize
4.75 KB

Finally got round to this.

I've called it $MULTIPLE_PARAMS in capitals to make it dead clear that it's not just a normal parameter called $params!

jhodgdon’s picture

Status: Needs review » Needs work

I think that is a reasonable way to document this function.

So... This is nearly ready to go. A few things I noticed:

a) Our callback standards:
https://drupal.org/node/1354#callback-def
say we're supposed to have a line like

 *
* Callback for batch_set().

in the callback function docs. This is missing from both of the callbacks.

b)

+ *     be called again, and execution passes to the next operation or the
+ *     finished callback. Any other value causes this operation to be called

"finished callback"... Should that be callback_batch_finished()? or at least be in quotes or something?

c)

+ *     finished callback. Any other value causes this operation to be called
+ *     again, however it should be noted that the value set here does not

before "however" it should be ; not ,

d)

 *   - 'sandbox': This may be used by operations to persist data between
+ *     successive calls to the current operation. For example, an operation may
+ *     which to store a pointer in a file or an offset for a large query. This
+ *     array key is not initially set when this callback is first called, which makes it useful for determining
+ *     whether it is the first run or not:

Um... Something got left out here? And that 2nd to last line is too long. Actually, the whole sandbox section doesn't make much sense to me.

e) callback_batch_finished() has no @param or @return docs. It may not need @return, but does have several parameters.

f) Doesn't the batch_set() documentation need to reference these new callback definitions?

joachim’s picture

Status: Needs work » Needs review
FileSize
8.16 KB

I think this fixes everything.

> Actually, the whole sandbox section doesn't make much sense to me.

I've added a bit more about it. Hope that helps.

jhodgdon’s picture

Much better! I like all of the documentation, and I think the examples are very clear. The only thing that I wondered about was in the operations example, whether it would be clear that $option1 and $option2 local variables were supposed to be part of the parameters? Maybe a code comment at the top explaining this would be helpful?

Anyway, I guess we could commit this as it is... the only other thing that might be helpful though would be to update documentation for actual operations/finished callbacks in Core to reference this documentation. Which you can find by looking at api.drupal.org for who calls batch_set(). I wouldn't document the ones used it tests (kind of pointless) but maybe the others? Or we can just leave that out. Thoughts?

joachim’s picture

> update documentation for actual operations/finished callbacks in Core to reference this documentation. Which you can find by looking at api.drupal.org for who calls batch_set()

That's definitely something that needs doing, but I would really like to leave that as a follow-up. Doing it all in one go on the form #process callback patch has really burnt me out :(

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Fair enough. Let's just get this in then.

jhodgdon’s picture

There's an "avoid commit conflicts" issue touching form.inc
#1996238: Replace hook_library_info() by *.libraries.yml file
so I'm going to be extra-cautious and wait to commit this until that's resolved, even though I am pretty sure there isn't a direct conflict with this patch.

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

I got leave from the other issue person to commit this, so it's committed to 8.x. The patch also worked OK for 7.x, so I committed it there too. Thanks again!

joachim’s picture

Brilliant, thanks!

I've filed the promised follow-up: #2195183: document Batch API callbacks as callback implementations.

(And of course now that I see the actual text on api.d.o I can see ways to improve it, but that can wait... :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.