在 Drupal 中对表单进行主题化,该表单必须是实体内容的一部分

发布于 2024-11-16 05:48:09 字数 734 浏览 2 评论 0原文

我的模块(mymodule)中有一个表单...

function mymodule_form()
{
   $form['mytext'] = array(
     '#title' => 'Password',
     '#type' => 'password',
     '#size' => 10,
   );
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => 'Cool!',
   );
   $form['cool_submit'] = array(
     '#type' => 'submit',
     '#value' => 'Cool Submit!',
   );
   return $form;
}

我使用了 hook_entity_view 挂钩在显示的所有 drupal 实体下显示此表单。

function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
  $entity->content['myadditionalfield'] = mymodule_form();
}

显示此表单时,drupal 会自行将 DIV 标记添加到 mytext(密码字段)。我想覆盖它并为此表单提供我自己的 DIV 标签和主题。我该怎么做?

谢谢 :-)

I've a form in my module (mymodule)...

function mymodule_form()
{
   $form['mytext'] = array(
     '#title' => 'Password',
     '#type' => 'password',
     '#size' => 10,
   );
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => 'Cool!',
   );
   $form['cool_submit'] = array(
     '#type' => 'submit',
     '#value' => 'Cool Submit!',
   );
   return $form;
}

I've used the hook_entity_view hook to display this form under all drupal entities that are displayed.

function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
  $entity->content['myadditionalfield'] = mymodule_form();
}

When showing this form, drupal adds a DIV tag to the mytext (password field) by itself. I want to override this and provide my own DIV tags and theme to this form. How do I do it?

Thanks :-)

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

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

发布评论

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

评论(1

强者自强 2024-11-23 05:48:09

我进一步研究这个问题来解决它。问题就解决了。我更改了上面的代码...

function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
  $element = array(
    'start' => array(
        '#type' => 'button',
        '#name' => 'start', 
        '#button_type' => 'submit',
    ),
    'my_text' => array(
        '#type' => 'textfield',
        '#size' => 30, 
        '#maxlength' => 50,
    ),
    'my_submit' => array(
        '#type' => 'button',
        '#name' => 'Submit Discussion',
        '#button_type' => 'submit',
    ),
  );

  $entity->content['disc_bar'] = $element;
}

每当呈现文本字段或密码字段时,问题就会不断出现。我检查了系统元素信息函数(elements_info 挂钩)中的类型数组,发现文本字段和密码字段带有 #theme-wrapper 的默认值。我试图通过这种方式覆盖它......

'my_text' => array(
        '#type' => 'textfield',
        '#size' => 30, 
        '#maxlength' => 50,
        '#theme-wrapper' => '',
),

并且它有效。它不会生成任何额外的部门标签...:-)

I worked further on this question to solve it. And the problem got solved. I changed the above code...

function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
  $element = array(
    'start' => array(
        '#type' => 'button',
        '#name' => 'start', 
        '#button_type' => 'submit',
    ),
    'my_text' => array(
        '#type' => 'textfield',
        '#size' => 30, 
        '#maxlength' => 50,
    ),
    'my_submit' => array(
        '#type' => 'button',
        '#name' => 'Submit Discussion',
        '#button_type' => 'submit',
    ),
  );

  $entity->content['disc_bar'] = $element;
}

And the problem kept creeping up whenever a textfield or a password field was rendered. I checked the type array in systems elements info function (a elements_info hook) and found that textfield and password fields come with a default value for the #theme-wrapper. I tried to override it by this way...

'my_text' => array(
        '#type' => 'textfield',
        '#size' => 30, 
        '#maxlength' => 50,
        '#theme-wrapper' => '',
),

and It worked. Its not generating any additional division tags... :-)

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