comparison core/lib/Drupal/Core/Field/WidgetBase.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
101 // If the widget does not handle multiple values itself, (and we are not 101 // If the widget does not handle multiple values itself, (and we are not
102 // displaying an individual element), process the multiple value form. 102 // displaying an individual element), process the multiple value form.
103 else { 103 else {
104 $elements = $this->formMultipleElements($items, $form, $form_state); 104 $elements = $this->formMultipleElements($items, $form, $form_state);
105 } 105 }
106
107 // Allow modules to alter the field multi-value widget form element.
108 // This hook can also be used for single-value fields.
109 $context = [
110 'form' => $form,
111 'widget' => $this,
112 'items' => $items,
113 'default' => $this->isDefaultValueWidget($form_state),
114 ];
115 \Drupal::moduleHandler()->alter([
116 'field_widget_multivalue_form',
117 'field_widget_multivalue_' . $this->getPluginId() . '_form',
118 ], $elements, $form_state, $context);
106 119
107 // Populate the 'array_parents' information in $form_state->get('field') 120 // Populate the 'array_parents' information in $form_state->get('field')
108 // after the form is built, so that we catch changes in the form structure 121 // after the form is built, so that we catch changes in the form structure
109 // performed in alter() hooks. 122 // performed in alter() hooks.
110 $elements['#after_build'][] = [get_class($this), 'afterBuild']; 123 $elements['#after_build'][] = [get_class($this), 'afterBuild'];
408 421
409 // Only set errors if the element is visible. 422 // Only set errors if the element is visible.
410 if (Element::isVisibleElement($element)) { 423 if (Element::isVisibleElement($element)) {
411 $handles_multiple = $this->handlesMultipleValues(); 424 $handles_multiple = $this->handlesMultipleValues();
412 425
413 $violations_by_delta = []; 426 $violations_by_delta = $item_list_violations = [];
414 foreach ($violations as $violation) { 427 foreach ($violations as $violation) {
415 // Separate violations by delta. 428 // Separate violations by delta.
416 $property_path = explode('.', $violation->getPropertyPath()); 429 $property_path = explode('.', $violation->getPropertyPath());
417 $delta = array_shift($property_path); 430 $delta = array_shift($property_path);
418 $violations_by_delta[$delta][] = $violation; 431 if (is_numeric($delta)) {
432 $violations_by_delta[$delta][] = $violation;
433 }
434 // Violations at the ItemList level are not associated to any delta.
435 else {
436 $item_list_violations[] = $violation;
437 }
419 $violation->arrayPropertyPath = $property_path; 438 $violation->arrayPropertyPath = $property_path;
420 } 439 }
421 440
422 /** @var \Symfony\Component\Validator\ConstraintViolationInterface[] $delta_violations */ 441 /** @var \Symfony\Component\Validator\ConstraintViolationInterface[] $delta_violations */
423 foreach ($violations_by_delta as $delta => $delta_violations) { 442 foreach ($violations_by_delta as $delta => $delta_violations) {
424 // Pass violations to the main element: 443 // Pass violations to the main element if this is a multiple-value
425 // - if this is a multiple-value widget, 444 // widget.
426 // - or if the violations are at the ItemList level. 445 if ($handles_multiple) {
427 if ($handles_multiple || !is_numeric($delta)) {
428 $delta_element = $element; 446 $delta_element = $element;
429 } 447 }
430 // Otherwise, pass errors by delta to the corresponding sub-element. 448 // Otherwise, pass errors by delta to the corresponding sub-element.
431 else { 449 else {
432 $original_delta = $field_state['original_deltas'][$delta]; 450 $original_delta = $field_state['original_deltas'][$delta];
438 if ($error_element !== FALSE) { 456 if ($error_element !== FALSE) {
439 $form_state->setError($error_element, $violation->getMessage()); 457 $form_state->setError($error_element, $violation->getMessage());
440 } 458 }
441 } 459 }
442 } 460 }
461
462 /** @var \Symfony\Component\Validator\ConstraintViolationInterface[] $item_list_violations */
463 // Pass violations to the main element without going through
464 // errorElement() if the violations are at the ItemList level.
465 foreach ($item_list_violations as $violation) {
466 $form_state->setError($element, $violation->getMessage());
467 }
443 } 468 }
444 } 469 }
445 } 470 }
446 471
447 /** 472 /**