API page: https://api.drupal.org/api/drupal/includes%21form.inc/function/batch_set/7
I have also made a comment for other developers in the interim that the queue definition can be overridden per $batch_definition.
Description of Issue
The associative array used for $batch_definition documented above is missing one optional element , queue. I discovered that you can override the class used for the queue which is set in the _batch_populate_queue function by defining the class and name of the queue in the $batch_definition.
For example:
$batch = array(
// Standard batch definition elements.
// Overriding the queue class and name.
'queue' => array(
'name' => 'my_custom_batch_queue',
'class' => 'MyCustomBatchMemoryQueue',
)
);
It is important to note that both the name and class elements of the queue definition must be set. It is important that the overriding class is derived from either BatchMemoryQueue OR BatchQueue.
However, I believe you could:
- Implement a class which is of type DrupalReliableQueueInterface (progressive) OR DrupalQueueInterface (non-progressive).
- The implemented Queue class derived from an interface must also implement the method getAllItems() which returns the remaining items in the queue.
This was discovered through the updates in the InstallKit installation profile. The problem that was faced was that the custom batch operations where relatively difficult to "alter" through the use of alter hooks the batch operations due to numerical key indices instead of string identified indices found in an associative array. An example of how overriding the queue element was used can be found here.
| Comment | File | Size | Author |
|---|---|---|---|
| #35 | batch_api_overriding-2296445-33.patch | 1.34 KB | prajaankit |
| #31 | batch_api_overriding-2296445-31.patch | 1.34 KB | Anonymous (not verified) |
| #31 | interdiff-29-31.txt | 737 bytes | Anonymous (not verified) |
| #29 | batch_api_overriding-2296445-29.patch | 1.33 KB | Anonymous (not verified) |
| #29 | interdiff-27-29.txt | 1.26 KB | Anonymous (not verified) |
Comments
Comment #1
jhodgdonOK, thanks for this issue! I have read through the code and agree with what you are saying here, for Drupal 7.
But the situation is pretty much the same in Drupal 8. In Drupal 8, here is what we need to document, on batch_set():
a) $batch_definition can contain an element 'queue', which is an array containing 'name' and 'class' elements (both must be present).
b) 'name' is a unique identifier for the queue.
c) 'class' is the name of a class that implements \Drupal\Core\Queue\QueueInterface, and it must have a constructor with two arguments: $name and a \Drupal\Core\Database\Connection object. Typically, the class will either be 'Drupal\Core\Queue\Batch' or 'Drupal\Core\Queue\BatchMemory'. The class name must include the namespace but not start with a backslash.
For Drupal 7, the only thing that changes is (c):
'class' is the name of a class that implements DrupalQueueInterface; typically, this would be 'BatchQueue' or 'BatchMemoryQueue'. The constructor just takes $name, and the class name is not namespaced, so the class must be loaded or auto-loadable (the class file needs to be in the files section of the module.info file).
Comment #2
amcgowanca commentedHi @jhodgdon,
Thanks for pointing out the D8 related item, I had not even thought about checking D8's implementation prior to posting or else I would have included that information as well.
Aaron
Comment #3
roderikAwesome. Tagging for Amsterdam.
I think that since jhodgdon laid it out so neatly, one sprinter can implement the function documentation and another can review.
But I'd be happy to 2nd-review it on site or hear your ideas, if necessary. Ping me in that case.
Also feel free to upload D7 patch in addition :) (named -D7.patch as per https://www.drupal.org/patch/submit, don't change version/status until committed)
Comment #4
blazey commentedAttaching patch. Changes are based on issue description and comments.
Comment #5
tom_ek commentedPatch applies without problems and does precisely what it is required to do - changes the comments as stated in the summary. Marking as RTBC
Comment #6
jhodgdonThanks! This patch needs a bit of work:
a) We do not have *....* in documentation anywhere else:
Just leave out the * * please. Also instead of "items" how about "elements"?
b) list formatting:
- Line before a list should end in :
- Each list item (after the : if there is one) should start in a capital letter and end in .
See https://www.drupal.org/node/1354#lists
c)
Do not enclose class names in ' and the namespace should start with \ in documentation.
d)
This example is probably not necessary. But if you want to include an example, please precede it by a line saying something like "For example:" and also since the instructions say to include the namespace, how about putting one in the example? In d8 all classes have namespaces.
Comment #7
Palashvijay4O commentedComment #8
Palashvijay4O commentedSubmitting a patch .
Comment #9
roderikMaybe I shouldn't get in here too, but I saw a superfluous single quote and the first line does not end in ':' as jhodgdon requested.
Some of the wording of patch #4 (at least partially) restored.
Comment #11
Palashvijay4O commentedAdding a colon (:).
Comment #12
Palashvijay4O commentedComment #14
jhodgdonThis patch is getting pretty close!
There is still an extra ' on the 6th line.
Also... I think the part about the class is a bit confusing. Maybe in the first line it should say "The name of a class that implements \Drupal\Core\Queue\QueueInterface" and the part about the namespace and backslash should be right after that? Because as it is, when I got to that last sentence, I was pretty confused about what it meant. So how about: "The name of a class that implements \Drupal\Core\Queue\QueueInterface, including the full namespace but not starting with a backslash." Then continue with the part about the constructor and typical classes.
Also... In batch_set() above this patch, it says that all of these elements are optional unless indicated. So it would be useful here to say what the default queue is, if you don't override it... So I guess in the first line of this patch, it would be good to say something like:
- queue: An override of the default queue. An array with two elements:
Comment #15
Palashvijay4O commentedRe-rolling the patch with changes.
Comment #16
Palashvijay4O commentedComment #17
jhodgdonOK, this looks good!
A couple more things to fix:
a) What is the default queue? That would be helpful to say.
b) Formatting -- these lines need rewrapping:
Each line should be as close to 80 characters as possible without going over.
Comment #18
Palashvijay4O commentedRe-rolling with changes .
Comment #19
Palashvijay4O commentedComment #20
jhodgdonThe line wrapping is closer but not really ideal... there are two lines over 80 characters, and the line ending in "namespace" and the next one could both have another word or two on them.
Also, we still don't have any indication of what the default queue is.
Comment #21
jhodgdonWow, having more discussions with Palashvijay4 in IRC. This is definitely not a Novice issue... I think we've figured out what to document and hopefully there will be a new patch shortly! :)
In short:
- There's another element called 'progressive', which defaults to TRUE. TRUE means "expected to take more than one run" and FALSE means "must finish in one run so this batch can stay in memory".
- The queue class defaults to BatchMemory if progressive is FALSE, and Batch if progressive is TRUE.
Anyway that is a rough idea... hopefully we'll have a new patch!
Comment #22
jhodgdon@Palashvijay4O - if you have a question about this issue, please ask it here!
Comment #23
Anonymous (not verified) commentedIt's been a while, so unassigning.
Rerolled. Fixed the wrapping as proposed in #20, and added comments based on the feedback from #21.
Comment #24
jhodgdonLooks pretty good! A few things to fix:
a) We should talk of a "Boolean" and "TRUE" and "FALSE" in documentation. Not "boolean", "true", "True", etc. (capitalization).
b)
The first line says name/class are optional, but in the list it says they are required. They cannot both be correct! Maybe neither is? According to the text below, it looks like class is optional because it has defaults... and if you look at
https://api.drupal.org/api/drupal/core!includes!form.inc/function/_batch...
you can see that both of these are optional. So they should not be marked (required).
c)
Those last two lines should be wrapped into the previous bullet item. And the part about BatchMemory doesn't quite make sense, should say "... or to BatchMemory if progressive is TRUE" I think?
d) In (c) we see that progressive has a default. So in the list above we should document that default.
Comment #25
pushpinderchauhan commentedIncorporated changes as per Comment #24, please review.
Comment #26
jhodgdonThanks for the new patch! A few comments:
(default) is not quite right. That should be removed, and instead where it says "FALSE indicates..." make it say "FALSE(default) indicates..."
The part about "Defaults to" should be wrapped with the rest of the part about the class, not separated. It's about the defaults for the class piece. And I would leave out the (default) in this area, since that is indicated up above with progressive's documentation.
Comment #27
pushpinderchauhan commentedPlease review, updated as per last suggestion.
Comment #28
jhodgdonThanks! Mostly there, a few little issues:
Line passes 80 characters.
Wondering if this is backwards? Yes, it is -- here is what _batch_populate_queue() does:
Comment #29
Anonymous (not verified) commentedFixed the feedback from #28.
Comment #30
jhodgdonTotal nitpick (sorry!): probably need a space before (default) here.
Rest looks perfect!
Comment #31
Anonymous (not verified) commentedFair enough :)
Comment #32
jhodgdonGreat, thanks! [Docs only, unfrozen in beta]
Comment #33
prajaankit commentedComment #34
jhodgdonThe patch in #31 was correct, not the patch in #33, which has a formatting problem.
Comment #35
prajaankit commentedComment #37
jhodgdon@prajaankat: This issue was fine with the patch in #31. I am confused as to why you assigned it to yourself and uploaded new patches. Please find a different issue to work on that actually needs a new patch, thanks!
I committed the patch in #31 to 8.0.x (Docs patches are not frozen in Beta).