ข้ามไปยังเนื้อหาหลัก
หน้าแรก
DrupalThailand

Main navigation

  • หน้าแรก
  • ฟอรั่ม
  • ประกาศงาน
  • บทความ
  • กิจกรรม
User account menu
  • เข้าสู่ระบบ

การแสดงเส้นทาง

  1. หน้าแรก
  2. บทความ

Drupal 8 Entity API cheat sheet (Part 1)

By admin, 11 พฤษภาคม, 2021
Drupal 8 Entity API cheat sheet

การทำงานเกี่ยวกับ 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

 

Tags

  • drupal
  • api
  • drupal8
  • Log in or register to post comments

Comments

Releases for Drupal core

drupal 9.3.17

Releases for Drupal core

drupal 9.4.1

Releases for Drupal core

drupal 10.1.x-dev

Releases for Drupal core
More posts about Releases for Drupal core

Tags

  • ธีม
  • docker
  • css
  • mac
  • drupal8
Powered by Drupal Thailand