gpt4 book ai didi

forms - Drupal 7 - 覆盖基本表单 HTML 标记

转载 作者:行者123 更新时间:2023-12-04 16:40:29 24 4
gpt4 key购买 nike

我真的很难理解 Drupal Form API ……实际上是 Drupal 作为一个整体。

这是我的问题,我已经创建了一个渲染良好的表单,但是我现在想做的是围绕某些表单元素包装 div,这样我就以适合我的网站的方式来设置表单的样式,而不是一些盒子标准废话。

有人可以帮忙,或者至少指出一个“好”教程的正确方向,而不是一些在网络上贴满的简短且非常模糊的废话?

谢谢。

最佳答案

hook_form_alter 是你的 friend 。

在主题的 template.php 文件中,您可以为表单、表单元素等添加前缀和后缀。

下面是我最近做的一个网站的例子。

function bhha_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_login') {
$form['#prefix'] = '<div class="loginForm">';
$form['#suffix'] = '</div>';
$form['name']['#title'] = Null; // Change text on form
$form['name']['#description'] = Null; // Change text on form
$form['name']['#attributes'] = array('placeholder' => t('username'));
$form['name']['#size'] = '30';
$form['pass']['#title'] = Null;
$form['pass']['#description'] = Null; // Change text on form
$form['pass']['#attributes'] = array('placeholder' => t('password'));
$form['pass']['#size'] = '30';
//$form['actions']['submit']['#value'] = t('password');
$form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/images/Login.png');
$form['links']['#markup'] = '<a class="user-password" href="'.url('user/password').'">' . t('Forgot your password?') . '</a>'; // Remove Request New Password from Block form
$form['links']['#weight'] = 540;
}
}

检查您的代码以获取您想要主题 ID 的表单。用连字符替换下划线,您应该能够使用上面的示例来执行您想要的操作。

在最基本的我想一个例子是:
function THEME_NAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'FORM_ID') {
// Adds a wrapper div to the whole form
$form['#prefix'] = '<div class="loginForm">';
$form['#suffix'] = '</div>';

// Adds a wrapper div to the element NAME
$form['name']['#prefix'] = '<div class="formRow">';
$form['name']['#suffix'] = '</div>';
}
}

关于forms - Drupal 7 - 覆盖基本表单 HTML 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11330828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com