You are here

trait EntityFieldHelperTrait in Entity Share 8.2

Same name and namespace in other branches
  1. 8.3 tests/modules/entity_share_test/src/EntityFieldHelperTrait.php \Drupal\entity_share_test\EntityFieldHelperTrait

Provides helper functions to get field values.

Hierarchy

1 file declares its use of EntityFieldHelperTrait
EntityShareClientFunctionalTestBase.php in modules/entity_share_client/tests/src/Functional/EntityShareClientFunctionalTestBase.php

File

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

Namespace

Drupal\entity_share_test
View source
trait EntityFieldHelperTrait {

  /**
   * Retrieve the value from a field.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The content entity.
   * @param string $field_name
   *   The field to retrieve the value.
   *
   * @return array
   *   The field values. Empty if there is no field with this machine name or if
   *   there is no value.
   */
  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;
  }

  /**
   * Retrieve the value from a field.
   *
   * Properties not in the expected structure are removed.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The content entity.
   * @param string $field_name
   *   The field to retrieve the value.
   * @param array $structure
   *   The initial data structure.
   *
   * @return array
   *   The field values. Empty if there is no field with this machine name or if
   *   there is no value.
   */
  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;
  }

  /**
   * Retrieve the value from a field.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The content entity.
   * @param string $field_name
   *   The field to retrieve the value.
   *
   * @return string
   *   The field value. Empty if there is no field with this machine name or if
   *   there is no value.
   */
  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;
  }

  /**
   * Retrieve the value from a field where the value key is target_id.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The content entity.
   * @param string $field_name
   *   The field to retrieve the value.
   *
   * @return string
   *   The field value. Empty if there is no field with this machine name or if
   *   there is no value.
   */
  public function getTargetId(ContentEntityInterface $entity, string $field_name) {
    $value = '';
    if ($entity
      ->hasField($field_name) && !$entity
      ->get($field_name)
      ->isEmpty()) {
      $value = $entity
        ->get($field_name)
        ->getValue()[0]['target_id'];
    }
    return (string) $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityFieldHelperTrait::getFilteredStructureValues public function Retrieve the value from a field.
EntityFieldHelperTrait::getTargetId public function Retrieve the value from a field where the value key is target_id.
EntityFieldHelperTrait::getValue public function Retrieve the value from a field.
EntityFieldHelperTrait::getValues public function Retrieve the value from a field.