Same name and namespace in other branches
- 8.x-1.x src/Controller/EntityReferenceAjaxController.php \Drupal\entity_reference_ajax_formatter\Controller\EntityReferenceAjaxController::viewField() 1 comment
Render the desired field into an AjaxResponse.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity to get the field from.
string $field_name: The field to display.
string $view_mode: The view mode to display the field with.
string $language: The language to get the field in.
int $start: Currently only used in the EntityReferenceAjaxFormatter.
?string $printed: Used when sorting and order isn't guaranteed to ensure no duplicates.
Return value
\Drupal\Core\Ajax\AjaxResponse The ajax response.
Throws
\Symfony\Component\HttpKernel\Exception\BadRequestHttpException If the field doesn't exist or not a Content Entity.
1 string reference to 'EntityReferenceAjaxController::viewField'
File
-
src/
Controller/ EntityReferenceAjaxController.php, line 60
Class
- EntityReferenceAjaxController
- Returns responses for our ajaxified entity reference route.
Namespace
Drupal\entity_reference_ajax_formatter\ControllerCode
public function viewField(EntityInterface $entity, string $field_name, string $view_mode, string $language, int $start, ?string $printed) {
// Ensure that this is a valid Content Entity.
if (!$entity instanceof ContentEntityInterface) {
throw new BadRequestHttpException('Requested Entity is not a Content Entity.');
}
// Check that this field exists.
/** @var \Drupal\Core\Field\FieldItemListInterface $field */
if (!($field = $entity->getTranslation($language)
->get($field_name))) {
throw new BadRequestHttpException('Requested Field does not exist.');
}
$response = new AjaxResponse();
$field_elements = $field->view($view_mode);
$response->addCommand(new ReplaceCommand("#ajax-field-{$entity->getEntityTypeId()}-{$entity->id()}-{$field_name}", $this->renderer
->render($field_elements)));
return $response;
}