This patch is a first rough version of autocomplete functionality with entityreference fields.

It discards settings that you can have on the entityreference field (Like whether to search on Start Only, of Contains) and just searches on Contains.

But its a start.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hass’s picture

Title: autocomplete for entityreference fields » Autocomplete for entityreference fields
hass’s picture

Status: Needs work » Needs review
FileSize
2.84 KB

I have added Contains and Starts with support and fixed all remaining tasks. This works great for me now.

Status: Needs review » Needs work

The last submitted patch, 1722112_realname_Autocomplete+for+entityreference+fields.patch, failed testing.

hass’s picture

Status: Needs work » Needs review
FileSize
2.83 KB

Suxxx Eclipse copy and paste bugs

hass’s picture

Status: Needs review » Needs work

There seems to be a validation issue in node forms. This fails and the form error is shown:

  // Validate the "authored by" field.
  if (!empty($node->name) && !($account = user_load_by_name($node->name))) {
    // The use of empty() is mandatory in the context of usernames
    // as the empty string denotes the anonymous user. In case we
    // are dealing with an anonymous user we set the user ID to 0.
    form_set_error('name', t('The username %name does not exist.', array('%name' => $node->name)));
  }
hass’s picture

Assigned: Unassigned »

The autocomplete need to keep $matches[$account->name] as is, except on entity references. Needs a major overhaul of this patch.

hass’s picture

Status: Needs work » Needs review
FileSize
4.67 KB

New patch should fix all issues and is fairly smarter as it prepares the module for more modules with autocomplete user fields.

hass’s picture

hass’s picture

Status: Needs review » Needs work

Heck, bad patch.

hass’s picture

Status: Needs work » Needs review
FileSize
4.8 KB

Final.

Dave Reid’s picture

entity reference should be using entity_label() for display, is it not? Or is this to enable searching/querying on the real name? Maybe we should provide a default Reference selection view instead as the proper solution?

hass’s picture

It"s for searching the real name. It's extremly confusing that i need to enter the username and get the realname as result. Additional the usernames are often fantasy names. This patch search for both to get the best results we can provide.

Edit: i may confuse myself with #985944: Replace user autocomplete with realname autocomplete. This patch just make the entity references work like the user selection field. Additional it searches in both username and realname what looked extremly usable for me.

BrightBold’s picture

Thanks! This did exactly what I was hoping - allowed people to search for the real name instead of the username, and then continue seeing the real name as the result.

Could you reroll the patch for the latest dev? It didn't apply cleanly because the realname_autocomplete function seems to have changed a lot. I made some uninformed choices about which lines to keep and which to overwrite, and it seems to work, but I don't really recommend that as a patching strategy.

hass’s picture

Re-roled patch attached.

hass’s picture

entity reference should be using entity_label() for display

@Dave: I'm not sure if I understand this. Can you explain this a bit more, please?

I thought entity_label() is only used on output in view mode, not in edit mode forms. I've written these autocomplete based on the _entityreference_autocomplete_tags_validate() function that only looks for an integer in brackets.

      // Take "label (entity id)', match the id from parenthesis.
      if (preg_match("/.+\((\d+)\)/", $entity, $matches)) {
        $value[] = array(
          'target_id' => $matches[1],
        );
      }
japerry’s picture

Status: Needs review » Reviewed & tested by the community

We've tested this in commons, and the patch in #14 works as described.

super_romeo’s picture

Thanks for patch.

But there are some notices:

Notice: Undefined index: admin в функции realname_autocomplete() (строка 352 в файле /home/u6819/domains/cargo-system.ru/sites/all/modules/realname/realname.module).
Notice: Undefined index: admin в функции realname_autocomplete() (строка 352 в файле /home/u6819/domains/cargo-system.ru/sites/all/modules/realname/realname.module).
Notice: Undefined index: Attergyundege в функции realname_autocomplete() (строка 352 в файле /home/u6819/domains/cargo-system.ru/sites/all/modules/realname/realname.module).
Notice: Undefined index: Drupal в функции realname_autocomplete() (строка 352 в файле /home/u6819/domains/cargo-system.ru/sites/all/modules/realname/realname.module).
Notice: Undefined index: EmainaSuimi в функции realname_autocomplete() (строка 352 в файле /home/u6819/domains/cargo-system.ru/sites/all/modules/realname/realname.module).

hass’s picture

@super_romeo: This looks independed from this patch. Please open a new case.

jessehs’s picture

Status: Reviewed & tested by the community » Needs work

It seems like if the autocomplete field is not an entityreference type then the fallback is to use "Begins with" core behavior. This makes UI hard for some things. Ex: at the Organic Groups `/group/node/[nid]/admin/people/add-user` path to add users to groups.

How about adding a `realname_autocomplete` variable setting at `/admin/config/people/realname` so a person could decide what to default to for all user autocomplete fields?

hass’s picture

Status: Needs work » Reviewed & tested by the community

Feel free to create a new feature request, but don't highjack this case please.

hass’s picture

hass’s picture

hass’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)
Issue tags: +

Automatically closed - issue fixed for 2 weeks with no activity.

japerry’s picture

Issue summary: View changes
Status: Closed (fixed) » Needs work
FileSize
276.55 KB

This fix is the cause of #2225889: Notice: Undefined offset: 5 in _og_field_widget_replace_autocomplete_path()

Marking needs work because it changes the output of the entityreference api as seen below:

hass’s picture

Status: Needs work » Closed (fixed)

Please open a new case and write a repro case. Your screenshot is not really helpful. Post your patch over there, too.

I do not understand what may be wrong here. Maybe og is the root cause.