You are here

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

Retrieve the value from a field.

Properties not in the expected structure are removed.

Parameters

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

string $field_name: The field to retrieve the value.

array $structure: The initial data structure.

Return value

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

1 call to EntityFieldHelperTrait::getFilteredStructureValues()
EntityShareClientFunctionalTestBase::checkCreatedEntities in modules/entity_share_client/tests/src/Functional/EntityShareClientFunctionalTestBase.php
Helper function that test that the entities had been recreated.

File

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

Class

EntityFieldHelperTrait
Provides helper functions to get field values.

Namespace

Drupal\entity_share_test

Code

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

    // Remove unexpected properties.
    foreach ($values as $delta => $value) {
      $values[$delta] = array_intersect_key($value, array_flip($structure));
    }
  }
  return $values;
}