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

时间:2015-07-23 07:01:39

标签: php drupal drupal-7

我在Drupal中使用Entity API,Form API和Entity Reference Autocomplete。 我有两种类型,俱乐部和课程之间的关系,每个club可以有很多courses所以基本上表course包含一个名为goclid的列,它引用{{1}像这样的id号:

club

然后我在// 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

现在,仅当我输入身份证号码时,自动填充功能才会提供建议,然后使用标签(高尔夫球杆的名称)填充字段值。 我想要的恰恰相反:我希望通过输入俱乐部的名称来获得建议,然后当我选择一个时,表格字段应该填充该对象的ID号。

为什么实体参考自动完成功能会以意想不到的方式运行?我该怎么做才能获得理想的行为?

2 个答案:

答案 0 :(得分:1)

点击此处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

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

答案 1 :(得分:0)

Salvador Molina,实体参考自动填充模块的作者gave me an answer on Drupal Answers,值得在Stackoverflow上分享:

  

我想你可能没有指定"标签"实体定义中的列。这属于实体键'数组,就像' id'键。

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

否则,entityreference将不知道在DB中用于搜索的列。这仍然可以在您引用实体后显示标签(正如您所经历的那样),因为'标签'密钥不是强制性的,您可能已经指定了"标签回调"相反,当调用entity_label()来获取特定实体的标签时,Entity API将使用它。

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

相关问题