การทำงานเกี่ยวกับ nodes
โหลด Node โดย nid:
$nid = 123; // ตัวอย่าง nid
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load($nid);
Get ค่า nid:
echo $node->id(); // 123
Get ค่า bundle type (หรือ content type):
echo $node->bundle(); // 'article'
echo $node->getType(); // 'article' - ทำงานเหมือนกับ bundle(), แต่ไม่แนะนำให้ใช้งาน
get ค่าของ field:
echo $node->get('title')->value; // "Lorem Ipsum..."
echo $node->get('created')->value; // 1510948801
echo $node->get('body')->value; // "The full node body, <strong>with HTML</strong>"
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)
get ค่าของ field อีกรูปแปป:
echo $node->title->value; // "Lorem Ipsum..."
echo $node->created->value; // 1510948801
echo $node->body->value; // "This is the full node body, <strong>with HTML</strong>"
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
Comments