Search code examples
drupaldrupal-6drupal-modules

In nodereference field of a content type, Is it possible to show the content type name added with the autocomplete results?


In nodereference field of a content type, Is it possible to show the content type name added with the autocomplete results ?

Example :

I have a content type named Article which has a node reference field (Related content -> field_related_documents), So that this article will referenced to other nodes.

While creating an article, by entering 'help' in 'field_related_documents' I am getting autocomplete results (titles of referenced nodes) like below

helpsystems - rules 
Decision management Help
Using help menu
Help support glossary

Instead, I like to show results like below

Page | helpsystems - rules
HelpSystems | Decision management Help
Page | Using help menu
Document | Help support glossary

ie) autocomplete results should display title of other nodes added with their related content type.

How should i achieve this ? Is there any contributed modules available to achieve this?


Solution

  • I created a new custom Module nodereference_patch with below hook

    /*
     * Altering nodereference/autocomplete menu using menu alter
     */
    function nodereference_patch_menu_alter(&$items) {
      $items['nodereference/autocomplete']['page callback'] = 'nodereference_patch_new_nodereference_autocomplete';
    }
    

    Then, I copied the nodereference_autocomplete function into your custom module, changing it's name to nodereference_patch_new_nodereference_autocomplete.

    Then I changed this line:

    $matches[$row['title'] ." [nid:$id]"] = '<div class="reference-autocomplete">'. $row['rendered'] . '</div>';
    

    to

    $matches[$row['type'] . " | " . $row['title'] . " [nid:$id]"] = '<div class="reference-autocomplete">' . $row['type'] . " | " . $row['rendered'] . '</div>';
    

    I copied _nodereference_potential_references function from nodereference.module to custom module and renamed it as _nodereference_patch_potential_references

    Then I changed the following line as

    $references = _nodereference_potential_references_standard($field, $string, $match, $ids, $limit);
    

    to

    $references = _nodereference_patch_potential_references_standard($field, $string, $match, $ids, $limit);
    

    I copied _nodereference_potential_references_standard from nodereference.module to custom module and renamed it as _nodereference_patch_potential_references_standard

    Then I added 'type' element to $references[]. ie) changed following array from

    $references[$node->nid] = array(
        'title' => $node->node_title,
        'rendered' => check_plain($node->node_title),
    );
    

    to

    $references[$node->nid] = array(
        'title' => $node->node_title,
        'type' => $node->node_type,
        'rendered' => check_plain($node->node_title),
    );