Refer : // https://www.sitepoint.com/drupal-8-version-entityfieldquery/
Services Login/Logout
Custom login/logou/register
#ใน Drupal 8 การจะเช็กว่า User ใหน Login อยู่
$account = \Drupal::currentUser();
if ($account->id() == 1) {
return "Hiya, boss!";
}else {
return "You are not the site administrator.";
}
1. การ สร้าง Node ใหม่ && file
// Create Node Object ---------------------------- // Create file object from remote URI.
// การที่เรา post image file โดยเข้ารหัส base64
$data = base64_decode("xxx");
// ดึง file จาก URI
$data = file_get_contents('https://www.google.co.th/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');
// บันทึก ลงใน folder users(โดยที่เราจำเป็นต้องสร้างไว้ก่อน แล้วก็ chmod = 777 ด้วย)
$file = file_save_data($data, 'public://users/druplicon.png', FILE_EXISTS_REPLACE); // Create node object with attached file.
$node = Node::create([
'type' => 'article',
'title' => 'Druplicon test',
'body' => 'My Body',
'field_image' => [
'target_id' => $file->id(),
'alt' => 'Hello world',
'title' => 'Goodbye world'
],
]);
// บันทึก
$node->save();
2. การ Update Node เราจำเป็นต้องรู้ Node ID ด้วยถึงจะทำการ Update Node นั้นได้
// Update Node Object ----------------------------// เราจะทำการ Load Node ID = 52$node = Node::load(52);//set value for field$node->type = "article";$node->title = "Update Title - article";$node->body->value = 'My Body';$node->body->format = 'full_html'; //save to updatenode$node->save();3. ผมขอยกตัวอย่าง Module Profile3.1 การ Query$uid = 1; // User ID$query = \Drupal::entityQuery('profile') ->condition('uid', $uid); /* การ query จาก entity profile โดย condition uid = 1 */// จะได้ Node ID$nids = $query->execute();กรณีเราต้องการดึง field ของ entity == profile เราสามารถเรียกใช้งานได้ 2 แบบด้วยกัน// สามารถ load entity ได้ 2 แบบ1. $profile = Profile::load(2); // แบบที่ 12. $profile = entity_load("profile", reset($nids) /* reset จะเป้นการ ดึง ค่า index = 0 เป็นคำสั่งของ php นั้น เอง */); // แบบที่ 2// การ access value field// dsm($profile->get('field_parse_id')->getValue());// dsm($profile->get('field_parse_id')->getValue()[0]['value']);// กรณีต้องการ update ค่า$profile->field_parse_id = "654321";$profile->save();3.2 การ Create/* กรณียังไม่ได้ สร้าง Profile ให้สร้างให้ uid */$profile = Profile::create([ 'type' => 'main','uid' => $uid,'field_parse_id'=>array('value'=>'-x-'),]);$profile->save();4. Query Database4.1 ตัวอยา่งการ Query Database$query = \Drupal::entityQuery('node') ->condition('status', 1) ->condition("type", "article"); $nids = $query->execute();อธิบายเป็น การ Query Table = node โดยที่ status == 1 And type=='acticle' จากการ execute จากได้ array nide ทั่งหมดออกมาจากการที่เราได้ array nids ออกมาทั้งหมดแล้ว เราสามารถ load node มาใช้งานได้ 2 แบบแบบที่ 1$node = entity_load('node', 40); // 40 คือ node id ที่เราต้องการดึงออกมาใช้งานการอ้างอิง field สามารถทำได้ดังนี้dsm($node->get('nid')->getValue());dsm($node->get('type')->getValue()); dsm($node->get('body')->getValue()); dsm($node->get('langcode')->getValue());แบบที่ 2$nodes = entity_load_multiple('node', $nids);foreach ($nodes as $value) { dsm($value);dsm($value->get('nid')->getValue());dsm($value->get('type')->getValue());dsm($value->get('body')->getValue());dsm($value->get('langcode')->getValue()); }4.2 Join TableRefer : http://blog.danjuls.se/drupal-8-database-select-with-join/: http://drupal.stackexchange.com/questions/133553/how-to-get-all-rows-from-a-database-table$query = \Drupal::database()->select('node_revision', 'nr');$query->join('node', 'n', 'n.vid = nr.vid');$query->leftJoin('users_field_data', 'u', 'u.uid = nr.revision_uid');$query->fields('nr', ['revision_uid']) ->fields('u', ['name', 'mail']) ->condition('n.nid', 23);$result = $query->execute();while($record = $result->fetchAssoc()){print_r($record);}5. การหา path file จาก fidRefer : http://drupal.stackexchange.com/questions/105064/how-to-get-the-absolute-path-for-files-based-on-fid# การ get path file image$file = file_load(11 /* field_image_target_id, fid */);dsm($file->get('uri')->getValue());//public://2016-06/generateImage_fTIMI3.jpeg# Example การ ดึง URL ตัวอยา่ง เช่น http://localhost/sites/default/files/2016-06/generateImage_fTIMI3.jpeg$url = file_create_url("public://2016-06/generateImage_fTIMI3.jpeg");dsm($url);
ไม่มีความคิดเห็น:
แสดงความคิดเห็น