function QuickEditImageController::getField

Same name in this branch
  1. 9 core/modules/quickedit/src/Controller/QuickEditImageController.php \Drupal\quickedit\Controller\QuickEditImageController::getField()
Same name and namespace in other branches
  1. 8.9.x core/modules/image/src/Controller/QuickEditImageController.php \Drupal\image\Controller\QuickEditImageController::getField()

Returns JSON representing the current state of the field.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity of which an image field is being rendered.

string $field_name: The name of the (image) field that is being rendered

string $langcode: The language code of the field that is being rendered.

Return value

\Drupal\image\Plugin\Field\FieldType\ImageItem The field for this request.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException Throws an exception if the request is invalid.

2 calls to QuickEditImageController::getField()
QuickEditImageController::getInfo in core/modules/image/src/Controller/QuickEditImageController.php
Returns JSON representing an image field's metadata.
QuickEditImageController::upload in core/modules/image/src/Controller/QuickEditImageController.php
Returns JSON representing the new file upload, or validation errors.

File

core/modules/image/src/Controller/QuickEditImageController.php, line 220

Class

QuickEditImageController
Returns responses for our image routes.

Namespace

Drupal\image\Controller

Code

protected function getField(EntityInterface $entity, $field_name, $langcode) {
    // Ensure that this is a valid 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_list */
    $field_list = $entity->getTranslation($langcode)
        ->get($field_name);
    if (!$field_list) {
        throw new BadRequestHttpException('Requested Field does not exist.');
    }
    // If the list is empty, append an empty item to use.
    if ($field_list->isEmpty()) {
        $field = $field_list->appendItem();
    }
    else {
        $field = $entity->getTranslation($langcode)
            ->get($field_name)
            ->first();
    }
    // Ensure that the field is the type we expect.
    if (!$field instanceof ImageItem) {
        throw new BadRequestHttpException('Requested Field is not of type "image".');
    }
    return $field;
}

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