public function EditController::fieldForm

Returns a single field edit form as an Ajax response.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity being edited.

string $field_name: The name of the field that is being edited.

string $langcode: The name of the language for which the field is being edited.

string $view_mode_id: The view mode the field should be rerendered in.

\Symfony\Component\HttpFoundation\Request $request: The current request object containing the search string.

Return value

\Drupal\Core\Ajax\AjaxResponse The Ajax response.

1 string reference to 'EditController::fieldForm'
edit.routing.yml in drupal/core/modules/edit/edit.routing.yml
drupal/core/modules/edit/edit.routing.yml

File

drupal/core/modules/edit/lib/Drupal/edit/EditController.php, line 110
Contains of \Drupal\edit\EditController.

Class

EditController
Returns responses for Edit module routes.

Namespace

Drupal\edit

Code

public function fieldForm(EntityInterface $entity, $field_name, $langcode, $view_mode_id, Request $request) {
  $response = new AjaxResponse();
  $form_state = array(
    'langcode' => $langcode,
    'no_redirect' => TRUE,
    'build_info' => array(
      'args' => array(
        $entity,
        $field_name,
      ),
    ),
  );
  $form = drupal_build_form('edit_field_form', $form_state);
  if (!empty($form_state['executed'])) {

    // The form submission took care of saving the updated entity. Return the
    // updated view of the field.
    $entity = entity_load($form_state['entity']
      ->entityType(), $form_state['entity']
      ->id(), TRUE);

    // @todo Remove when http://drupal.org/node/1346214 is complete.
    $entity = $entity
      ->getBCEntity();
    $output = field_view_field($entity, $field_name, $view_mode_id, $langcode);
    $response
      ->addCommand(new FieldFormSavedCommand(drupal_render($output)));
  }
  else {
    $response
      ->addCommand(new FieldFormCommand(drupal_render($form)));
    $errors = form_get_errors();
    if (count($errors)) {
      $status_messages = array(
        '#theme' => 'status_messages',
      );
      $response
        ->addCommand(new FieldFormValidationErrorsCommand(drupal_render($status_messages)));
    }
  }

  // When working with a hidden form, we don't want any CSS or JS to be loaded.
  if ($request->request
    ->get('nocssjs') === 'true') {
    drupal_static_reset('drupal_add_css');
    drupal_static_reset('drupal_add_js');
  }
  return $response;
}