trait EntityFieldHelperTrait in Entity Share 8.3
Same name and namespace in other branches
- 8.2 tests/modules/entity_share_test/src/EntityFieldHelperTrait.php \Drupal\entity_share_test\EntityFieldHelperTrait
Provides helper functions to get field values.
Hierarchy
- trait \Drupal\entity_share_test\EntityFieldHelperTrait
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_testView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityFieldHelperTrait:: |
public | function | Retrieve the value from a field. | |
EntityFieldHelperTrait:: |
public | function | Retrieve the value from a field where the value key is target_id. | |
EntityFieldHelperTrait:: |
public | function | Retrieve the value from a field. | |
EntityFieldHelperTrait:: |
public | function | Retrieve the value from a field. |