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

发布于 2024-12-22 08:16:15 字数 483 浏览 1 评论 0原文

在 Drupal 7 中,我以编程方式创建了一个实体。它以以下形式添加了一些文本字段,例如名称、价格等:

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

但是,我希望能够向每个实体添加多个图像。

当我使用 Drupal 的用户界面添加字段时,我可以在底部的“添加现有字段”下选择“图像:field_image(图像)”。这个字段正是我想要的行为。

如何以编程方式将其添加到我的实体中?

In Drupal 7 I created an entity programmatically. 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". This field has exactly the behaviour I want.

How can I add it programmatically to my entity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-12-29 08:16:15

您可以使用 HOOK_form_alter() 钩子扩展使用 Drupal UI 创建的内容类型。

或者,您可以使用 var_dump(); 创建多个图像字段时找出数组生成的内容,并将其添加到自定义模板的数组中。

You can just extend the content-type you made with the Drupal UI by using the HOOK_form_alter()-hook.

Or you can find out what the Array is generating when you make a multiple image-field by using a var_dump(); and add it to the Array of your custom made template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文