You are here

public function FieldDefinitionNormalizer::normalize in Schemata 8

Same name in this branch
  1. 8 schemata_json_schema/src/Normalizer/jsonapi/FieldDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\jsonapi\FieldDefinitionNormalizer::normalize()
  2. 8 schemata_json_schema/src/Normalizer/json/FieldDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\FieldDefinitionNormalizer::normalize()

Overrides ListDataDefinitionNormalizer::normalize

File

schemata_json_schema/src/Normalizer/json/FieldDefinitionNormalizer.php, line 27

Class

FieldDefinitionNormalizer
Normalizer for FieldDefinitionInterface objects.

Namespace

Drupal\schemata_json_schema\Normalizer\json

Code

public function normalize($entity, $format = NULL, array $context = []) {

  /* @var $entity \Drupal\Core\Field\FieldDefinitionInterface */
  $normalized = parent::normalize($entity, $format, $context);

  // Specify non-contextual default value as an example.
  $default_value = $entity
    ->getDefaultValueLiteral();
  if (!empty($default_value)) {
    $normalized['properties'][$context['name']]['default'] = $default_value;
  }

  // The cardinality is the configured maximum number of values the field can
  // contain. If unlimited, we do not include a maxItems attribute.
  $cardinality = $entity
    ->getFieldStorageDefinition()
    ->getCardinality();
  if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
    $normalized['properties'][$context['name']]['maxItems'] = $cardinality;
  }
  return $normalized;
}