function QuickEditController::fieldForm

Same name in other branches
  1. 8.9.x core/modules/quickedit/src/QuickEditController.php \Drupal\quickedit\QuickEditController::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 'QuickEditController::fieldForm'
quickedit.routing.yml in core/modules/quickedit/quickedit.routing.yml
core/modules/quickedit/quickedit.routing.yml

File

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

Class

QuickEditController
Returns responses for Quick Edit module routes.

Namespace

Drupal\quickedit

Code

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') ?: [];
        $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 = [
                '#type' => 'status_messages',
            ];
            $response->addCommand(new FieldFormValidationErrorsCommand($this->renderer
                ->renderRoot($status_messages)));
        }
    }
    return $response;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.