QuickEditController::fieldForm

public QuickEditController::fieldForm(EntityInterface $entity, $field_name, $langcode, $view_mode_id, Request $request)

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.

File

core/modules/quickedit/src/QuickEditController.php, line 174

Class

QuickEditController
Returns responses for Quick Edit module routes.

Namespace

Drupal\quickedit

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public function fieldForm(EntityInterface $entity, $field_name, $langcode, $view_mode_id, Request $request) {
  $response = new AjaxResponse();
 
  // Replace entity with PrivateTempStore copy if available and not resetting,
  // init PrivateTempStore copy otherwise.
  $tempstore_entity = $this->tempStoreFactory->get('quickedit')->get($entity->uuid());
  if ($tempstore_entity && $request->request->get('reset') !== 'true') {
    $entity = $tempstore_entity;
  }
  else {
    $this->tempStoreFactory->get('quickedit')->set($entity->uuid(), $entity);
  }
 
  $form_state = (new FormState())
    ->set('langcode', $langcode)
    ->disableRedirect()
    ->addBuildInfo('args', [$entity, $field_name]);
  $form = $this->formBuilder()->buildForm('Drupal\quickedit\Form\QuickEditFieldForm', $form_state);
 
  if ($form_state->isExecuted()) {
    // The form submission saved the entity in PrivateTempStore. Return the
    // updated view of the field from the PrivateTempStore copy.
    $entity = $this->tempStoreFactory->get('quickedit')->get($entity->uuid());
 
    // Closure to render the field given a view mode.
    $render_field_in_view_mode = function($view_mode_id) use ($entity, $field_name, $langcode) {
      return $this->renderField($entity, $field_name, $langcode, $view_mode_id);
    };
 
    // Re-render the updated field.
    $output = $render_field_in_view_mode($view_mode_id);
 
    // Re-render the updated field for other view modes (i.e. for other
    // instances of the same logical field on the user's page).
    $other_view_mode_ids = $request->request->get('other_view_modes') ? : array();
    $other_view_modes = array_map($render_field_in_view_mode, array_combine($other_view_mode_ids, $other_view_mode_ids));
 
    $response->addCommand(new FieldFormSavedCommand($output, $other_view_modes));
  }
  else {
    $output = $this->renderer->renderRoot($form);
    // When working with a hidden form, we don't want its CSS/JS to be loaded.
    if ($request->request->get('nocssjs') !== 'true') {
      $response->setAttachments($form['#attached']);
    }
    $response->addCommand(new FieldFormCommand($output));
 
    $errors = $form_state->getErrors();
    if (count($errors)) {
      $status_messages = array(
        '#type' => 'status_messages'
      );
      $response->addCommand(new FieldFormValidationErrorsCommand($this->renderer->renderRoot($status_messages)));
    }
  }
 
  return $response;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.