This is especially important together with the "show all" functionality.

The node references module has the limit currently hardcoded to 10, so I had to supply a different autocomplete callback, where I did set it manually to 200. This should probably be configurable.

Here is the code, mostly copy & paste from the node references module:


/**
 * Implements hook_menu().
 */
function acdx_node_reference_menu() {
  $items['acdx_node_reference/autocomplete/%/%/%'] = array(
    'page callback' => 'acdx_node_reference_autocomplete',
    'page arguments' => array(2, 3, 4),
    'access callback' => 'reference_autocomplete_access',
    'access arguments' => array(2, 3, 4),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback for the autocomplete results.
 */
function acdx_node_reference_autocomplete($entity_type, $bundle, $field_name, $string = '') {
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);

  $options = array(
    'string' => $string,
    'match' => $instance['widget']['settings']['autocomplete_match'],
    'limit' => 200,
  );
  $references = node_reference_potential_references($field, $options);

  $matches = array();
  foreach ($references as $id => $row) {
    // Markup is fine in autocompletion results (might happen when rendered
    // through Views) but we want to remove hyperlinks.
    $suggestion = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '$2', $row['rendered']);
    // Add a class wrapper for a few required CSS overrides.
    $matches[$row['title'] . " [nid:$id]"] = '<div class="reference-autocomplete">' . $suggestion . '</div>';
  }

  drupal_json_output($matches);
}


function acdx_node_reference_field_widget_info() {
  return array(
    'node_reference_autocomplete_deluxe' => array(
      'label'       => t('Autocomplete Deluxe'),
      'description' => t('Display the list of referenceable nodes as a textfield with autocomplete deluxe behaviour.'),
      'field types' => array('node_reference'),
      'settings'    => array(
        'autocomplete_match' => 'contains',
        'size' => 60,
        'autocomplete_path' => 'acdx_node_reference/autocomplete',
      ),
    ),

  );
}

Comments

berdir’s picture

Now again, better formatted:

/**
 * Implements hook_menu().
 */
function acdx_node_reference_menu() {
  $items['acdx_node_reference/autocomplete/%/%/%'] = array(
    'page callback' => 'acdx_node_reference_autocomplete',
    'page arguments' => array(2, 3, 4),
    'access callback' => 'reference_autocomplete_access',
    'access arguments' => array(2, 3, 4),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback for the autocomplete results.
 */
function acdx_node_reference_autocomplete($entity_type, $bundle, $field_name, $string = '') {
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);

  $options = array(
    'string' => $string,
    'match' => $instance['widget']['settings']['autocomplete_match'],
    'limit' => 200,
  );
  $references = node_reference_potential_references($field, $options);

  $matches = array();
  foreach ($references as $id => $row) {
    // Markup is fine in autocompletion results (might happen when rendered
    // through Views) but we want to remove hyperlinks.
    $suggestion = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '$2', $row['rendered']);
    // Add a class wrapper for a few required CSS overrides.
    $matches[$row['title'] . " [nid:$id]"] = '<div class="reference-autocomplete">' . $suggestion . '</div>';
  }

  drupal_json_output($matches);
}


function acdx_node_reference_field_widget_info() {
  return array(
    'node_reference_autocomplete_deluxe' => array(
      'label'       => t('Autocomplete Deluxe'),
      'description' => t('Display the list of referenceable nodes as a textfield with autocomplete deluxe behaviour.'),
      'field types' => array('node_reference'),
      'settings'    => array(
        'autocomplete_match' => 'contains',
        'size' => 60,
        'autocomplete_path' => 'acdx_node_reference/autocomplete',
      ),
    ),
  );
}

If you need, I can make a patch of this, but it's all new code except the widget info hook where I just changed the autocomplete path.

Note that I had to re-save the settings form after changing the path of existing fields to update the settings.

spacereactor’s picture

If there is a UI for changing the display limit will be easier to work with

marcoscano’s picture

subscribe, would love to have it in the UI also

sepgil’s picture

If you could provide a patch, I would be glad to commit it.

zatarain21’s picture

subscribe, would love to have it in the UI also

I agree

drclaw’s picture

Assigned: Unassigned » drclaw
Status: Active » Needs review
StatusFileSize
new4.12 KB

Thought I'd take a stab at this one. This should do the trick. Let me know if it works for you all!

drclaw’s picture

That last one had a little mistake with the element default value. This one is better. Sorry about that!

chop’s picture

Issue summary: View changes

Fix PHP open / close tags to allow correct code formatting