You are here

class FieldDefinitionNormalizer in JSON:API Schema 8

Normalizer for FieldDefinitionInterface objects.

This normalizes the variant of data fields particular to the Field system. By accessing this via the FieldDefinitionInterface, there is greater access to some of the methods providing deeper schema properties.

Hierarchy

Expanded class hierarchy of FieldDefinitionNormalizer

1 string reference to 'FieldDefinitionNormalizer'
jsonapi_schema.services.yml in ./jsonapi_schema.services.yml
jsonapi_schema.services.yml
1 service uses FieldDefinitionNormalizer
serializer.normalizer.field_definition.schema_json in ./jsonapi_schema.services.yml
Drupal\jsonapi_schema\Normalizer\FieldDefinitionNormalizer

File

src/Normalizer/FieldDefinitionNormalizer.php, line 16

Namespace

Drupal\jsonapi_schema\Normalizer
View source
class FieldDefinitionNormalizer extends ListDataDefinitionNormalizer {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = FieldDefinitionInterface::class;

  /**
   * {@inheritdoc}
   */
  public function normalize($field_definition, $format = NULL, array $context = []) {
    assert($field_definition instanceof FieldDefinitionInterface);
    $cardinality = $field_definition
      ->getFieldStorageDefinition()
      ->getCardinality();
    $context['cardinality'] = $cardinality;
    $normalized = parent::normalize($field_definition, $format, $context);

    // Specify non-contextual default value as an example.
    $default_value = $field_definition
      ->getDefaultValueLiteral();
    $field_name = $context['name'];
    if (!empty($default_value)) {
      $field_type = $field_definition
        ->getType();
      $default_value = $cardinality == 1 ? reset($default_value) : $default_value;
      $default_value = count($default_value) == 1 ? reset($default_value) : $default_value;
      $default_value = $field_type == "boolean" ? boolval($default_value) : $default_value;
      NestedArray::setValue($normalized, [
        'properties',
        $field_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.
    if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && $cardinality != 1) {
      NestedArray::setValue($normalized, [
        'properties',
        $field_name,
        'maxItems',
      ], $cardinality);
    }
    return $normalized;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
DataDefinitionNormalizer::$format protected property The formats that the Normalizer can handle. Overrides NormalizerBase::$format
DataDefinitionNormalizer::$supportedDataTypes protected property The supported data type. 6
DataDefinitionNormalizer::denormalize public function
DataDefinitionNormalizer::extractPropertyData protected function Extracts property details from a data definition. 6
DataDefinitionNormalizer::JSON_TYPES constant
DataDefinitionNormalizer::normalizeProperties protected function Normalize an array of data definitions.
DataDefinitionNormalizer::requiredProperty protected function Determine if the given property is a required element of the schema.
DataDefinitionNormalizer::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
DataDefinitionNormalizer::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization 1
FieldDefinitionNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides ListDataDefinitionNormalizer::$supportedInterfaceOrClass
FieldDefinitionNormalizer::normalize public function Overrides ListDataDefinitionNormalizer::normalize
ListDataDefinitionNormalizer::isReferenceField protected function Determine if the current field is a reference field.
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2