You are here

public function QuickEditController::metadata in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/quickedit/src/QuickEditController.php \Drupal\quickedit\QuickEditController::metadata()
  2. 9 core/modules/quickedit/src/QuickEditController.php \Drupal\quickedit\QuickEditController::metadata()

Returns the metadata for a set of fields.

Given a list of field quick edit IDs as POST parameters, run access checks on the entity and field level to determine whether the current user may edit them. Also retrieves other metadata.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

1 string reference to 'QuickEditController::metadata'
quickedit.routing.yml in core/modules/quickedit/quickedit.routing.yml
core/modules/quickedit/quickedit.routing.yml

File

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

Class

QuickEditController
Returns responses for Quick Edit module routes.

Namespace

Drupal\quickedit

Code

public function metadata(Request $request) {
  if (!$request->request
    ->has('fields')) {
    throw new NotFoundHttpException();
  }
  $fields = $request->request
    ->all('fields');
  $entities = $request->request
    ->all('entities');
  $metadata = [];
  foreach ($fields as $field) {
    [
      $entity_type,
      $entity_id,
      $field_name,
      $langcode,
      $view_mode,
    ] = explode('/', $field);

    // Load the entity.
    if (!$entity_type || !$this
      ->entityTypeManager()
      ->getDefinition($entity_type)) {
      throw new NotFoundHttpException();
    }
    $entity = $this
      ->entityTypeManager()
      ->getStorage($entity_type)
      ->load($entity_id);
    if (!$entity) {
      throw new NotFoundHttpException();
    }

    // Validate the field name and language.
    if (!$field_name || !$entity
      ->hasField($field_name)) {
      throw new NotFoundHttpException();
    }
    if (!$langcode || !$entity
      ->hasTranslation($langcode)) {
      throw new NotFoundHttpException();
    }
    $entity = $entity
      ->getTranslation($langcode);

    // If the entity information for this field is requested, include it.
    $entity_id = $entity
      ->getEntityTypeId() . '/' . $entity_id;
    if (is_array($entities) && in_array($entity_id, $entities) && !isset($metadata[$entity_id])) {
      $metadata[$entity_id] = $this->metadataGenerator
        ->generateEntityMetadata($entity);
    }
    $metadata[$field] = $this->metadataGenerator
      ->generateFieldMetadata($entity
      ->get($field_name), $view_mode);
  }
  return new JsonResponse($metadata);
}