แสดงชื่อเต็มของผู้เขียนเนื้อหา

sugree's picture

ปกติ Drupal ไม่ได้เก็บชื่อเต็ม ถ้าอยากให้เก็บต้อง

  1. เปิดโมดูล profile แล้วสร้างฟิลด์ fullname ให้เป็น required
  2. สร้างฟังก์ชั่น phptemplate_username($object) ใน template.php ในไดเรกทอรีของ theme
function phptemplate_username($object) {
 
  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }
    if ($object->fullname) {
      if (drupal_strlen($object->fullname) > 20) {
        $name = drupal_substr($object->fullname, 0, 15) .'...';
      }
      else {
        $name = $object->fullname;
      }
    }
 
    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }
 
    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', t('Anonymous'));
  }
 
  return $output;
}

ผมไม่ได้ลองจริง แต่น่าจะทำงานได้ตามทฤษฎี

Blue Rabbit's picture

ผมลองแล้วยังไม่ได้ครับ

สร้างฟังก์ชันนี้ใน template.php แล้ว
ต้องแก้ไขใน node.tpl.php ด้วยหรือไม่ครับ

><?php print $submitted?></blockcode>

sugree's picture

ใช้โมดูล profile หรือยังครับ เพิ่มฟิลด์ชื่อ fullname

Blue Rabbit's picture

ใช้แล้วครับ

แล้วมีวิธีเรียก ชื่อเต็ม (ค่าในฟิลด์ fullname) มาแสดงใน node.tpl.php โดยไม่ต้องเพิ่มฟังก์ชันใน template.php ได้มั้ยครับ

sugree's picture

ที่แท้ผมดูผิดนี่เอง $object คือ $node นี่นา

function phptemplate_username($object) {
 
  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }
    $profile = user_load(array('uid' => $object->uid));
    if ($profile->fullname) {
      if (drupal_strlen($profile->fullname) > 20) {
        $name = drupal_substr($profile->fullname, 0, 15) .'...';
      }
      else {
        $name = $profile->fullname;
      }
    }
 
    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }
 
    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', t('Anonymous'));
  }
 
  return $output;
}
Blue Rabbit's picture

ใช้ได้แล้วครับ... ขอบคุณมากๆ ครับ

mk's picture

เขียนสดรึนี่!!

ผมสงสัยว่าทำไมเราเช็คความยาวที่ 20 แต่ substr ที่ 15

sugree's picture

เพราะมันใส่ ... ข้างหลังแล้วก็เผื่อโน่นเผื่อนี่อีก 2 ตัว

ไม่ได้เขียนสดทั้งหมดหรอก เอามาจาก theme_username() แล้วเพิ่มเข้าไปอีกไม่กี่บรรทัด

ขอบคุณหลายๆ ครับ แล้วจะลองดู

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น