简体   繁体   English

Drupal 7,以编程方式将field_image添加到实体

[英]Drupal 7, programmatically add field_image to entity

In Drupal 7 I created an entity programmatically. 在Drupal 7中,我以编程方式创建了一个实体。 It has a few textfields added like name, price etc in the form: 它以以下形式添加了一些文本字段,例如名称,价格等:

$form['data']['name'] = array(
   '#type' => 'textfield',
   '#title' => t('Name'),
   '#default_value' => isset($model->data['name']) ? $model->data['name']: '',
   '#maxlength' => 255,
   '#required' => TRUE,
   '#weight' => -3,
);

However, I'd like to be able to add multiple images to every entity it as well. 但是,我也希望能够向每个实体添加多个图像。

When I use Drupal's user interface to add fields, I can pick "Image: field_image (Image)" at the bottom, under "Add an existing field". 当我使用Drupal的用户界面添加字段时,可以在底部的“添加现有字段”下选择“图像:field_image(图像)”。 This field has exactly the behaviour I want. 该字段具有我想要的行为。

How can I add it programmatically to my entity? 如何以编程方式将其添加到我的实体?

You can just extend the content-type you made with the Drupal UI by using the HOOK_form_alter() -hook. 您可以通过使用HOOK_form_alter() hook扩展使用Drupal UI制作的内容类型。

Or you can find out what the Array is generating when you make a multiple image-field by using a var_dump(); 或者,您可以通过使用var_dump();来确定创建多个图像字段时Array生成的内容var_dump(); and add it to the Array of your custom made template. 并将其添加到自定义模板的数组中。

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

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