You are here

public function ContentEntityNormalizer::normalize in Zircon Profile 8

Same name in this branch
  1. 8 core/modules/hal/src/Normalizer/ContentEntityNormalizer.php \Drupal\hal\Normalizer\ContentEntityNormalizer::normalize()
  2. 8 core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php \Drupal\serialization\Normalizer\ContentEntityNormalizer::normalize()
Same name and namespace in other branches
  1. 8.0 core/modules/hal/src/Normalizer/ContentEntityNormalizer.php \Drupal\hal\Normalizer\ContentEntityNormalizer::normalize()

Normalizes an object into a set of arrays/scalars.

Parameters

object $object object to normalize:

string $format format the normalization result will be encoded as:

array $context Context options for the normalizer:

Return value

array|string|bool|int|float|null

Overrides NormalizerInterface::normalize

1 call to ContentEntityNormalizer::normalize()
FileEntityNormalizer::normalize in core/modules/hal/src/Normalizer/FileEntityNormalizer.php
Normalizes an object into a set of arrays/scalars.
1 method overrides ContentEntityNormalizer::normalize()
FileEntityNormalizer::normalize in core/modules/hal/src/Normalizer/FileEntityNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

core/modules/hal/src/Normalizer/ContentEntityNormalizer.php, line 66
Contains \Drupal\hal\Normalizer\ContentEntityNormalizer.

Class

ContentEntityNormalizer
Converts the Drupal entity object structure to a HAL array structure.

Namespace

Drupal\hal\Normalizer

Code

public function normalize($entity, $format = NULL, array $context = array()) {
  $context += array(
    'account' => NULL,
    'included_fields' => NULL,
  );

  // Create the array of normalized fields, starting with the URI.

  /** @var $entity \Drupal\Core\Entity\ContentEntityInterface */
  $normalized = array(
    '_links' => array(
      'self' => array(
        'href' => $this
          ->getEntityUri($entity),
      ),
      'type' => array(
        'href' => $this->linkManager
          ->getTypeUri($entity
          ->getEntityTypeId(), $entity
          ->bundle(), $context),
      ),
    ),
  );

  // If the fields to use were specified, only output those field values.
  // Otherwise, output all field values except internal ID.
  if (isset($context['included_fields'])) {
    $fields = array();
    foreach ($context['included_fields'] as $field_name) {
      $fields[] = $entity
        ->get($field_name);
    }
  }
  else {
    $fields = $entity
      ->getFields();
  }

  // Ignore the entity ID and revision ID.
  $exclude = array(
    $entity
      ->getEntityType()
      ->getKey('id'),
    $entity
      ->getEntityType()
      ->getKey('revision'),
  );
  foreach ($fields as $field) {

    // Continue if this is an excluded field or the current user does not have
    // access to view it.
    if (in_array($field
      ->getFieldDefinition()
      ->getName(), $exclude) || !$field
      ->access('view', $context['account'])) {
      continue;
    }
    $normalized_property = $this->serializer
      ->normalize($field, $format, $context);
    $normalized = NestedArray::mergeDeep($normalized, $normalized_property);
  }
  return $normalized;
}