来自模块的 drupal 表单布局

发布于 2024-09-01 00:02:50 字数 1370 浏览 7 评论 0原文

我创建了自己的模块,名为“cssswitch”。我正在尝试创建自己的 html 布局来显示模块的管理表单部分。我了解如何使用 hook_form_alter() 修改表单元素,但我需要创建整个表单布局以使某些字段彼此相邻出现。 看来我需要类似使用 theme_cssswitch_display() 显示节点前端视图的方式,但对于节点的管理部分。

function cssswitch_form_alter(&$form, $form_state, $form_id) {

    switch($form_id) {  

        case 'cssswitch_node_form':
            $form['hour_start']['#prefix'] = '<div class="start-box">';
            $form['ampm_start']['#suffix'] = '</div>';
            $form['ampm_start']['#attributes'] = array("style" => "border-width:2px;");
            break;
    }

}

function cssswitch_theme() {

    return array(
      'cssswitch_display' => array(
        'arguments' => array('node'),
      ),

    );

}

// to display the view of the node
function theme_cssswitch_display($node) {

  $output = '<div class="cssswitch-cssfilename">Filename: '.
    check_plain($node->cssfilename). '</div>';
  $output .= '<div class="cssswitch-time_start">Start: '.
    check_plain($node->hour_start). ':'.check_plain($node->minute_start).' '.check_plain($node->ampm_start).'</div>';

  $output .= '<div class="cssswitch-time_end">End: '.
    check_plain($node->hour_end). ':'.check_plain($node->minute_end).' '.check_plain($node->ampm_end).'</div>';    

  return $output;
}

感谢您的帮助

I created my own module called "cssswitch". I'm trying to create my own html layout to display the admin form portion of the module. I understand how to use the hook_form_alter() to modify form elements, but I need to create the entire form layout to make some fields appear next to each other.
It seems I need something like the way I have the frontend view of the node displayed with theme_cssswitch_display(), but for the admin part of the node.

function cssswitch_form_alter(&$form, $form_state, $form_id) {

    switch($form_id) {  

        case 'cssswitch_node_form':
            $form['hour_start']['#prefix'] = '<div class="start-box">';
            $form['ampm_start']['#suffix'] = '</div>';
            $form['ampm_start']['#attributes'] = array("style" => "border-width:2px;");
            break;
    }

}

function cssswitch_theme() {

    return array(
      'cssswitch_display' => array(
        'arguments' => array('node'),
      ),

    );

}

// to display the view of the node
function theme_cssswitch_display($node) {

  $output = '<div class="cssswitch-cssfilename">Filename: '.
    check_plain($node->cssfilename). '</div>';
  $output .= '<div class="cssswitch-time_start">Start: '.
    check_plain($node->hour_start). ':'.check_plain($node->minute_start).' '.check_plain($node->ampm_start).'</div>';

  $output .= '<div class="cssswitch-time_end">End: '.
    check_plain($node->hour_end). ':'.check_plain($node->minute_end).' '.check_plain($node->ampm_end).'</div>';    

  return $output;
}

thanks for any help

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

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

发布评论

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

评论(3

紙鸢 2024-09-08 00:02:56

我找到了我现在需要的大部分东西。我必须使用“Themer Info”给我的“建议”表单 ID,即“cssswitch_node_form”。我在名为“cssswitch_form()”的表单构建函数中使用了它,如下所示:
$form['#theme'] = 'cssswitch_node_form';

我还需要在此函数中使用该名称:

function cssswitch_theme() {

    return array(
      'cssswitch_display' => array(
        'arguments' => array('node'),
      ),

      'cssswitch_node_form' => array(
        'arguments' => array('form' => NULL),
      ),      


    );

这是同名但前面带有“theme_”的函数:

function theme_cssswitch_node_form($form) {

    $output='';
    $output.='<span>testing 123</span>';
    $output.=drupal_render($form['hour_start']['name']);
    $output.=drupal_render($form['minute_start']);
        $output .= drupal_render($form);

  return $output;

}

I仍然存在 html 代码自动用 div 围绕表单元素的问题,这不是我想要的。在本例中,我想并排放置一些表单元素。我想这就是函数 drupal_render() 的作用。是否有一个函数调用只会给我表单元素而不包含所有 html 标记?

谢谢

I found most of what I need now. I had to use the "suggested" form id that Themer Info" gave me which was "cssswitch_node_form". I used that in my form builing function called "cssswitch_form() like this:
$form['#theme'] = 'cssswitch_node_form';

I also need to use that name in this function:

function cssswitch_theme() {

    return array(
      'cssswitch_display' => array(
        'arguments' => array('node'),
      ),

      'cssswitch_node_form' => array(
        'arguments' => array('form' => NULL),
      ),      


    );

And here is the function of the same name but with "theme_" prepended to it:

function theme_cssswitch_node_form($form) {

    $output='';
    $output.='<span>testing 123</span>';
    $output.=drupal_render($form['hour_start']['name']);
    $output.=drupal_render($form['minute_start']);
        $output .= drupal_render($form);

  return $output;

}

I still have the problem of html code surrounding the form elements with divs automatically which is not what I wanted. In this instance, I want to place some form elements side by side. I guess that's what the function drupal_render() does. Is there a function call that will just give me the form element without all the html markup?

thanks

违心° 2024-09-08 00:02:55

我现在已经把一切都解决了。
我的hook_theme是这样的:

function cssswitch_theme() {

    return array(
      'cssswitch_display' => array(
        'arguments' => array('node'),
      ),

      'cssswitch_node_form' => array(
        'arguments' => array('form' => NULL),
        'template' => 'cssswitch-node-form.tpl.php',
      ),

      );

}

我创建了文件themes/garland/cssswitch-node-form.tpl.php
然后我可以控制在哪里放置任何表单元素,如下所示:

<style>
#edit-hour-start-wrapper, #edit-minute-start-wrapper, #edit-ampm-start-wrapper, #edit-hour-end-wrapper, #edit-minute-end-wrapper, #edit-ampm-end-wrapper {
    float:left;
    margin-right:25px;
}
</style>
<div id="regform1">

<?php print drupal_render($form['title']); ?>
<?php print drupal_render($form['cssfilename']); ?>

    <fieldset class="group-account-basics collapsible">
        <legend>Time Start</legend>
        <?php print drupal_render($form['hour_start']); ?>
        <?php print drupal_render($form['minute_start']); ?>
        <?php print drupal_render($form['ampm_start']); ?>
    </fieldset>

    <fieldset class="group-account-basics collapsible"><legend>Time end</legend>
        <?php print drupal_render($form['hour_end']); ?>
        <?php print drupal_render($form['minute_end']); ?>
        <?php print drupal_render($form['ampm_end']); ?>    
    </fieldset>
</div>

<?php print drupal_render($form['options']); ?>
<?php print drupal_render($form['author']); ?>
<?php print drupal_render($form['revision_information']); ?>
<?php print drupal_render($form['path']); ?>
<?php print drupal_render($form['attachments']); ?>
<?php print drupal_render($form['comment_settings']); ?>
<?php print drupal_render($form); ?>

I got it all sorted out now.
My hook_theme is this:

function cssswitch_theme() {

    return array(
      'cssswitch_display' => array(
        'arguments' => array('node'),
      ),

      'cssswitch_node_form' => array(
        'arguments' => array('form' => NULL),
        'template' => 'cssswitch-node-form.tpl.php',
      ),

      );

}

I created the file themes/garland/cssswitch-node-form.tpl.php
Then I can have control over where to put any form elements like this:

<style>
#edit-hour-start-wrapper, #edit-minute-start-wrapper, #edit-ampm-start-wrapper, #edit-hour-end-wrapper, #edit-minute-end-wrapper, #edit-ampm-end-wrapper {
    float:left;
    margin-right:25px;
}
</style>
<div id="regform1">

<?php print drupal_render($form['title']); ?>
<?php print drupal_render($form['cssfilename']); ?>

    <fieldset class="group-account-basics collapsible">
        <legend>Time Start</legend>
        <?php print drupal_render($form['hour_start']); ?>
        <?php print drupal_render($form['minute_start']); ?>
        <?php print drupal_render($form['ampm_start']); ?>
    </fieldset>

    <fieldset class="group-account-basics collapsible"><legend>Time end</legend>
        <?php print drupal_render($form['hour_end']); ?>
        <?php print drupal_render($form['minute_end']); ?>
        <?php print drupal_render($form['ampm_end']); ?>    
    </fieldset>
</div>

<?php print drupal_render($form['options']); ?>
<?php print drupal_render($form['author']); ?>
<?php print drupal_render($form['revision_information']); ?>
<?php print drupal_render($form['path']); ?>
<?php print drupal_render($form['attachments']); ?>
<?php print drupal_render($form['comment_settings']); ?>
<?php print drupal_render($form); ?>
那些过往 2024-09-08 00:02:55

仅当您想更改另一个模块已定义的表单时才需要 hook_form_alter 。由于您正在定义表单,因此不需要更改它,而是可以按照您想要的方式定义它。

要更改表单的标记,您需要使用表单本身和/或其元素的 #theme 属性。有了它,您可以使用自己的主题函数来呈现表单及其元素。

请在 Drupal 的 FAPI 参考中寻求帮助。

hook_form_alter is only needed, if you want to change a form that another module has defined. Since you are defining the form, you don't need to alter it, instead you can just define it like you want.

What you need to use, to change the markup of the form, is the #theme attribute of the form itself and/or it's elements. With it you can use your own theme functions to render the form and it's elements.

Look for help in Drupal's FAPI reference.

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