includes/pages.inc | 27 ++++------ lib/Drupal/edit/Ajax/BaseCommand.php | 52 ++++++++++++++++++++ lib/Drupal/edit/Ajax/FieldFormCommand.php | 27 ++++++++++ lib/Drupal/edit/Ajax/FieldFormSavedCommand.php | 28 +++++++++++ lib/Drupal/edit/Ajax/FieldFormValidationErrors.php | 28 +++++++++++ 5 files changed, 145 insertions(+), 17 deletions(-) diff --git a/includes/pages.inc b/includes/pages.inc index d29e87b..dfaae0f 100644 --- a/includes/pages.inc +++ b/includes/pages.inc @@ -4,6 +4,10 @@ * AJAX endpoints to retrieve & save subforms for fields and re-render fields. */ +use Drupal\Core\Ajax\AjaxResponse; +use Drupal\edit\Ajax\FieldFormCommand; +use Drupal\edit\Ajax\FieldFormSavedCommand; +use Drupal\edit\Ajax\FieldFormValidationErrorsCommand; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -24,6 +28,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * A render array. */ function edit_field_edit($entity_type, $entity_id, $field_name, $langcode, $view_mode) { + $response = new AjaxResponse(); + // Ensure the entity type is valid. if (empty($entity_type)) { throw new NotFoundHttpException(); @@ -68,7 +74,6 @@ function edit_field_edit($entity_type, $entity_id, $field_name, $langcode, $view form_load_include($form_state, 'inc', 'edit', 'includes/form'); $form = drupal_build_form('edit_field_form', $form_state); - $id = "$entity_type:$entity_id:$field_name:$langcode:$view_mode"; if (!empty($form_state['executed'])) { // Retrieve the updated entity, save it and render only the modified field. $entity = $form_state['entity']; @@ -77,26 +82,14 @@ function edit_field_edit($entity_type, $entity_id, $field_name, $langcode, $view field_attach_prepare_view($entity->entityType(), array($entity->id() => $entity), $view_mode, $langcode, $options); $output = field_attach_view($entity->entityType(), $entity, $view_mode, $langcode, $options); - $commands[] = array( - 'command' => 'edit_field_form_saved', - 'id' => $id, - 'data' => drupal_render($output), - ); + $response->addCommand(new FieldFormSavedCommand(drupal_render($output))); } else { - $commands[] = array( - 'command' => 'edit_field_form', - 'id' => $id, - 'data' => drupal_render($form), - ); + $response->addCommand(new FieldFormCommand(drupal_render($form))); $errors = form_get_errors(); if (count($errors)) { - $commands[] = array( - 'command' => 'edit_field_form_validation_errors', - 'id' => $id, - 'data' => theme('status_messages'), - ); + $response->addCommand(new FieldFormValidationErrorsCommand(theme('status_messages'))); } } @@ -106,7 +99,7 @@ function edit_field_edit($entity_type, $entity_id, $field_name, $langcode, $view drupal_static_reset('drupal_add_js'); } - return array('#type' => 'ajax', '#commands' => $commands); + return $response; } /** diff --git a/lib/Drupal/edit/Ajax/BaseCommand.php b/lib/Drupal/edit/Ajax/BaseCommand.php new file mode 100644 index 0000000..32d325d --- /dev/null +++ b/lib/Drupal/edit/Ajax/BaseCommand.php @@ -0,0 +1,52 @@ +command = $command; + $this->data = $data; + } + + /** + * Implements Drupal\Core\Ajax\CommandInterface:render(). + */ + public function render() { + return array( + 'command' => $this->command, + 'data' => $this->data, + ); + } + +} diff --git a/lib/Drupal/edit/Ajax/FieldFormCommand.php b/lib/Drupal/edit/Ajax/FieldFormCommand.php new file mode 100644 index 0000000..0f53b48 --- /dev/null +++ b/lib/Drupal/edit/Ajax/FieldFormCommand.php @@ -0,0 +1,27 @@ +