public function EditController::metadata

Returns the metadata for a set of fields.

Given a list of field 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 'EditController::metadata'
edit.routing.yml in drupal/core/modules/edit/edit.routing.yml
drupal/core/modules/edit/edit.routing.yml

File

drupal/core/modules/edit/lib/Drupal/edit/EditController.php, line 36
Contains of \Drupal\edit\EditController.

Class

EditController
Returns responses for Edit module routes.

Namespace

Drupal\edit

Code

public function metadata(Request $request) {
  $fields = $request->request
    ->get('fields');
  if (!isset($fields)) {
    throw new NotFoundHttpException();
  }
  $metadataGenerator = $this->container
    ->get('edit.metadata.generator');
  $metadata = array();
  foreach ($fields as $field) {
    list($entity_type, $entity_id, $field_name, $langcode, $view_mode) = explode('/', $field);

    // Load the entity.
    if (!$entity_type || !entity_get_info($entity_type)) {
      throw new NotFoundHttpException();
    }
    $entity = entity_load($entity_type, $entity_id);
    if (!$entity) {
      throw new NotFoundHttpException();
    }

    // Validate the field name and language.
    if (!$field_name || !($instance = field_info_instance($entity
      ->entityType(), $field_name, $entity
      ->bundle()))) {
      throw new NotFoundHttpException();
    }
    if (!$langcode || field_valid_language($langcode) !== $langcode) {
      throw new NotFoundHttpException();
    }
    $metadata[$field] = $metadataGenerator
      ->generate($entity, $instance, $langcode, $view_mode);
  }
  return new JsonResponse($metadata);
}