function ResourceObject::getLabelFieldName

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::getLabelFieldName()
  2. 10 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::getLabelFieldName()
  3. 11.x core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::getLabelFieldName()

Determines the entity type's (internal) label field name.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity from which fields should be extracted.

Return value

string The label field name.

1 call to ResourceObject::getLabelFieldName()
LabelOnlyResourceObject::extractFieldsFromEntity in core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php
Extracts the entity's fields.

File

core/modules/jsonapi/src/JsonApiResource/ResourceObject.php, line 315

Class

ResourceObject
Represents a JSON:API resource object.

Namespace

Drupal\jsonapi\JsonApiResource

Code

protected static function getLabelFieldName(EntityInterface $entity) {
    $label_field_name = $entity->getEntityType()
        ->getKey('label');
    // Special handling for user entities that allows a JSON:API user agent to
    // access the display name of a user. This is useful when displaying the
    // name of a node's author.
    // @see \Drupal\jsonapi\JsonApiResource\ResourceObject::extractContentEntityFields()
    // @todo: eliminate this special casing in https://www.drupal.org/project/drupal/issues/3079254.
    if ($entity->getEntityTypeId() === 'user') {
        $label_field_name = 'display_name';
    }
    return $label_field_name;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.