Tampilkan ID simpul beserta judul di daftar Entitas Rujukan Autocomplete

8

Saya ingin menambahkan fungsionalitas ini ke widget pelengkapan otomatis di bidang Entityreference untuk menampilkan ID simpul di sebelah judul dalam daftar dropdown. Alasan di balik gagasan itu adalah untuk membedakan antara beberapa node dengan judul yang sama.

Contoh:

  • Ini adalah judul (3)
  • Ini adalah judul (2)
  • Ini adalah judul (1)

Saya tahu ID simpul ditampilkan setelah pemilihan dilakukan tetapi saya ingin menunjukkannya di daftar dropdown untuk memilih simpul yang tepat dengan cepat berdasarkan ID simpul.

neemu
sumber
@ oksana-c periksa jawaban saya dengan cara mudah lainnya
Adrian Cid Almaguer

Jawaban:

20

Pasang modul Tampilan dan Entitas Referensi , buat Tampilan baru dan Tambahkan Tampilan Referensi Entitas:

masukkan deskripsi gambar di sini

Kemudian Tambahkan di bidang judul konten dan nid, klik di nid dan centang Kecualikan dari tampilan, Simpan dan klik dalam judul dan pergi untuk menulis ulang output dari judul sebagai [title] - ([nid])

masukkan deskripsi gambar di sini masukkan deskripsi gambar di sini

Pergi untuk mengedit pengaturan format dan memeriksa judul, ini akan memungkinkan Anda untuk mencari berdasarkan judul.

masukkan deskripsi gambar di sini

Simpan tampilan.

Pergi untuk mengedit bidang Referensi Entitas Anda dan pilih di Mode Views: .... (seperti gambar berikut) dan pilih View Anda (dalam hal ini namanya adalah: articles_with_id) dan simpan pengaturan:

masukkan deskripsi gambar di sini

Lalu pergi untuk melihat hasilnya:

masukkan deskripsi gambar di sini

EDIT: Ini sekarang bekerja di Drupal 8, setidaknya dalam versi 8.3.4.

Adrian Cid Almaguer
sumber
2
OMG, saya selalu bertanya-tanya untuk apa opsi view itu. Ini kotor !!!
No Sssweat
1
@NoSssweat saya sedang belajar bahasa Inggris sekarang, bisakah Anda memberikan saya sinonim dari kotor? Saya tidak dapat memahami ungkapan "Ini kotor"
Adrian Cid Almaguer
3
Tidak, itu berarti solusi yang sangat bagus / mengesankan. Mis: Sasaran Shootout Kotor Alexander Nylander
No Sssweat
1
@AdrianCidAlmaguer Saya setuju solusi ini "sakit"! (idiom)
John R
2
Satu-satunya masalah dengan solusi ini, adalah bahwa bidang referensi entitas, setelah dipilih, menunjukkan ID dua kali dalam formulir edit entitas, karena disertakan secara default setelah dipilih.
Yuri
5

Buat bidang Referensi Entitas dengan konfigurasi default

masukkan deskripsi gambar di sini

Function entitasreference_autocomplete_callback_get_matches menentukan output dari autocomplete.

function entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id = '', $string = '') {
  $matches = array();

  $entity = NULL;
  if ($entity_id !== 'NULL') {
    $entity = entity_load_single($entity_type, $entity_id);
    $has_view_access = (entity_access('view', $entity_type, $entity) !== FALSE);
    $has_update_access = (entity_access('update', $entity_type, $entity) !== FALSE);
    if (!$entity || !($has_view_access || $has_update_access)) {
      return MENU_ACCESS_DENIED;
    }
  }

  $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);

  if ($type == 'tags') {
    // The user enters a comma-separated list of tags. We only autocomplete the last tag.
    $tags_typed = drupal_explode_tags($string);
    $tag_last = drupal_strtolower(array_pop($tags_typed));
    if (!empty($tag_last)) {
      $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
    }
  }
  else {
    // The user enters a single tag.
    $prefix = '';
    $tag_last = $string;
  }

  if (isset($tag_last)) {
    // Get an array of matching entities.
    $entity_labels = $handler->getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'], 10);

    // Loop through the products and convert them into autocomplete output.
    foreach ($entity_labels as $values) {
      foreach ($values as $entity_id => $label) {
        $key = "$label ($entity_id)";
        // Strip things like starting/trailing white spaces, line breaks and tags.
        $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
        // Names containing commas or quotes must be wrapped in quotes.
        if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
          $key = '"' . str_replace('"', '""', $key) . '"';
        }
        /* *** */$matches[$prefix . $key] = '<div class="reference-autocomplete">' . $label .' - ('. $entity_id . ')</div>';//****
      }
    }
  }
  drupal_json_output($matches);
}

baris terakhir $matches[$prefix . $key] = '<div class="reference-autocomplete">'menentukan output dan $entity_idtersedia yang merupakan ID. Anda dapat melakukan apa yang saya lakukan di baris itu (ditunjukkan oleh **), cukup tulis:

 $matches[$prefix . $key] = '<div class="reference-autocomplete">' . $label .' - ('. $entity_id . ')</div>';

Anda dapat menggunakan $entity_iduntuk mengambil bidang lain dan apa pun yang Anda inginkan.

Satu hal lagi!

Beberapa kali itu bukan ide yang baik untuk mengubah fungsi modul inti (jika tidak penting bagi Anda solusi di atas sudah cukup).

Jika Anda perlu mengganti fungsi inti entity_referencemodul, buat modul kecil dan beri namaelabel

ini elabel.info

;$Id;
name = My Entity Reference Label
description = This module creates special Entity Reference Label
package = My Modules
core = 7.x
php = 5.1
files[] = elabel.module

dan itu elabel.module

<?php function elabel_menu_alter(&$items){
    unset($items['entityreference/autocomplete/single/%/%/%']);
    unset($items['entityreference/autocomplete/tags/%/%/%']);

      $items['entityreference/autocomplete/single/%/%/%'] = array(
    'title' => 'Entity Reference Autocomplete',
    'page callback' => 'elabel_autocomplete_callback',
    'page arguments' => array(2, 3, 4, 5),
    'access callback' => 'entityreference_autocomplete_access_callback',
    'access arguments' => array(2, 3, 4, 5),
    'type' => MENU_CALLBACK,
  );

    $items['entityreference/autocomplete/tags/%/%/%'] = array(
    'title' => 'Entity Reference Autocomplete',
    'page callback' => 'elabel_autocomplete_callback',
    'page arguments' => array(2, 3, 4, 5),
    'access callback' => 'entityreference_autocomplete_access_callback',
    'access arguments' => array(2, 3, 4, 5),
    'type' => MENU_CALLBACK,
  );
  return $items;

}

function elabel_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $entity_id = '', $string = '') {
  // If the request has a '/' in the search text, then the menu system will have
  // split it into multiple arguments and $string will only be a partial. We want
  //  to make sure we recover the intended $string.
  $args = func_get_args();
  // Shift off the $type, $field_name, $entity_type, $bundle_name, and $entity_id args.
  array_shift($args);
  array_shift($args);
  array_shift($args);
  array_shift($args);
  array_shift($args);
  $string = implode('/', $args);

  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle_name);

  return elabel_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id, $string);
}

function elabel_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id = '', $string = '') {
  $matches = array();

  $entity = NULL;
  if ($entity_id !== 'NULL') {
    $entity = entity_load_single($entity_type, $entity_id);
    $has_view_access = (entity_access('view', $entity_type, $entity) !== FALSE);
    $has_update_access = (entity_access('update', $entity_type, $entity) !== FALSE);
    if (!$entity || !($has_view_access || $has_update_access)) {
      return MENU_ACCESS_DENIED;
    }
  }

  $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);

  if ($type == 'tags') {
    // The user enters a comma-separated list of tags. We only autocomplete the last tag.
    $tags_typed = drupal_explode_tags($string);
    $tag_last = drupal_strtolower(array_pop($tags_typed));
    if (!empty($tag_last)) {
      $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
    }
  }
  else {
    // The user enters a single tag.
    $prefix = '';
    $tag_last = $string;
  }

  if (isset($tag_last)) {
    // Get an array of matching entities.
    $entity_labels = $handler->getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'], 10);

    // Loop through the products and convert them into autocomplete output.
    foreach ($entity_labels as $values) {
      foreach ($values as $entity_id => $label) {
        $key = "$label ($entity_id)";
        // Strip things like starting/trailing white spaces, line breaks and tags.
        $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
        // Names containing commas or quotes must be wrapped in quotes.
        if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
          $key = '"' . str_replace('"', '""', $key) . '"';
        }
        /* *** */ $matches[$prefix . $key] = '<div class="reference-autocomplete">' . $label .'('.$entity_id.')' .'</div>';
      }
    }
  }

  drupal_json_output($matches);
}

Saya mencoba kode ini dan berfungsi dengan baik Jika ada jenis referensi entitas lain dan Anda tidak perlu melakukan ini untuk mereka, cukup tambahkan IFpernyataan dan periksa bundel atau tipe konten.

M ama D
sumber