Yêu cầu thg 1 9, 2022 11:45 SA 98 0 0
  • 98 0 0
+1

Em có "sao chép" được 1 đoạn code để truy vấn dữ liệu và xuất ra dạng json trong Drupal 7 nhưng lại không hiểu rõ nó, mong các cao nhân giúp em ạ.

Chia sẻ
  • 98 0 0

Em có thể hiểu là đoạn code này có 2 function để lấy ra toàn bộ và 1 quyển sách theo id nhưng em không biết nó đang dùng method gì như: post, get, ... cũng như phải khai báo chỗ nào, mong các cao nhân giúp em

<?php

/**
 * Implements hook_menu().
 */
function book_menu()
{
  $item = array();

  $item['api/v1/books'] = array(
    'title' => 'Books',
    'description' => 'list data of books',
    'page callback' => 'book_all',
    'access callback' => TRUE,
  );

  $item['api/v1/books/%'] = array(
    'title' => 'Books',
    'description' => 'data of a book',
    'page callback' => 'book_index',
    'page arguments' => array(3),
    'access callback' => TRUE,
  );

  return $item;
}

/**
 * selecting a book by nid
 */
function book_index($args = '')
{
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'book')
    ->propertyCondition('status', 1)
    ->propertyCondition('nid', $args)->pager(1);
  $result = $query->execute();
  if (isset($result['node'])) {
    $items_nids = array_keys($result['node']);
    $items = entity_load('node', $items_nids);
    drupal_json_output($items);
  }
}

/**
 * selecting all books
 */
function book_all()
{
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'book')
    ->propertyCondition('status', 1)->pager(2);
  $result = $query->execute();
  if (isset($result['node'])) {
    $items_nids = array_keys($result['node']);
    $items = entity_load('node', $items_nids);
    drupal_json_output($items);
  }
}
Avatar TAMIX @tuananhbfs
thg 1 10, 2022 1:24 SA

Bạn tìm Class EntityFieldQuery trong source code và đọc tất cả những gì trong đó.

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí