Get field value of a Node / Entity

Submitted by editor on Wed, 12/02/2015 - 14:06
Question

How to get the field value of a node / Entity ?

Methode 1 : Example:

$value = $node->get($field)->value;

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();

 

Add new comment