Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Get field value of a Node / Entity

By editor , 2 December, 2015
Question

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

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']];

 

Tags

  • Drupal 8
  • Code
  • Node
Create a field in a node entity programmatically on Drupal 8.

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer