You are here

public function EntityFieldHelperTrait::getValues 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::getValues()

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

array The field values. 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 26

Class

EntityFieldHelperTrait
Provides helper functions to get field values.

Namespace

Drupal\entity_share_test

Code

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