Skip to content

Instantly share code, notes, and snippets.

@radheymkumar
Created October 22, 2019 12:08
Show Gist options
  • Save radheymkumar/96e9b9e38dcb9774d505d2d00f08c0f8 to your computer and use it in GitHub Desktop.
Save radheymkumar/96e9b9e38dcb9774d505d2d00f08c0f8 to your computer and use it in GitHub Desktop.
Access field value for a node in Drupal
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 :
echo $node->id(); // 123
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)
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; // <-- this won't work
} else {
echo "The node doesn't have this paragraph type.";
}
Get a file by ID:
$fid = 42; // example value
Method 1
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
$file = $file_storage->load($fid);
Method 2
$file = \Drupal\file\Entity\File::load($ffid);
$fileSRC = file_create_url($file->getFileUri());
Get a file referenced in a node:
$file = $node->field_image->entity;
Reading some properties from a File entity:
echo $file->getFileUri(); // "public://file123.jpg"
// if you want the URL without Drupal's custom scheme, you can translate it to a plain URL:
echo file_url_transform_relative(file_create_url($file->getFileUri())); // "/sites/default/files/public/file123.jpg"
echo $file->filename->value; // "file123.jpg"
echo $file->filemime->value; // "image/jpeg"
echo $file->filesize->value; // 63518 (size in bytes)
echo $file->created->value; // 1511206249 (Unix timestamp)
echo $file->changed->value; // 1511234256 (Unix timestamp)
echo $file->id(); // 432
The file's user data
echo $file->uid->target_id; // 1
echo $file->uid->value; // <-- doesn't work. Use target_id instead.
echo $file->uid->entity->name->value; // "alice"
echo $file->uid->entity->timezone->value; // "America/Los_Angeles"
Get file id
$node->field_image->getValue();
Reading from entity reference fields that allow multiple values:
foreach ($node->field_my_entity_reference as $reference) {
echo $reference->target_id; // 1 (a node's nid)
echo $reference->entity->title->value; // "Moby Dick"
}
Populate the value of an entity reference field which allows multiple values (this replaces any existing value in the DB)
$nids = [3,4,5,6]; // example value
$node->set('field_my_entity_reference', $nids);
$node->save();
Append new referenced items to an entity reference field (this preserves existing values)
$nids = [3,4,5,6]; // example value
foreach ($nids as $nid) {
$node->field_my_entity_reference[] = [
'target_id' => $nid
];
}
$node->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment