简体   繁体   English

将image_field附加到drupal 7中的实体

[英]attach image_field to entity in drupal 7

I use drupal 7, and Entity API to develope a module. 我使用drupal 7和Entity API开发模块。 I have an entity to record client information. 我有一个实体来记录客户信息。 I wish to use image_field to let client upload their logo. 我希望使用image_field让客户端上传其徽标。 So I have this function: 所以我有这个功能:

    function silver_client_enable()
{
  field_cache_clear();
  field_associate_fields("silver_client");

  if(field_info_field('logo'))
    return;

  $field = array(
    'field_name' => 'logo',
    'cadinality' => 1,
    'type' => 'image',
    );

    field_create_field($field);

  $instance = array(
    'field_name' => 'logo',
    'entity_type' => 'silver_client',
    'bundle' => 'silver_client',
    'label' => 'Logo',
    'description' => 'Logo',
    'display' => array(
      'default' => array('label' => 'hidden')
    ),
    'settings' => array(
      'file_directory' => '/logo',
    ),
    'widget' => array(
      'type' => 'image_image',
     ),
  );

  field_create_instance($instance);
}

In the entity creation/edit form, I use : 在实体创建/编辑表单中,我使用:

field_attach_form('silver_client', $client, $form, $form_state);

to attch the field. 参加比赛。

When I called up this form, the image upload field was corrected displayed. 当我调用此表单时,图像上载字段已更正显示。 An i can use it to uplod file to serve. 我可以用它来上载文件。

In the form submit function, I save the entity as: 在表单提交功能中,我将实体另存为:

entity_save('silver_client', $client);

However, after I press the save button, the entity table is correctly saved. 但是,按保存按钮后,实体表被正确保存。 Field table is not. 字段表不是。 Both field_data_logo and field_revision_logo are empty. field_data_logo和field_revision_logo均为空。

I believer Entity API looks after the retrieving and saving of attached fields. 我相信Entity API负责检索和保存附加字段。 Can someone tell me what is wrong with my code? 有人可以告诉我我的代码有什么问题吗? Thank you. 谢谢。

You have to write the values back into your entity: 您必须将值写回到您的实体中:

field_attach_submit('silver_client', $client, $form, $form_state);
entity_save('silver_client', $client);

http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_submit/7 http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_submit/7

And you should validate the field values: 并且您应该验证字段值:

field_attach_validate('silver_client', $client, $form, $form_state);

http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_validate/7 http://api.drupal.org/api/drupal/modules!field!field.attach.inc/function/field_attach_validate/7

Furthermore if you don't want to declare your entity and fields by yourself you might checkout the EntityConstructionKit : http://drupal.org/project/eck which allows to export entity structures with Features just like Views . 此外,如果您不想自己声明实体和字段,则可以签出EntityConstructionKithttp : //drupal.org/project/eck ,它可以导出具有Features实体结构,例如Views

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM