You are here

public function EntityFieldHelperTrait::getValue in Entity Share 8.3

Same name and namespace in other branches
  1. 8.2 tests/modules/entity_share_test/src/EntityFieldHelperTrait.php \Drupal\entity_share_test\EntityFieldHelperTrait::getValue()

Retrieve the value from a field.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The content entity.

string $field_name: The field to retrieve the value.

Return value

string The field value. Empty if there is no field with this machine name or if there is no value.

File

tests/modules/entity_share_test/src/EntityFieldHelperTrait.php, line 79

Class

EntityFieldHelperTrait
Provides helper functions to get field values.

Namespace

Drupal\entity_share_test

Code

public function getValue(ContentEntityInterface $entity, string $field_name) {
  $value = '';
  if ($entity
    ->hasField($field_name) && !$entity
    ->get($field_name)
    ->isEmpty()) {
    $value = $entity
      ->get($field_name)
      ->getValue()[0]['value'];
  }
  return (string) $value;
}