How to get the field value of a node / Entity ?
Question
Methode 1 : Example:
$value = $node->get($field)->value;
$target = $node->get($field)->target_id; // For entity ref
Methode 2 Example:
$field = 'field_thefieldname';
$index = 0;
$a = $node->toArray();
if (isset($a[$field][$index]['value'])) {
$value = $a[$field][$index]['value'];
}
Example 3 : Get / Render image (Show current user's picture)
$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$renderd_image = $user->get('user_picture')->first()->view();
Or for multiple values
foreach ($node->get('field_images')->getValue() as $key => $image) {
$image = $node->field_images[$key]->view($settings);
}
//Example Of $settings
$settings = ['settings' => ['image_style' => 'thumbnail']];
Comments