@@ -49,7 +49,23 @@ function cm_tools_module_disable($modules) {
4949}
5050
5151/**
52- * Helper for batch executing a callback across all nodes of a given type.
52+ * Apply a given callback to the entities returned by the given EntityFieldQuery.
53+ *
54+ * This function is designed to work in the context of a Drupal update hook, it
55+ * accepts the sandbox that is passed to the update hook as its first parameter.
56+ * The results of the query, and the state of the iterations are stored in the
57+ * sandbox.
58+ *
59+ * @param $sandbox
60+ * The sandbox passed to the drupal hook hook that this function is called from.
61+ * @param \EntityFieldQuery $query
62+ * The EntityFieldQuery to execute and iterate over the results of.
63+ * @param $callback
64+ * The callback to apply to each entity returned from the $query. The callback is called with two arguments:
65+ * - The entity type of the entity in the second argument
66+ * - The loaded entity.
67+ * @param int $entities_per_pass
68+ * The number of entities to load and process in each batch iteration.
5369 */
5470function cm_tools_update_efq_walk (&$ sandbox , EntityFieldQuery $ query , $ callback , $ entities_per_pass = 10 ) {
5571 // If this is the first pass through this update function then work out the work to be done here..
@@ -82,12 +98,28 @@ function cm_tools_update_efq_walk(&$sandbox, EntityFieldQuery $query, $callback,
8298
8399 // Set the value for finished. If current == total then finished will be 1, signifying we are done.
84100 $ sandbox ['#finished ' ] = $ sandbox ['_cm_tools_update_efq_batch ' ]['current ' ] / $ sandbox ['_cm_tools_update_efq_batch ' ]['total ' ];
85-
86- return NULL ;
87101}
88102
89103/**
90- * Helper for batch executing a callback across all entities of a given type.
104+ * Apply a given callback to all entities of a specific type, and optionally bundle.
105+ *
106+ * This function is designed to work in the context of a Drupal update hook, it
107+ * accepts the sandbox that is passed to the update hook as its first parameter.
108+ * The results of the query, and the state of the iterations are stored in the
109+ * sandbox.
110+ *
111+ * @param $sandbox
112+ * The sandbox passed to the drupal hook hook that this function is called from.
113+ * @param string $entity_type
114+ * The entity type to iterate over.
115+ * @param $callback
116+ * The callback to apply to each entity returned from the $query. The callback is called with two arguments:
117+ * - The entity type of the entity in the second argument
118+ * - The loaded entity.
119+ * @param NULL|string $bundle
120+ * The bundle to iterate over, if NULL then all bundles of the given entity type will be iterated.
121+ * @param int $entities_per_pass
122+ * The number of entities to load and process in each batch iteration.
91123 */
92124function cm_tools_update_entity_walk (&$ sandbox , $ entity_type , $ callback , $ bundle = NULL , $ entities_per_pass = 10 ) {
93125 $ query = new EntityFieldQuery ();
@@ -103,19 +135,51 @@ function cm_tools_update_entity_walk(&$sandbox, $entity_type, $callback, $bundle
103135 call_user_func ($ callback , $ entity );
104136 };
105137
106- return cm_tools_update_efq_walk ($ sandbox , $ query , $ wrapper_callback , $ entities_per_pass );
138+ cm_tools_update_efq_walk ($ sandbox , $ query , $ wrapper_callback , $ entities_per_pass );
107139}
108140
109141/**
110- * Helper for batch executing a callback across all nodes of a given type.
142+ * Apply a given callback to all nodes of a specific type.
143+ *
144+ * This function is designed to work in the context of a Drupal update hook, it
145+ * accepts the sandbox that is passed to the update hook as its first parameter.
146+ * The results of the query, and the state of the iterations are stored in the
147+ * sandbox.
148+ *
149+ * @param $sandbox
150+ * The sandbox passed to the drupal hook hook that this function is called from.
151+ * @param $callback
152+ * The callback to apply to each entity returned from the $query. The callback is called with two arguments:
153+ * - The entity type of the entity in the second argument
154+ * - The loaded entity.
155+ * @param NULL|string $node_type
156+ * The node type to iterate over, if NULL then all nodes will be iterated.
157+ * @param int $nodes_per_pass
158+ * The number of nodes to load and process in each batch iteration.
111159 */
112160function cm_tools_update_node_walk (&$ sandbox , $ callback , $ node_type = NULL , $ nodes_per_pass = 10 ) {
113- return cm_tools_update_entity_walk ($ sandbox , 'node ' , $ callback , $ node_type , $ nodes_per_pass );
161+ cm_tools_update_entity_walk ($ sandbox , 'node ' , $ callback , $ node_type , $ nodes_per_pass );
114162}
115163
116164
117165/**
118- * Helper for batch executing a callback across all nodes of a given type.
166+ * Iterate over a set of entities returned by the given EntityFieldQuery.
167+ *
168+ * This function is designed to work in the context of a Drupal update hook, it
169+ * accepts the sandbox that is passed to the update hook as its first parameter.
170+ * The results of the query, and the state of the iterations are stored in the
171+ * sandbox.
172+ *
173+ * @param $sandbox
174+ * The sandbox passed to the drupal hook hook that this function is called from.
175+ * @param \EntityFieldQuery $query
176+ * The EntityFieldQuery to execute and iterate over the results of.
177+ * @param int $entities_per_pass
178+ * The number of entities to load and process in each batch iteration.
179+ *
180+ * @return
181+ * An associative array of entity types mapped to entities to process in the
182+ * iteration.
119183 */
120184function cm_tools_update_efq (&$ sandbox , EntityFieldQuery $ query , $ entities_per_pass = 10 ) {
121185 $ entities = array ();
@@ -130,7 +194,24 @@ function cm_tools_update_efq(&$sandbox, EntityFieldQuery $query, $entities_per_p
130194}
131195
132196/**
133- * Helper for batch executing a callback across all entities of a given type.
197+ * Iterate over all entities of a specific type, and optionally bundle.
198+ *
199+ * This function is designed to work in the context of a Drupal update hook, it
200+ * accepts the sandbox that is passed to the update hook as its first parameter.
201+ * The results of the query, and the state of the iterations are stored in the
202+ * sandbox.
203+ *
204+ * @param $sandbox
205+ * The sandbox passed to the drupal hook hook that this function is called from.
206+ * @param string $entity_type
207+ * The entity type to iterate over.
208+ * @param NULL|string $bundle
209+ * The bundle to iterate over, if NULL then all bundles of the given entity type will be iterated.
210+ * @param int $entities_per_pass
211+ * The number of entities to load and process in each batch iteration.
212+ *
213+ * @return
214+ * An array of entities to process in this iteration.
134215 */
135216function cm_tools_update_entity (&$ sandbox , $ entity_type , $ bundle = NULL , $ entities_per_pass = 10 ) {
136217 $ entities = array ();
@@ -145,14 +226,57 @@ function cm_tools_update_entity(&$sandbox, $entity_type, $bundle = NULL, $entiti
145226}
146227
147228/**
148- * Helper for batch executing a callback across all nodes of a given type.
229+ * Iterate over all nodes of a specific type.
230+ *
231+ * This function is designed to work in the context of a Drupal update hook, it
232+ * accepts the sandbox that is passed to the update hook as its first parameter.
233+ * The results of the query, and the state of the iterations are stored in the
234+ * sandbox.
235+ *
236+ * @param $sandbox
237+ * The sandbox passed to the drupal hook hook that this function is called from.
238+ * @param NULL|string $node_type
239+ * The node type to iterate over, if NULL then all nodes will be iterated.
240+ * @param int $nodes_per_pass
241+ * The number of nodes to load and process in each batch iteration.
242+ *
243+ * @return
244+ * An array of nodes to process in this iteration.
149245 */
150246function cm_tools_update_node (&$ sandbox , $ node_type = NULL , $ nodes_per_pass = 10 ) {
151247 return cm_tools_update_entity ($ sandbox , 'node ' , $ node_type , $ nodes_per_pass );
152248}
153249
154250/**
155- * Helper for batch executing a callback across all nodes of a given type.
251+ * Fill in missing default field values on entities.
252+ *
253+ * When adding a new field to an existing entity type, instances of those
254+ * entities will be missing values for that field. Drupal does not ensure that
255+ * when loaded they get the default, and they won't be able to be queried etc.
256+ *
257+ * Entities processed by this function will have the default value set
258+ * according to the field configuration, so will match the defaults of newly
259+ * created entities.
260+ *
261+ * This function is designed to work in the context of a Drupal update hook, it
262+ * accepts the sandbox that is passed to the update hook as its first parameter.
263+ * The results of the query, and the state of the iterations are stored in the
264+ * sandbox.
265+ *
266+ * This function requires the Entity API module to be enabled.
267+ *
268+ * @param $sandbox
269+ * The sandbox passed to the drupal hook hook that this function is called from.
270+ * @param string $entity_type
271+ * The entity type to iterate over.
272+ * @param string $bundle
273+ * The bundle to iterate over.
274+ * @param $fields
275+ * An associative array of fields to set the default value for. Keys are the field names, and values are either scalar (and then ignored) or an array of language codes to check and set the default values for.
276+ * @param int $entities_per_pass
277+ * The number of entities to load and process in each batch iteration.
278+ *
279+ * @throws \DrupalUpdateException
156280 */
157281function cm_tools_update_missing_default_field_values_insert (&$ sandbox , $ entity_type , $ bundle , $ fields , $ entites_per_pass = 10 ) {
158282
0 commit comments