意外的实体引用自动完成行为

Unexpected Entity Reference Autocomplete behaviour

我在 Drupal 中使用实体 API、表单 API 和实体引用自动完成功能。 我有两种类型,俱乐部和课程之间的关系,每个 club 可以有很多 courses 所以基本上 table course 包含一个名为 goclid 的列引用club id 号码像这样:

// hook_schema()

$schema['course'] = array(
    // other fields...
'goclid' => array(
    'description' => 'Reference to the club',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ),

// ... a few lines later...
'foreign key' => array(
  'related_club' => array( // join alias
    'table' => 'club',
    'columns' => array('goclid' => 'id'),
  ),
),
// etc.
);

然后我在 course 表单中包含一个这样的字段:

$form['goclid'] = array(
    '#title' => t('Reference to the club'),
    '#type' => 'entityreference',
    '#era_entity_type' => 'club',
    '#default_value' => isset($course->goclid) ? $course->goclid : '',
    '#required' => FALSE,
);

现在,只有当我输入一个 ID 号码时,自动完成功能才会给出建议,然后用标签(高尔夫球杆的名称)填充字段值。 我想要的恰恰相反:我想通过输入俱乐部的名称来获得建议,然后当我选择一个时,表单字段应该填充该对象的 ID 号。

为什么实体引用自动完成的行为异常?我该怎么做才能获得所需的行为?

点这里Link to readme.txt

你会看到这个:

$form['my_entity_reference'] = array(
      '#type' => 'entityreference',
      '#title' => t('My Reference'),
      '#era_entity_type' => 'user',  // Mandatory.
      '#era_bundles' => array(), // Optional (Any bundle by default).
      '#era_cardinality' => 3,       // Optional (1 By default).
      '#era_query_settings' => array(
        'limit' => 15, // Default is 50.
        'property_conditions' => array(
          // 'entity property', 'filter value', 'operator'.
          array('uid', 30, '>'),
        ),
        'field_contitions' => array(
          // 'field name', 'column', 'value', 'op', 'delta', 'language'.
          array('field_test_field', 'value', 'test'),
        ),
      ),
    );


          - 'field_conditions':  Allows to filter the results returned in
                      the query, based on the value of any field of the of
                      the entity. This property is meant to be an array, in
                      which each element is an array of the arguments to
                      pass to the fieldCondition() method of the
                      EntityFieldQuery class.
                      Example of use:

                            '#era_query_settings' => array(
                              'field_conditions' => array(
                                // 'field name', 'column', 'value'.
                                array('field_test_field', 'value', 'test'),
                              ),
                            ),

                      For further information, see the documentation of the
                      fieldCondition() method of the EntityFieldQuery class

希望它能解决您的问题:)

Salvador Molina, the author of the Entity Reference Autocomplete Module, gave me an answer on Drupal Answers 这也值得在 Whosebug 上分享:

I think you might have not specified the "label" column in the entity definition. That goes within the 'entity keys' array, like the 'id' key.

$entity_info = array(
   .....
  'entity keys' array(
    'id' => 'primary key column...',
    'label' =>  'column holding the label'
  ),
)

Otherwise entityreference will not know which column to use for searching in the DB. That still makes it possible to show the label (as you're experiencing) after you reference the entity, because 'label' key is not mandatory, and you may have specified a "label callback" instead, which the Entity API will use when entity_label() is called to get the label of a specific entity.

Salvador 的提示完美无缺,在 entity keys 数组中包含 label 后,我可以通过输入实体标签来搜索实体。