annotate core/includes/form.inc @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Functions for form and batch generation and processing.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Component\Utility\UrlHelper;
Chris@0 9 use Drupal\Core\Render\Element;
Chris@0 10 use Drupal\Core\Render\Element\RenderElement;
Chris@0 11 use Drupal\Core\Template\Attribute;
Chris@0 12 use Drupal\Core\Url;
Chris@0 13 use Symfony\Component\HttpFoundation\RedirectResponse;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Prepares variables for select element templates.
Chris@0 17 *
Chris@0 18 * Default template: select.html.twig.
Chris@0 19 *
Chris@0 20 * It is possible to group options together; to do this, change the format of
Chris@0 21 * $options to an associative array in which the keys are group labels, and the
Chris@0 22 * values are associative arrays in the normal $options format.
Chris@0 23 *
Chris@0 24 * @param $variables
Chris@0 25 * An associative array containing:
Chris@0 26 * - element: An associative array containing the properties of the element.
Chris@0 27 * Properties used: #title, #value, #options, #description, #extra,
Chris@0 28 * #multiple, #required, #name, #attributes, #size.
Chris@0 29 */
Chris@0 30 function template_preprocess_select(&$variables) {
Chris@0 31 $element = $variables['element'];
Chris@0 32 Element::setAttributes($element, ['id', 'name', 'size']);
Chris@0 33 RenderElement::setAttributes($element, ['form-select']);
Chris@0 34
Chris@0 35 $variables['attributes'] = $element['#attributes'];
Chris@0 36 $variables['options'] = form_select_options($element);
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Converts an options form element into a structured array for output.
Chris@0 41 *
Chris@0 42 * This function calls itself recursively to obtain the values for each optgroup
Chris@0 43 * within the list of options and when the function encounters an object with
Chris@0 44 * an 'options' property inside $element['#options'].
Chris@0 45 *
Chris@0 46 * @param array $element
Chris@0 47 * An associative array containing the following key-value pairs:
Chris@0 48 * - #multiple: Optional Boolean indicating if the user may select more than
Chris@0 49 * one item.
Chris@0 50 * - #options: An associative array of options to render as HTML. Each array
Chris@0 51 * value can be a string, an array, or an object with an 'option' property:
Chris@0 52 * - A string or integer key whose value is a translated string is
Chris@0 53 * interpreted as a single HTML option element. Do not use placeholders
Chris@0 54 * that sanitize data: doing so will lead to double-escaping. Note that
Chris@0 55 * the key will be visible in the HTML and could be modified by malicious
Chris@0 56 * users, so don't put sensitive information in it.
Chris@0 57 * - A translated string key whose value is an array indicates a group of
Chris@0 58 * options. The translated string is used as the label attribute for the
Chris@0 59 * optgroup. Do not use placeholders to sanitize data: doing so will lead
Chris@0 60 * to double-escaping. The array should contain the options you wish to
Chris@0 61 * group and should follow the syntax of $element['#options'].
Chris@0 62 * - If the function encounters a string or integer key whose value is an
Chris@0 63 * object with an 'option' property, the key is ignored, the contents of
Chris@0 64 * the option property are interpreted as $element['#options'], and the
Chris@0 65 * resulting HTML is added to the output.
Chris@0 66 * - #value: Optional integer, string, or array representing which option(s)
Chris@0 67 * to pre-select when the list is first displayed. The integer or string
Chris@0 68 * must match the key of an option in the '#options' list. If '#multiple' is
Chris@0 69 * TRUE, this can be an array of integers or strings.
Chris@0 70 * @param array|null $choices
Chris@0 71 * (optional) Either an associative array of options in the same format as
Chris@0 72 * $element['#options'] above, or NULL. This parameter is only used internally
Chris@0 73 * and is not intended to be passed in to the initial function call.
Chris@0 74 *
Chris@0 75 * @return mixed[]
Chris@0 76 * A structured, possibly nested, array of options and optgroups for use in a
Chris@0 77 * select form element.
Chris@0 78 * - label: A translated string whose value is the text of a single HTML
Chris@0 79 * option element, or the label attribute for an optgroup.
Chris@0 80 * - options: Optional, array of options for an optgroup.
Chris@0 81 * - selected: A boolean that indicates whether the option is selected when
Chris@0 82 * rendered.
Chris@0 83 * - type: A string that defines the element type. The value can be 'option'
Chris@0 84 * or 'optgroup'.
Chris@0 85 * - value: A string that contains the value attribute for the option.
Chris@0 86 */
Chris@0 87 function form_select_options($element, $choices = NULL) {
Chris@0 88 if (!isset($choices)) {
Chris@0 89 if (empty($element['#options'])) {
Chris@0 90 return [];
Chris@0 91 }
Chris@0 92 $choices = $element['#options'];
Chris@0 93 }
Chris@0 94 // array_key_exists() accommodates the rare event where $element['#value'] is NULL.
Chris@0 95 // isset() fails in this situation.
Chris@0 96 $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
Chris@0 97 $value_is_array = $value_valid && is_array($element['#value']);
Chris@0 98 // Check if the element is multiple select and no value has been selected.
Chris@0 99 $empty_value = (empty($element['#value']) && !empty($element['#multiple']));
Chris@0 100 $options = [];
Chris@0 101 foreach ($choices as $key => $choice) {
Chris@0 102 if (is_array($choice)) {
Chris@0 103 $options[] = [
Chris@0 104 'type' => 'optgroup',
Chris@0 105 'label' => $key,
Chris@0 106 'options' => form_select_options($element, $choice),
Chris@0 107 ];
Chris@0 108 }
Chris@0 109 elseif (is_object($choice) && isset($choice->option)) {
Chris@0 110 $options = array_merge($options, form_select_options($element, $choice->option));
Chris@0 111 }
Chris@0 112 else {
Chris@0 113 $option = [];
Chris@0 114 $key = (string) $key;
Chris@0 115 $empty_choice = $empty_value && $key == '_none';
Chris@0 116 if ($value_valid && ((!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value']))) || $empty_choice)) {
Chris@0 117 $option['selected'] = TRUE;
Chris@0 118 }
Chris@0 119 else {
Chris@0 120 $option['selected'] = FALSE;
Chris@0 121 }
Chris@0 122 $option['type'] = 'option';
Chris@0 123 $option['value'] = $key;
Chris@0 124 $option['label'] = $choice;
Chris@0 125 $options[] = $option;
Chris@0 126 }
Chris@0 127 }
Chris@0 128 return $options;
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * Returns the indexes of a select element's options matching a given key.
Chris@0 133 *
Chris@0 134 * This function is useful if you need to modify the options that are
Chris@0 135 * already in a form element; for example, to remove choices which are
Chris@0 136 * not valid because of additional filters imposed by another module.
Chris@0 137 * One example might be altering the choices in a taxonomy selector.
Chris@0 138 * To correctly handle the case of a multiple hierarchy taxonomy,
Chris@0 139 * #options arrays can now hold an array of objects, instead of a
Chris@0 140 * direct mapping of keys to labels, so that multiple choices in the
Chris@0 141 * selector can have the same key (and label). This makes it difficult
Chris@0 142 * to manipulate directly, which is why this helper function exists.
Chris@0 143 *
Chris@0 144 * This function does not support optgroups (when the elements of the
Chris@0 145 * #options array are themselves arrays), and will return FALSE if
Chris@0 146 * arrays are found. The caller must either flatten/restore or
Chris@0 147 * manually do their manipulations in this case, since returning the
Chris@0 148 * index is not sufficient, and supporting this would make the
Chris@0 149 * "helper" too complicated and cumbersome to be of any help.
Chris@0 150 *
Chris@0 151 * As usual with functions that can return array() or FALSE, do not
Chris@0 152 * forget to use === and !== if needed.
Chris@0 153 *
Chris@0 154 * @param $element
Chris@0 155 * The select element to search.
Chris@0 156 * @param $key
Chris@0 157 * The key to look for.
Chris@0 158 *
Chris@0 159 * @return
Chris@0 160 * An array of indexes that match the given $key. Array will be
Chris@0 161 * empty if no elements were found. FALSE if optgroups were found.
Chris@0 162 */
Chris@0 163 function form_get_options($element, $key) {
Chris@0 164 $keys = [];
Chris@0 165 foreach ($element['#options'] as $index => $choice) {
Chris@0 166 if (is_array($choice)) {
Chris@0 167 return FALSE;
Chris@0 168 }
Chris@0 169 elseif (is_object($choice)) {
Chris@0 170 if (isset($choice->option[$key])) {
Chris@0 171 $keys[] = $index;
Chris@0 172 }
Chris@0 173 }
Chris@0 174 elseif ($index == $key) {
Chris@0 175 $keys[] = $index;
Chris@0 176 }
Chris@0 177 }
Chris@0 178 return $keys;
Chris@0 179 }
Chris@0 180
Chris@0 181 /**
Chris@0 182 * Prepares variables for fieldset element templates.
Chris@0 183 *
Chris@0 184 * Default template: fieldset.html.twig.
Chris@0 185 *
Chris@0 186 * @param array $variables
Chris@0 187 * An associative array containing:
Chris@0 188 * - element: An associative array containing the properties of the element.
Chris@0 189 * Properties used: #attributes, #children, #description, #id, #title,
Chris@0 190 * #value.
Chris@0 191 */
Chris@0 192 function template_preprocess_fieldset(&$variables) {
Chris@0 193 $element = $variables['element'];
Chris@0 194 Element::setAttributes($element, ['id']);
Chris@0 195 RenderElement::setAttributes($element);
Chris@0 196 $variables['attributes'] = isset($element['#attributes']) ? $element['#attributes'] : [];
Chris@0 197 $variables['prefix'] = isset($element['#field_prefix']) ? $element['#field_prefix'] : NULL;
Chris@0 198 $variables['suffix'] = isset($element['#field_suffix']) ? $element['#field_suffix'] : NULL;
Chris@0 199 $variables['title_display'] = isset($element['#title_display']) ? $element['#title_display'] : NULL;
Chris@0 200 $variables['children'] = $element['#children'];
Chris@0 201 $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
Chris@0 202
Chris@0 203 if (isset($element['#title']) && $element['#title'] !== '') {
Chris@0 204 $variables['legend']['title'] = ['#markup' => $element['#title']];
Chris@0 205 }
Chris@0 206
Chris@0 207 $variables['legend']['attributes'] = new Attribute();
Chris@0 208 // Add 'visually-hidden' class to legend span.
Chris@0 209 if ($variables['title_display'] == 'invisible') {
Chris@0 210 $variables['legend_span']['attributes'] = new Attribute(['class' => ['visually-hidden']]);
Chris@0 211 }
Chris@0 212 else {
Chris@0 213 $variables['legend_span']['attributes'] = new Attribute();
Chris@0 214 }
Chris@0 215
Chris@0 216 if (!empty($element['#description'])) {
Chris@0 217 $description_id = $element['#attributes']['id'] . '--description';
Chris@0 218 $description_attributes['id'] = $description_id;
Chris@0 219 $variables['description']['attributes'] = new Attribute($description_attributes);
Chris@0 220 $variables['description']['content'] = $element['#description'];
Chris@0 221
Chris@0 222 // Add the description's id to the fieldset aria attributes.
Chris@0 223 $variables['attributes']['aria-describedby'] = $description_id;
Chris@0 224 }
Chris@0 225
Chris@0 226 // Suppress error messages.
Chris@0 227 $variables['errors'] = NULL;
Chris@0 228 }
Chris@0 229
Chris@0 230 /**
Chris@0 231 * Prepares variables for details element templates.
Chris@0 232 *
Chris@0 233 * Default template: details.html.twig.
Chris@0 234 *
Chris@0 235 * @param array $variables
Chris@0 236 * An associative array containing:
Chris@0 237 * - element: An associative array containing the properties of the element.
Chris@0 238 * Properties used: #attributes, #children, #open,
Chris@0 239 * #description, #id, #title, #value, #optional.
Chris@0 240 */
Chris@0 241 function template_preprocess_details(&$variables) {
Chris@0 242 $element = $variables['element'];
Chris@0 243 $variables['attributes'] = $element['#attributes'];
Chris@0 244 $variables['summary_attributes'] = new Attribute();
Chris@0 245 if (!empty($element['#title'])) {
Chris@0 246 $variables['summary_attributes']['role'] = 'button';
Chris@0 247 if (!empty($element['#attributes']['id'])) {
Chris@0 248 $variables['summary_attributes']['aria-controls'] = $element['#attributes']['id'];
Chris@0 249 }
Chris@0 250 $variables['summary_attributes']['aria-expanded'] = !empty($element['#attributes']['open']) ? 'true' : 'false';
Chris@0 251 $variables['summary_attributes']['aria-pressed'] = $variables['summary_attributes']['aria-expanded'];
Chris@0 252 }
Chris@0 253 $variables['title'] = (!empty($element['#title'])) ? $element['#title'] : '';
Chris@0 254 $variables['description'] = (!empty($element['#description'])) ? $element['#description'] : '';
Chris@0 255 $variables['children'] = (isset($element['#children'])) ? $element['#children'] : '';
Chris@0 256 $variables['value'] = (isset($element['#value'])) ? $element['#value'] : '';
Chris@0 257 $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
Chris@0 258
Chris@0 259 // Suppress error messages.
Chris@0 260 $variables['errors'] = NULL;
Chris@0 261 }
Chris@0 262
Chris@0 263 /**
Chris@0 264 * Prepares variables for radios templates.
Chris@0 265 *
Chris@0 266 * Default template: radios.html.twig.
Chris@0 267 *
Chris@0 268 * @param array $variables
Chris@0 269 * An associative array containing:
Chris@0 270 * - element: An associative array containing the properties of the element.
Chris@0 271 * Properties used: #title, #value, #options, #description, #required,
Chris@0 272 * #attributes, #children.
Chris@0 273 */
Chris@0 274 function template_preprocess_radios(&$variables) {
Chris@0 275 $element = $variables['element'];
Chris@0 276 $variables['attributes'] = [];
Chris@0 277 if (isset($element['#id'])) {
Chris@0 278 $variables['attributes']['id'] = $element['#id'];
Chris@0 279 }
Chris@0 280 if (isset($element['#attributes']['title'])) {
Chris@0 281 $variables['attributes']['title'] = $element['#attributes']['title'];
Chris@0 282 }
Chris@0 283 $variables['children'] = $element['#children'];
Chris@0 284 }
Chris@0 285
Chris@0 286 /**
Chris@0 287 * Prepares variables for checkboxes templates.
Chris@0 288 *
Chris@0 289 * Default template: checkboxes.html.twig.
Chris@0 290 *
Chris@0 291 * @param array $variables
Chris@0 292 * An associative array containing:
Chris@0 293 * - element: An associative array containing the properties of the element.
Chris@0 294 * Properties used: #children, #attributes.
Chris@0 295 */
Chris@0 296 function template_preprocess_checkboxes(&$variables) {
Chris@0 297 $element = $variables['element'];
Chris@0 298 $variables['attributes'] = [];
Chris@0 299 if (isset($element['#id'])) {
Chris@0 300 $variables['attributes']['id'] = $element['#id'];
Chris@0 301 }
Chris@0 302 if (isset($element['#attributes']['title'])) {
Chris@0 303 $variables['attributes']['title'] = $element['#attributes']['title'];
Chris@0 304 }
Chris@0 305 $variables['children'] = $element['#children'];
Chris@0 306 }
Chris@0 307
Chris@0 308 /**
Chris@0 309 * Prepares variables for vertical tabs templates.
Chris@0 310 *
Chris@0 311 * Default template: vertical-tabs.html.twig.
Chris@0 312 *
Chris@0 313 * @param array $variables
Chris@0 314 * An associative array containing:
Chris@0 315 * - element: An associative array containing the properties and children of
Chris@0 316 * the details element. Properties used: #children.
Chris@0 317 */
Chris@0 318 function template_preprocess_vertical_tabs(&$variables) {
Chris@0 319 $element = $variables['element'];
Chris@0 320 $variables['children'] = (!empty($element['#children'])) ? $element['#children'] : '';
Chris@0 321 }
Chris@0 322
Chris@0 323 /**
Chris@0 324 * Prepares variables for input templates.
Chris@0 325 *
Chris@0 326 * Default template: input.html.twig.
Chris@0 327 *
Chris@0 328 * @param array $variables
Chris@0 329 * An associative array containing:
Chris@0 330 * - element: An associative array containing the properties of the element.
Chris@0 331 * Properties used: #attributes.
Chris@0 332 */
Chris@0 333 function template_preprocess_input(&$variables) {
Chris@0 334 $element = $variables['element'];
Chris@0 335 // Remove name attribute if empty, for W3C compliance.
Chris@0 336 if (isset($variables['attributes']['name']) && empty((string) $variables['attributes']['name'])) {
Chris@0 337 unset($variables['attributes']['name']);
Chris@0 338 }
Chris@0 339 $variables['children'] = $element['#children'];
Chris@0 340 }
Chris@0 341
Chris@0 342 /**
Chris@0 343 * Prepares variables for form templates.
Chris@0 344 *
Chris@0 345 * Default template: form.html.twig.
Chris@0 346 *
Chris@0 347 * @param $variables
Chris@0 348 * An associative array containing:
Chris@0 349 * - element: An associative array containing the properties of the element.
Chris@0 350 * Properties used: #action, #method, #attributes, #children
Chris@0 351 */
Chris@0 352 function template_preprocess_form(&$variables) {
Chris@0 353 $element = $variables['element'];
Chris@0 354 if (isset($element['#action'])) {
Chris@0 355 $element['#attributes']['action'] = UrlHelper::stripDangerousProtocols($element['#action']);
Chris@0 356 }
Chris@0 357 Element::setAttributes($element, ['method', 'id']);
Chris@0 358 if (empty($element['#attributes']['accept-charset'])) {
Chris@0 359 $element['#attributes']['accept-charset'] = "UTF-8";
Chris@0 360 }
Chris@0 361 $variables['attributes'] = $element['#attributes'];
Chris@0 362 $variables['children'] = $element['#children'];
Chris@0 363 }
Chris@0 364
Chris@0 365 /**
Chris@0 366 * Prepares variables for textarea templates.
Chris@0 367 *
Chris@0 368 * Default template: textarea.html.twig.
Chris@0 369 *
Chris@0 370 * @param array $variables
Chris@0 371 * An associative array containing:
Chris@0 372 * - element: An associative array containing the properties of the element.
Chris@0 373 * Properties used: #title, #value, #description, #rows, #cols, #maxlength,
Chris@0 374 * #placeholder, #required, #attributes, #resizable.
Chris@0 375 */
Chris@0 376 function template_preprocess_textarea(&$variables) {
Chris@0 377 $element = $variables['element'];
Chris@0 378 $attributes = ['id', 'name', 'rows', 'cols', 'maxlength', 'placeholder'];
Chris@0 379 Element::setAttributes($element, $attributes);
Chris@0 380 RenderElement::setAttributes($element, ['form-textarea']);
Chris@0 381 $variables['wrapper_attributes'] = new Attribute();
Chris@0 382 $variables['attributes'] = new Attribute($element['#attributes']);
Chris@0 383 $variables['value'] = $element['#value'];
Chris@0 384 $variables['resizable'] = !empty($element['#resizable']) ? $element['#resizable'] : NULL;
Chris@0 385 $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
Chris@0 386 }
Chris@0 387
Chris@0 388 /**
Chris@0 389 * Returns HTML for a form element.
Chris@0 390 * Prepares variables for form element templates.
Chris@0 391 *
Chris@0 392 * Default template: form-element.html.twig.
Chris@0 393 *
Chris@0 394 * In addition to the element itself, the DIV contains a label for the element
Chris@0 395 * based on the optional #title_display property, and an optional #description.
Chris@0 396 *
Chris@0 397 * The optional #title_display property can have these values:
Chris@0 398 * - before: The label is output before the element. This is the default.
Chris@0 399 * The label includes the #title and the required marker, if #required.
Chris@0 400 * - after: The label is output after the element. For example, this is used
Chris@0 401 * for radio and checkbox #type elements. If the #title is empty but the field
Chris@0 402 * is #required, the label will contain only the required marker.
Chris@0 403 * - invisible: Labels are critical for screen readers to enable them to
Chris@0 404 * properly navigate through forms but can be visually distracting. This
Chris@0 405 * property hides the label for everyone except screen readers.
Chris@0 406 * - attribute: Set the title attribute on the element to create a tooltip
Chris@0 407 * but output no label element. This is supported only for checkboxes
Chris@0 408 * and radios in
Chris@0 409 * \Drupal\Core\Render\Element\CompositeFormElementTrait::preRenderCompositeFormElement().
Chris@0 410 * It is used where a visual label is not needed, such as a table of
Chris@0 411 * checkboxes where the row and column provide the context. The tooltip will
Chris@0 412 * include the title and required marker.
Chris@0 413 *
Chris@0 414 * If the #title property is not set, then the label and any required marker
Chris@0 415 * will not be output, regardless of the #title_display or #required values.
Chris@0 416 * This can be useful in cases such as the password_confirm element, which
Chris@0 417 * creates children elements that have their own labels and required markers,
Chris@0 418 * but the parent element should have neither. Use this carefully because a
Chris@0 419 * field without an associated label can cause accessibility challenges.
Chris@0 420 *
Chris@0 421 * @param array $variables
Chris@0 422 * An associative array containing:
Chris@0 423 * - element: An associative array containing the properties of the element.
Chris@0 424 * Properties used: #title, #title_display, #description, #id, #required,
Chris@0 425 * #children, #type, #name.
Chris@0 426 */
Chris@0 427 function template_preprocess_form_element(&$variables) {
Chris@0 428 $element = &$variables['element'];
Chris@0 429
Chris@0 430 // This function is invoked as theme wrapper, but the rendered form element
Chris@0 431 // may not necessarily have been processed by
Chris@0 432 // \Drupal::formBuilder()->doBuildForm().
Chris@0 433 $element += [
Chris@0 434 '#title_display' => 'before',
Chris@0 435 '#wrapper_attributes' => [],
Chris@0 436 '#label_attributes' => [],
Chris@0 437 ];
Chris@0 438 $variables['attributes'] = $element['#wrapper_attributes'];
Chris@0 439
Chris@0 440 // Add element #id for #type 'item'.
Chris@0 441 if (isset($element['#markup']) && !empty($element['#id'])) {
Chris@0 442 $variables['attributes']['id'] = $element['#id'];
Chris@0 443 }
Chris@0 444
Chris@0 445 // Pass elements #type and #name to template.
Chris@0 446 if (!empty($element['#type'])) {
Chris@0 447 $variables['type'] = $element['#type'];
Chris@0 448 }
Chris@0 449 if (!empty($element['#name'])) {
Chris@0 450 $variables['name'] = $element['#name'];
Chris@0 451 }
Chris@0 452
Chris@0 453 // Pass elements disabled status to template.
Chris@0 454 $variables['disabled'] = !empty($element['#attributes']['disabled']) ? $element['#attributes']['disabled'] : NULL;
Chris@0 455
Chris@0 456 // Suppress error messages.
Chris@0 457 $variables['errors'] = NULL;
Chris@0 458
Chris@0 459 // If #title is not set, we don't display any label.
Chris@0 460 if (!isset($element['#title'])) {
Chris@0 461 $element['#title_display'] = 'none';
Chris@0 462 }
Chris@0 463
Chris@0 464 $variables['title_display'] = $element['#title_display'];
Chris@0 465
Chris@0 466 $variables['prefix'] = isset($element['#field_prefix']) ? $element['#field_prefix'] : NULL;
Chris@0 467 $variables['suffix'] = isset($element['#field_suffix']) ? $element['#field_suffix'] : NULL;
Chris@0 468
Chris@0 469 $variables['description'] = NULL;
Chris@0 470 if (!empty($element['#description'])) {
Chris@0 471 $variables['description_display'] = $element['#description_display'];
Chris@0 472 $description_attributes = [];
Chris@0 473 if (!empty($element['#id'])) {
Chris@0 474 $description_attributes['id'] = $element['#id'] . '--description';
Chris@0 475 }
Chris@0 476 $variables['description']['attributes'] = new Attribute($description_attributes);
Chris@0 477 $variables['description']['content'] = $element['#description'];
Chris@0 478 }
Chris@0 479
Chris@0 480 // Add label_display and label variables to template.
Chris@0 481 $variables['label_display'] = $element['#title_display'];
Chris@0 482 $variables['label'] = ['#theme' => 'form_element_label'];
Chris@0 483 $variables['label'] += array_intersect_key($element, array_flip(['#id', '#required', '#title', '#title_display']));
Chris@0 484 $variables['label']['#attributes'] = $element['#label_attributes'];
Chris@0 485
Chris@0 486 $variables['children'] = $element['#children'];
Chris@0 487 }
Chris@0 488
Chris@0 489 /**
Chris@0 490 * Prepares variables for form label templates.
Chris@0 491 *
Chris@0 492 * Form element labels include the #title and a #required marker. The label is
Chris@0 493 * associated with the element itself by the element #id. Labels may appear
Chris@0 494 * before or after elements, depending on form-element.html.twig and
Chris@0 495 * #title_display.
Chris@0 496 *
Chris@0 497 * This function will not be called for elements with no labels, depending on
Chris@0 498 * #title_display. For elements that have an empty #title and are not required,
Chris@0 499 * this function will output no label (''). For required elements that have an
Chris@0 500 * empty #title, this will output the required marker alone within the label.
Chris@0 501 * The label will use the #id to associate the marker with the field that is
Chris@0 502 * required. That is especially important for screenreader users to know
Chris@0 503 * which field is required.
Chris@0 504 *
Chris@0 505 * @param array $variables
Chris@0 506 * An associative array containing:
Chris@0 507 * - element: An associative array containing the properties of the element.
Chris@0 508 * Properties used: #required, #title, #id, #value, #description.
Chris@0 509 */
Chris@0 510 function template_preprocess_form_element_label(&$variables) {
Chris@0 511 $element = $variables['element'];
Chris@0 512 // If title and required marker are both empty, output no label.
Chris@0 513 if (isset($element['#title']) && $element['#title'] !== '') {
Chris@0 514 $variables['title'] = ['#markup' => $element['#title']];
Chris@0 515 }
Chris@0 516
Chris@0 517 // Pass elements title_display to template.
Chris@0 518 $variables['title_display'] = $element['#title_display'];
Chris@0 519
Chris@0 520 // A #for property of a dedicated #type 'label' element as precedence.
Chris@0 521 if (!empty($element['#for'])) {
Chris@0 522 $variables['attributes']['for'] = $element['#for'];
Chris@0 523 // A custom #id allows the referenced form input element to refer back to
Chris@0 524 // the label element; e.g., in the 'aria-labelledby' attribute.
Chris@0 525 if (!empty($element['#id'])) {
Chris@0 526 $variables['attributes']['id'] = $element['#id'];
Chris@0 527 }
Chris@0 528 }
Chris@0 529 // Otherwise, point to the #id of the form input element.
Chris@0 530 elseif (!empty($element['#id'])) {
Chris@0 531 $variables['attributes']['for'] = $element['#id'];
Chris@0 532 }
Chris@0 533
Chris@0 534 // Pass elements required to template.
Chris@0 535 $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
Chris@0 536 }
Chris@0 537
Chris@0 538 /**
Chris@0 539 * @defgroup batch Batch operations
Chris@0 540 * @{
Chris@0 541 * Creates and processes batch operations.
Chris@0 542 *
Chris@0 543 * Functions allowing forms processing to be spread out over several page
Chris@0 544 * requests, thus ensuring that the processing does not get interrupted
Chris@0 545 * because of a PHP timeout, while allowing the user to receive feedback
Chris@0 546 * on the progress of the ongoing operations.
Chris@0 547 *
Chris@0 548 * The API is primarily designed to integrate nicely with the Form API
Chris@0 549 * workflow, but can also be used by non-Form API scripts (like update.php)
Chris@0 550 * or even simple page callbacks (which should probably be used sparingly).
Chris@0 551 *
Chris@0 552 * Example:
Chris@0 553 * @code
Chris@0 554 * $batch = array(
Chris@0 555 * 'title' => t('Exporting'),
Chris@0 556 * 'operations' => array(
Chris@0 557 * array('my_function_1', array($account->id(), 'story')),
Chris@0 558 * array('my_function_2', array()),
Chris@0 559 * ),
Chris@0 560 * 'finished' => 'my_finished_callback',
Chris@0 561 * 'file' => 'path_to_file_containing_myfunctions',
Chris@0 562 * );
Chris@0 563 * batch_set($batch);
Chris@0 564 * // Only needed if not inside a form _submit handler.
Chris@0 565 * // Setting redirect in batch_process.
Chris@0 566 * batch_process('node/1');
Chris@0 567 * @endcode
Chris@0 568 *
Chris@0 569 * Note: if the batch 'title', 'init_message', 'progress_message', or
Chris@0 570 * 'error_message' could contain any user input, it is the responsibility of
Chris@0 571 * the code calling batch_set() to sanitize them first with a function like
Chris@0 572 * \Drupal\Component\Utility\Html::escape() or
Chris@0 573 * \Drupal\Component\Utility\Xss::filter(). Furthermore, if the batch operation
Chris@0 574 * returns any user input in the 'results' or 'message' keys of $context, it
Chris@0 575 * must also sanitize them first.
Chris@0 576 *
Chris@0 577 * Sample callback_batch_operation():
Chris@0 578 * @code
Chris@0 579 * // Simple and artificial: load a node of a given type for a given user
Chris@0 580 * function my_function_1($uid, $type, &$context) {
Chris@0 581 * // The $context array gathers batch context information about the execution (read),
Chris@0 582 * // as well as 'return values' for the current operation (write)
Chris@0 583 * // The following keys are provided :
Chris@0 584 * // 'results' (read / write): The array of results gathered so far by
Chris@0 585 * // the batch processing, for the current operation to append its own.
Chris@0 586 * // 'message' (write): A text message displayed in the progress page.
Chris@0 587 * // The following keys allow for multi-step operations :
Chris@0 588 * // 'sandbox' (read / write): An array that can be freely used to
Chris@0 589 * // store persistent data between iterations. It is recommended to
Chris@0 590 * // use this instead of $_SESSION, which is unsafe if the user
Chris@0 591 * // continues browsing in a separate window while the batch is processing.
Chris@0 592 * // 'finished' (write): A float number between 0 and 1 informing
Chris@0 593 * // the processing engine of the completion level for the operation.
Chris@0 594 * // 1 (or no value explicitly set) means the operation is finished
Chris@0 595 * // and the batch processing can continue to the next operation.
Chris@0 596 *
Chris@0 597 * $nodes = \Drupal::entityTypeManager()->getStorage('node')
Chris@0 598 * ->loadByProperties(['uid' => $uid, 'type' => $type]);
Chris@0 599 * $node = reset($nodes);
Chris@0 600 * $context['results'][] = $node->id() . ' : ' . Html::escape($node->label());
Chris@0 601 * $context['message'] = Html::escape($node->label());
Chris@0 602 * }
Chris@0 603 *
Chris@0 604 * // A more advanced example is a multi-step operation that loads all rows,
Chris@0 605 * // five by five.
Chris@0 606 * function my_function_2(&$context) {
Chris@0 607 * if (empty($context['sandbox'])) {
Chris@0 608 * $context['sandbox']['progress'] = 0;
Chris@0 609 * $context['sandbox']['current_id'] = 0;
Chris@0 610 * $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT id) FROM {example}')->fetchField();
Chris@0 611 * }
Chris@0 612 * $limit = 5;
Chris@0 613 * $result = db_select('example')
Chris@0 614 * ->fields('example', array('id'))
Chris@0 615 * ->condition('id', $context['sandbox']['current_id'], '>')
Chris@0 616 * ->orderBy('id')
Chris@0 617 * ->range(0, $limit)
Chris@0 618 * ->execute();
Chris@0 619 * foreach ($result as $row) {
Chris@0 620 * $context['results'][] = $row->id . ' : ' . Html::escape($row->title);
Chris@0 621 * $context['sandbox']['progress']++;
Chris@0 622 * $context['sandbox']['current_id'] = $row->id;
Chris@0 623 * $context['message'] = Html::escape($row->title);
Chris@0 624 * }
Chris@0 625 * if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
Chris@0 626 * $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
Chris@0 627 * }
Chris@0 628 * }
Chris@0 629 * @endcode
Chris@0 630 *
Chris@0 631 * Sample callback_batch_finished():
Chris@0 632 * @code
Chris@0 633 * function my_finished_callback($success, $results, $operations) {
Chris@0 634 * // The 'success' parameter means no fatal PHP errors were detected. All
Chris@0 635 * // other error management should be handled using 'results'.
Chris@0 636 * if ($success) {
Chris@0 637 * $message = \Drupal::translation()->formatPlural(count($results), 'One post processed.', '@count posts processed.');
Chris@0 638 * }
Chris@0 639 * else {
Chris@0 640 * $message = t('Finished with an error.');
Chris@0 641 * }
Chris@0 642 * drupal_set_message($message);
Chris@0 643 * // Providing data for the redirected page is done through $_SESSION.
Chris@0 644 * foreach ($results as $result) {
Chris@0 645 * $items[] = t('Loaded node %title.', array('%title' => $result));
Chris@0 646 * }
Chris@0 647 * $_SESSION['my_batch_results'] = $items;
Chris@0 648 * }
Chris@0 649 * @endcode
Chris@0 650 */
Chris@0 651
Chris@0 652 /**
Chris@0 653 * Adds a new batch.
Chris@0 654 *
Chris@0 655 * Batch operations are added as new batch sets. Batch sets are used to spread
Chris@0 656 * processing (primarily, but not exclusively, forms processing) over several
Chris@0 657 * page requests. This helps to ensure that the processing is not interrupted
Chris@0 658 * due to PHP timeouts, while users are still able to receive feedback on the
Chris@0 659 * progress of the ongoing operations. Combining related operations into
Chris@0 660 * distinct batch sets provides clean code independence for each batch set,
Chris@0 661 * ensuring that two or more batches, submitted independently, can be processed
Chris@0 662 * without mutual interference. Each batch set may specify its own set of
Chris@0 663 * operations and results, produce its own UI messages, and trigger its own
Chris@0 664 * 'finished' callback. Batch sets are processed sequentially, with the progress
Chris@0 665 * bar starting afresh for each new set.
Chris@0 666 *
Chris@0 667 * @param $batch_definition
Chris@0 668 * An associative array defining the batch, with the following elements (all
Chris@0 669 * are optional except as noted):
Chris@0 670 * - operations: (required) Array of operations to be performed, where each
Chris@0 671 * item is an array consisting of the name of an implementation of
Chris@0 672 * callback_batch_operation() and an array of parameter.
Chris@0 673 * Example:
Chris@0 674 * @code
Chris@0 675 * array(
Chris@0 676 * array('callback_batch_operation_1', array($arg1)),
Chris@0 677 * array('callback_batch_operation_2', array($arg2_1, $arg2_2)),
Chris@0 678 * )
Chris@0 679 * @endcode
Chris@0 680 * - title: A safe, translated string to use as the title for the progress
Chris@0 681 * page. Defaults to t('Processing').
Chris@0 682 * - init_message: Message displayed while the processing is initialized.
Chris@0 683 * Defaults to t('Initializing.').
Chris@0 684 * - progress_message: Message displayed while processing the batch. Available
Chris@0 685 * placeholders are @current, @remaining, @total, @percentage, @estimate and
Chris@0 686 * @elapsed. Defaults to t('Completed @current of @total.').
Chris@0 687 * - error_message: Message displayed if an error occurred while processing
Chris@0 688 * the batch. Defaults to t('An error has occurred.').
Chris@0 689 * - finished: Name of an implementation of callback_batch_finished(). This is
Chris@0 690 * executed after the batch has completed. This should be used to perform
Chris@0 691 * any result massaging that may be needed, and possibly save data in
Chris@0 692 * $_SESSION for display after final page redirection.
Chris@0 693 * - file: Path to the file containing the definitions of the 'operations' and
Chris@0 694 * 'finished' functions, for instance if they don't reside in the main
Chris@0 695 * .module file. The path should be relative to base_path(), and thus should
Chris@0 696 * be built using drupal_get_path().
Chris@0 697 * - library: An array of batch-specific CSS and JS libraries.
Chris@0 698 * - url_options: options passed to the \Drupal\Core\Url object when
Chris@0 699 * constructing redirect URLs for the batch.
Chris@0 700 * - progressive: A Boolean that indicates whether or not the batch needs to
Chris@0 701 * run progressively. TRUE indicates that the batch will run in more than
Chris@0 702 * one run. FALSE (default) indicates that the batch will finish in a single
Chris@0 703 * run.
Chris@0 704 * - queue: An override of the default queue (with name and class fields
Chris@0 705 * optional). An array containing two elements:
Chris@0 706 * - name: Unique identifier for the queue.
Chris@0 707 * - class: The name of a class that implements
Chris@0 708 * \Drupal\Core\Queue\QueueInterface, including the full namespace but not
Chris@0 709 * starting with a backslash. It must have a constructor with two
Chris@0 710 * arguments: $name and a \Drupal\Core\Database\Connection object.
Chris@0 711 * Typically, the class will either be \Drupal\Core\Queue\Batch or
Chris@0 712 * \Drupal\Core\Queue\BatchMemory. Defaults to Batch if progressive is
Chris@0 713 * TRUE, or to BatchMemory if progressive is FALSE.
Chris@0 714 */
Chris@0 715 function batch_set($batch_definition) {
Chris@0 716 if ($batch_definition) {
Chris@0 717 $batch =& batch_get();
Chris@0 718
Chris@0 719 // Initialize the batch if needed.
Chris@0 720 if (empty($batch)) {
Chris@0 721 $batch = [
Chris@0 722 'sets' => [],
Chris@0 723 'has_form_submits' => FALSE,
Chris@0 724 ];
Chris@0 725 }
Chris@0 726
Chris@0 727 // Base and default properties for the batch set.
Chris@0 728 $init = [
Chris@0 729 'sandbox' => [],
Chris@0 730 'results' => [],
Chris@0 731 'success' => FALSE,
Chris@0 732 'start' => 0,
Chris@0 733 'elapsed' => 0,
Chris@0 734 ];
Chris@0 735 $defaults = [
Chris@0 736 'title' => t('Processing'),
Chris@0 737 'init_message' => t('Initializing.'),
Chris@0 738 'progress_message' => t('Completed @current of @total.'),
Chris@0 739 'error_message' => t('An error has occurred.'),
Chris@0 740 ];
Chris@0 741 $batch_set = $init + $batch_definition + $defaults;
Chris@0 742
Chris@0 743 // Tweak init_message to avoid the bottom of the page flickering down after
Chris@0 744 // init phase.
Chris@0 745 $batch_set['init_message'] .= '<br/>&nbsp;';
Chris@0 746
Chris@0 747 // The non-concurrent workflow of batch execution allows us to save
Chris@0 748 // numberOfItems() queries by handling our own counter.
Chris@0 749 $batch_set['total'] = count($batch_set['operations']);
Chris@0 750 $batch_set['count'] = $batch_set['total'];
Chris@0 751
Chris@0 752 // Add the set to the batch.
Chris@0 753 if (empty($batch['id'])) {
Chris@0 754 // The batch is not running yet. Simply add the new set.
Chris@0 755 $batch['sets'][] = $batch_set;
Chris@0 756 }
Chris@0 757 else {
Chris@0 758 // The set is being added while the batch is running. Insert the new set
Chris@0 759 // right after the current one to ensure execution order, and store its
Chris@0 760 // operations in a queue.
Chris@0 761 $index = $batch['current_set'] + 1;
Chris@0 762 $slice1 = array_slice($batch['sets'], 0, $index);
Chris@0 763 $slice2 = array_slice($batch['sets'], $index);
Chris@0 764 $batch['sets'] = array_merge($slice1, [$batch_set], $slice2);
Chris@0 765 _batch_populate_queue($batch, $index);
Chris@0 766 }
Chris@0 767 }
Chris@0 768 }
Chris@0 769
Chris@0 770 /**
Chris@0 771 * Processes the batch.
Chris@0 772 *
Chris@0 773 * This function is generally not needed in form submit handlers;
Chris@0 774 * Form API takes care of batches that were set during form submission.
Chris@0 775 *
Chris@0 776 * @param \Drupal\Core\Url|string $redirect
Chris@0 777 * (optional) Either path or Url object to redirect to when the batch has
Chris@0 778 * finished processing. Note that to simply force a batch to (conditionally)
Chris@0 779 * redirect to a custom location after it is finished processing but to
Chris@0 780 * otherwise allow the standard form API batch handling to occur, it is not
Chris@0 781 * necessary to call batch_process() and use this parameter. Instead, make
Chris@0 782 * the batch 'finished' callback return an instance of
Chris@0 783 * \Symfony\Component\HttpFoundation\RedirectResponse, which will be used
Chris@0 784 * automatically by the standard batch processing pipeline (and which takes
Chris@0 785 * precedence over this parameter).
Chris@0 786 * User will be redirected to the page that started the batch if this argument
Chris@0 787 * is omitted and no redirect response was returned by the 'finished'
Chris@0 788 * callback. Any query arguments will be automatically persisted.
Chris@0 789 * @param \Drupal\Core\Url $url
Chris@0 790 * (optional) URL of the batch processing page. Should only be used for
Chris@0 791 * separate scripts like update.php.
Chris@0 792 * @param $redirect_callback
Chris@0 793 * (optional) Specify a function to be called to redirect to the progressive
Chris@0 794 * processing page.
Chris@0 795 *
Chris@0 796 * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
Chris@0 797 * A redirect response if the batch is progressive. No return value otherwise.
Chris@0 798 */
Chris@0 799 function batch_process($redirect = NULL, Url $url = NULL, $redirect_callback = NULL) {
Chris@0 800 $batch =& batch_get();
Chris@0 801
Chris@0 802 if (isset($batch)) {
Chris@0 803 // Add process information
Chris@0 804 $process_info = [
Chris@0 805 'current_set' => 0,
Chris@0 806 'progressive' => TRUE,
Chris@0 807 'url' => isset($url) ? $url : Url::fromRoute('system.batch_page.html'),
Chris@0 808 'source_url' => Url::fromRouteMatch(\Drupal::routeMatch())->mergeOptions(['query' => \Drupal::request()->query->all()]),
Chris@0 809 'batch_redirect' => $redirect,
Chris@0 810 'theme' => \Drupal::theme()->getActiveTheme()->getName(),
Chris@0 811 'redirect_callback' => $redirect_callback,
Chris@0 812 ];
Chris@0 813 $batch += $process_info;
Chris@0 814
Chris@0 815 // The batch is now completely built. Allow other modules to make changes
Chris@0 816 // to the batch so that it is easier to reuse batch processes in other
Chris@0 817 // environments.
Chris@0 818 \Drupal::moduleHandler()->alter('batch', $batch);
Chris@0 819
Chris@0 820 // Assign an arbitrary id: don't rely on a serial column in the 'batch'
Chris@0 821 // table, since non-progressive batches skip database storage completely.
Chris@0 822 $batch['id'] = db_next_id();
Chris@0 823
Chris@0 824 // Move operations to a job queue. Non-progressive batches will use a
Chris@0 825 // memory-based queue.
Chris@0 826 foreach ($batch['sets'] as $key => $batch_set) {
Chris@0 827 _batch_populate_queue($batch, $key);
Chris@0 828 }
Chris@0 829
Chris@0 830 // Initiate processing.
Chris@0 831 if ($batch['progressive']) {
Chris@0 832 // Now that we have a batch id, we can generate the redirection link in
Chris@0 833 // the generic error message.
Chris@0 834 /** @var \Drupal\Core\Url $batch_url */
Chris@0 835 $batch_url = $batch['url'];
Chris@0 836 /** @var \Drupal\Core\Url $error_url */
Chris@0 837 $error_url = clone $batch_url;
Chris@0 838 $query_options = $error_url->getOption('query');
Chris@0 839 $query_options['id'] = $batch['id'];
Chris@0 840 $query_options['op'] = 'finished';
Chris@0 841 $error_url->setOption('query', $query_options);
Chris@0 842
Chris@0 843 $batch['error_message'] = t('Please continue to <a href=":error_url">the error page</a>', [':error_url' => $error_url->toString(TRUE)->getGeneratedUrl()]);
Chris@0 844
Chris@0 845 // Clear the way for the redirection to the batch processing page, by
Chris@0 846 // saving and unsetting the 'destination', if there is any.
Chris@0 847 $request = \Drupal::request();
Chris@0 848 if ($request->query->has('destination')) {
Chris@0 849 $batch['destination'] = $request->query->get('destination');
Chris@0 850 $request->query->remove('destination');
Chris@0 851 }
Chris@0 852
Chris@0 853 // Store the batch.
Chris@0 854 \Drupal::service('batch.storage')->create($batch);
Chris@0 855
Chris@0 856 // Set the batch number in the session to guarantee that it will stay alive.
Chris@0 857 $_SESSION['batches'][$batch['id']] = TRUE;
Chris@0 858
Chris@0 859 // Redirect for processing.
Chris@0 860 $query_options = $error_url->getOption('query');
Chris@0 861 $query_options['op'] = 'start';
Chris@0 862 $query_options['id'] = $batch['id'];
Chris@0 863 $batch_url->setOption('query', $query_options);
Chris@0 864 if (($function = $batch['redirect_callback']) && function_exists($function)) {
Chris@0 865 $function($batch_url->toString(), ['query' => $query_options]);
Chris@0 866 }
Chris@0 867 else {
Chris@0 868 return new RedirectResponse($batch_url->setAbsolute()->toString(TRUE)->getGeneratedUrl());
Chris@0 869 }
Chris@0 870 }
Chris@0 871 else {
Chris@0 872 // Non-progressive execution: bypass the whole progressbar workflow
Chris@0 873 // and execute the batch in one pass.
Chris@0 874 require_once __DIR__ . '/batch.inc';
Chris@0 875 _batch_process();
Chris@0 876 }
Chris@0 877 }
Chris@0 878 }
Chris@0 879
Chris@0 880 /**
Chris@0 881 * Retrieves the current batch.
Chris@0 882 */
Chris@0 883 function &batch_get() {
Chris@0 884 // Not drupal_static(), because Batch API operates at a lower level than most
Chris@0 885 // use-cases for resetting static variables, and we specifically do not want a
Chris@0 886 // global drupal_static_reset() resetting the batch information. Functions
Chris@0 887 // that are part of the Batch API and need to reset the batch information may
Chris@0 888 // call batch_get() and manipulate the result by reference. Functions that are
Chris@0 889 // not part of the Batch API can also do this, but shouldn't.
Chris@0 890 static $batch = [];
Chris@0 891 return $batch;
Chris@0 892 }
Chris@0 893
Chris@0 894 /**
Chris@0 895 * Populates a job queue with the operations of a batch set.
Chris@0 896 *
Chris@0 897 * Depending on whether the batch is progressive or not, the
Chris@0 898 * Drupal\Core\Queue\Batch or Drupal\Core\Queue\BatchMemory handler classes will
Chris@0 899 * be used. The name and class of the queue are added by reference to the
Chris@0 900 * batch set.
Chris@0 901 *
Chris@0 902 * @param $batch
Chris@0 903 * The batch array.
Chris@0 904 * @param $set_id
Chris@0 905 * The id of the set to process.
Chris@0 906 */
Chris@0 907 function _batch_populate_queue(&$batch, $set_id) {
Chris@0 908 $batch_set = &$batch['sets'][$set_id];
Chris@0 909
Chris@0 910 if (isset($batch_set['operations'])) {
Chris@0 911 $batch_set += [
Chris@0 912 'queue' => [
Chris@0 913 'name' => 'drupal_batch:' . $batch['id'] . ':' . $set_id,
Chris@0 914 'class' => $batch['progressive'] ? 'Drupal\Core\Queue\Batch' : 'Drupal\Core\Queue\BatchMemory',
Chris@0 915 ],
Chris@0 916 ];
Chris@0 917
Chris@0 918 $queue = _batch_queue($batch_set);
Chris@0 919 $queue->createQueue();
Chris@0 920 foreach ($batch_set['operations'] as $operation) {
Chris@0 921 $queue->createItem($operation);
Chris@0 922 }
Chris@0 923
Chris@0 924 unset($batch_set['operations']);
Chris@0 925 }
Chris@0 926 }
Chris@0 927
Chris@0 928 /**
Chris@0 929 * Returns a queue object for a batch set.
Chris@0 930 *
Chris@0 931 * @param $batch_set
Chris@0 932 * The batch set.
Chris@0 933 *
Chris@0 934 * @return
Chris@0 935 * The queue object.
Chris@0 936 */
Chris@0 937 function _batch_queue($batch_set) {
Chris@0 938 static $queues;
Chris@0 939
Chris@0 940 if (!isset($queues)) {
Chris@0 941 $queues = [];
Chris@0 942 }
Chris@0 943
Chris@0 944 if (isset($batch_set['queue'])) {
Chris@0 945 $name = $batch_set['queue']['name'];
Chris@0 946 $class = $batch_set['queue']['class'];
Chris@0 947
Chris@0 948 if (!isset($queues[$class][$name])) {
Chris@0 949 $queues[$class][$name] = new $class($name, \Drupal::database());
Chris@0 950 }
Chris@0 951 return $queues[$class][$name];
Chris@0 952 }
Chris@0 953 }
Chris@0 954
Chris@0 955 /**
Chris@0 956 * @} End of "defgroup batch".
Chris@0 957 */