0

I have tested the Entity Reference Autocomplete module by defining a form field of type entityreference (via hook_form) on the user entity - and it works as advertised.

When I try it on my own entity an AJAX error is thrown.

My form element definition:

$form['name'] = array(
  '#title' => t('Ground'),
  '#type' => 'entityreference',
  '#era_entity_type' => 'ground',
  '#size' => 32,
);

I can't imagine the problem is my entity's base table. So, the only thing I can think of, which may be the source of the error, is my hook_entity_info definition:

/**
 * Implements hook_entity_info().
 */
function scorebook_entity_info() {
  $return = array(
    'ground' => array(
      'label' => t('Ground'),
      'controller class' => 'EntityAPIController',
      'base table' => 'scorebook_ground',
      'entity keys' => array('id' => 'grid'),
    ),
  );
  return $return;
}

It contains the minimum keys. Do I need to add some other keys to my definition? If anyone knows of a working example of this module working with a custom entity that may help too. Thanks.

UPDATE:

Here is the correct hook_entity_info definition for anyone who doesn't want to read everything below. You need to define an access callback function, also, to return either TRUE/FALSE accordingly.

'ground' => array(
  'label' => t('Ground'),
  'controller class' => 'EntityAPIController',
  'base table' => 'scorebook_ground',
  'entity keys' => array(
    'id' => 'grid',
    'label' => 'name',
  ),
  'access callback' => 'scorebook_access',
),

2 Answers 2

0

Right. Checked your module, and your issue here is permissions.

Before returning results, the module checks whether the user has actually permission to see those results, and cuts the ones that the user is not supposed to see.

In this case, none of your entities define an 'access callback' in hook_entity_info(), which ultimately translates in the Entity API returning NULL when permissions are checked, which my module treats as "nothing defined, better be safe and don't return results".

So you were probably using my module in the right way, but your entities are missing the access checks.

I suggest you add at least a global check for them, like

      'access callback' => 'scorebook_entity_access',

And then implement that function.

function scorebook_entity_access($op, $entity, $account = NULL, $entity_type = '') 
{
 // Edit to fit your requirements.
  return TRUE;
}

I've tested it with grounds, which are the ones defining a label, and it works all right.

Hope it helps!

0

I'm the module maintainer. Thanks for using the module, hope you're finding it useful. You need to add a 'label' key to your entity definition, like:

'entity keys' => array(
  'id' => 'grid',
  'label' => 'table-column-that-contains-the-human-name-of-entity',
),

With that, and after flushing caches, etc, your form should work fine. The reason is that entityreference_autocomplete assumes that (as a best practice), every entity would have, apart of the ID, which will be normally just a number, some friendly way to identify it through a label. Therefore, you need that on your custom entity.

However, I get that not everyone might want / need that 'label' key or a column for the label in their schema, for whatever reason, and I've added support to the module to work without that. That bit is on the dev version on the module, and will be rolled out in the next stable version. So it's up to you, to define a column for the 'label' key in your schema, or download the dev version, or wait a couple days for the stable release that supports entities without the 'label' key.

Cheers!

6
  • Thanks. Adding the 'label' key stopped the error, but I don't get any suggestions dropping down. Do I need any other keys in hook_entity_info?
    – dbj44
    Commented Apr 28, 2014 at 16:29
  • The 'label' key is supposed to map to a column in the schema table of your entity in the database. Of course that needs to be prefilled with some values through your entity class / controller. Is that column in there an populated with any values? Commented Apr 28, 2014 at 17:27
  • Yes, there are rows in the base table, but the autocomplete widget is not returning any matches. Would it help to see my module? Thanks.
    – dbj44
    Commented Apr 28, 2014 at 18:39
  • Yes, that would definitely help! It's worth if you double check the db as well to make sure those columns for the label are populated, and to use xdebug to see what's happening in the autocomplete callback, to see if all the args are passed to the query as expected, and the results of it. Commented Apr 29, 2014 at 8:08
  • Module sent to the contact email on your website.
    – dbj44
    Commented Apr 29, 2014 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.