logo

How to access field value for a node in Drupal 8

22
April

How to access field value for a node in Drupal 8
By: Anonymous | Published On: Wed, 04/22/2020 - 22:13

Use this code at the top of your coding

use Drupal\node\Entity\Node		//Working with nodes Load a node by NID:
	$nid = 123;     // example value

Method 1

	$node_storage = \Drupal::entityTypeManager()->getStorage('node');
	$node = $node_storage->load($nid);

Method 2

$node = \Drupal\node\Entity\Node::load($nid);

Get node's NID:

echo $node->id();  // 123Get node's bundle type:

	echo $node->bundle();    // 'article'
	echo $node->getType();    // 'article' - this is the same as bundle() for most entities,

Get a built-in field value: Method 1

	echo $node->get('title')->value;           // "Lorem Ipsum..."
	echo $node->get('created')->value;         // 1510948801
	echo $node->get('body')->value;            // "The full node body, with HTML"
	echo $node->get('body')->summary;          // "This is the summary"
	// a custom text field
	echo $node->get('field_foo')->value;       // "whatever is in your custom field"
	// a file field
	echo $node->get('field_image')->target_id; // 432 (a managed file FID)
    

To get the complete URL of image field

$imgurl = file_create_url($node->get('field_image')->entity->uri->value);

Method 2

	
	echo $node->title->value;            // "Lorem Ipsum..."
	echo $node->created->value;          // 1510948801
	echo $node->body->value;             // "This is the full node body, with HTML"
	echo $node->body->summary;           // "This is the summary"
	echo $node->field_foo->value;        // "whatever is in your custom field"
	echo $node->field_image->target_id;  // 432

Get nodes from a query: get multile nodes values using query and loop, execute other activities for this node

	$query = \Drupal::entityQuery('node')
	->condition('type', 'article'),
	->condition('field_foo', 42);
	$nids = $query->execute();
	$nodes = $node_storage->loadMultiple($nids);

	foreach ($nodes as $n) {
	echo $n->title->value;            // "Lorem Ipsum..."

	// do whatever you would do with a node object (set fields, save, etc.)
	$n->set('title', "this is a test");
	$n->save();
	}
	

Set fields value programatically for node

	$node->set('title', "Moby Dick");
	$node->set('body', array(
	'summary' => "Book about a whale",
	'value' => "Call me Ishmael...",
	'format' => 'basic_html',
	));
	$node->save();

Delete node programatically

	// one
	$nid = 42;      // example value
	$node = $node_storage->load($nid);
	$node->delete();
	 
	// multiple
	$nids = [21,12,45,67];  // example value
	$nodes = $node_storage->loadMultiple($nids);
	$node_storage->delete($nodes);
	 
	// multiple, loading one at a time to avoid "out of memory" errors - may be slow
	$nids = [21,12,45,67];  // example value
	foreach($nids as $nid)
	{
	  $node = $node_storage->load($nid);
	  $node->delete();
	}

Working with Paragraphs Paragraphs" (from the popular contrib module of the same name) are separate entities that are related to the parent nodes via an entity reference revision.

	$my_paragraph = null;
	foreach ($node->get('field_paragraph_reference') as $para) {
	  if ($para->entity->getType() == 'your_paragraph_type') {   // e.g., "main_content" or "social_media"
		$my_paragraph = $para->entity;
	  }
	}
	 
	if (!empty($my_paragraph)) {
	  // $my_paragraph is a regular entity and can be interacted with like any other entity
	  echo $my_paragraph->field_somefield->value;
	 
	  // (however, they don't have a "title" like a node)
	  echo $my_paragraph->title->value;  // 

Get node created date and time

		$createddate = $node->getCreatedTime();
		$node_createddate = \Drupal::service('date.formatter')->format($createddate, 'd-M-Y h:i A'); // date formate you can change as your requirement
		

Get node updated or node changed date time

		$updateddate = $node->getChangedTime();
		$node_updateddate = \Drupal::service('date.formatter')->format($updateddate, 'd-M-Y h:i A'); // date formate you can change as your requirement
		

You need to use this line in head for date format

use Drupal\Core\Datetime\DrupalDateTime;

Need Help ?