Chris@0: 'submit', Chris@0: // Hide this button when JavaScript is enabled. Chris@0: '#attributes' => ['class' => ['js-hide']], Chris@0: '#submit' => ['views_ui_nojs_submit'], Chris@0: // Add a process function to limit this button's validation errors to the Chris@0: // triggering element only. We have to do this in #process since until the Chris@0: // form API has added the #parents property to the triggering element for Chris@0: // us, we don't have any (easy) way to find out where its submitted values Chris@0: // will eventually appear in $form_state->getValues(). Chris@0: '#process' => array_merge(['views_ui_add_limited_validation'], $element_info->getInfoProperty('submit', '#process', [])), Chris@0: // Add an after-build function that inserts a wrapper around the region of Chris@0: // the form that needs to be refreshed by AJAX (so that the AJAX system can Chris@0: // detect and dynamically update it). This is done in #after_build because Chris@0: // it's a convenient place where we have automatic access to the complete Chris@0: // form array, but also to minimize the chance that the HTML we add will Chris@0: // get clobbered by code that runs after we have added it. Chris@0: '#after_build' => array_merge($element_info->getInfoProperty('submit', '#after_build', []), ['views_ui_add_ajax_wrapper']), Chris@0: ]; Chris@0: // Copy #weight and #access from the triggering element to the button, so Chris@0: // that the two elements will be displayed together. Chris@0: foreach (['#weight', '#access'] as $property) { Chris@0: if (isset($triggering_element[$property])) { Chris@0: $wrapping_element[$button_key][$property] = $triggering_element[$property]; Chris@0: } Chris@0: } Chris@0: // For easiest integration with the form API and the testing framework, we Chris@0: // always give the button a unique #value, rather than playing around with Chris@0: // #name. We also cast the #title to string as we will use it as an array Chris@0: // key and it may be a TranslatableMarkup. Chris@0: $button_title = !empty($triggering_element['#title']) ? (string) $triggering_element['#title'] : $trigger_key; Chris@0: if (empty($seen_buttons[$button_title])) { Chris@0: $wrapping_element[$button_key]['#value'] = t('Update "@title" choice', [ Chris@0: '@title' => $button_title, Chris@0: ]); Chris@0: $seen_buttons[$button_title] = 1; Chris@0: } Chris@0: else { Chris@0: $wrapping_element[$button_key]['#value'] = t('Update "@title" choice (@number)', [ Chris@0: '@title' => $button_title, Chris@0: '@number' => ++$seen_buttons[$button_title], Chris@0: ]); Chris@0: } Chris@0: Chris@0: // Attach custom data to the triggering element and submit button, so we can Chris@0: // use it in both the process function and AJAX callback. Chris@0: $ajax_data = [ Chris@0: 'wrapper' => $triggering_element['#ajax']['wrapper'], Chris@0: 'trigger_key' => $trigger_key, Chris@0: 'refresh_parents' => $refresh_parents, Chris@0: ]; Chris@0: $seen_ids[$triggering_element['#ajax']['wrapper']] = TRUE; Chris@0: $triggering_element['#views_ui_ajax_data'] = $ajax_data; Chris@0: $wrapping_element[$button_key]['#views_ui_ajax_data'] = $ajax_data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Processes a non-JavaScript fallback submit button to limit its validation errors. Chris@0: */ Chris@0: function views_ui_add_limited_validation($element, FormStateInterface $form_state) { Chris@0: // Retrieve the AJAX triggering element so we can determine its parents. (We Chris@0: // know it's at the same level of the complete form array as the submit Chris@0: // button, so all we have to do to find it is swap out the submit button's Chris@0: // last array parent.) Chris@0: $array_parents = $element['#array_parents']; Chris@0: array_pop($array_parents); Chris@0: $array_parents[] = $element['#views_ui_ajax_data']['trigger_key']; Chris@0: $ajax_triggering_element = NestedArray::getValue($form_state->getCompleteForm(), $array_parents); Chris@0: Chris@0: // Limit this button's validation to the AJAX triggering element, so it can Chris@0: // update the form for that change without requiring that the rest of the Chris@0: // form be filled out properly yet. Chris@0: $element['#limit_validation_errors'] = [$ajax_triggering_element['#parents']]; Chris@0: Chris@0: // If we are in the process of a form submission and this is the button that Chris@0: // was clicked, the form API workflow in \Drupal::formBuilder()->doBuildForm() Chris@0: // will have already copied it to $form_state->getTriggeringElement() before Chris@0: // our #process function is run. So we need to make the same modifications in Chris@0: // $form_state as we did to the element itself, to ensure that Chris@0: // #limit_validation_errors will actually be set in the correct place. Chris@0: $clicked_button = &$form_state->getTriggeringElement(); Chris@0: if ($clicked_button && $clicked_button['#name'] == $element['#name'] && $clicked_button['#value'] == $element['#value']) { Chris@0: $clicked_button['#limit_validation_errors'] = $element['#limit_validation_errors']; Chris@0: } Chris@0: Chris@0: return $element; Chris@0: } Chris@0: Chris@0: /** Chris@0: * After-build function that adds a wrapper to a form region (for AJAX refreshes). Chris@0: * Chris@0: * This function inserts a wrapper around the region of the form that needs to Chris@0: * be refreshed by AJAX, based on information stored in the corresponding Chris@0: * submit button form element. Chris@0: */ Chris@0: function views_ui_add_ajax_wrapper($element, FormStateInterface $form_state) { Chris@0: // Find the region of the complete form that needs to be refreshed by AJAX. Chris@0: // This was earlier stored in a property on the element. Chris@0: $complete_form = &$form_state->getCompleteForm(); Chris@0: $refresh_parents = $element['#views_ui_ajax_data']['refresh_parents']; Chris@0: $refresh_element = NestedArray::getValue($complete_form, $refresh_parents); Chris@0: Chris@0: // The HTML ID that AJAX expects was also stored in a property on the Chris@0: // element, so use that information to insert the wrapper
here. Chris@0: $id = $element['#views_ui_ajax_data']['wrapper']; Chris@0: $refresh_element += [ Chris@0: '#prefix' => '', Chris@0: '#suffix' => '', Chris@0: ]; Chris@0: $refresh_element['#prefix'] = '
' . $refresh_element['#prefix']; Chris@0: $refresh_element['#suffix'] .= '
'; Chris@0: Chris@0: // Copy the element that needs to be refreshed back into the form, with our Chris@0: // modifications to it. Chris@0: NestedArray::setValue($complete_form, $refresh_parents, $refresh_element); Chris@0: Chris@0: return $element; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Updates a part of the add view form via AJAX. Chris@0: * Chris@0: * @return Chris@0: * The part of the form that has changed. Chris@0: */ Chris@0: function views_ui_ajax_update_form($form, FormStateInterface $form_state) { Chris@0: // The region that needs to be updated was stored in a property of the Chris@0: // triggering element by views_ui_add_ajax_trigger(), so all we have to do is Chris@0: // retrieve that here. Chris@0: return NestedArray::getValue($form, $form_state->getTriggeringElement()['#views_ui_ajax_data']['refresh_parents']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Non-Javascript fallback for updating the add view form. Chris@0: */ Chris@0: function views_ui_nojs_submit($form, FormStateInterface $form_state) { Chris@0: $form_state->setRebuild(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add a