0

我正在尝试在 Drupal 6 中编写一个自定义模块,并尝试使用表单 API 在管理区域中添加一个简单的表单。

我已经显示了表单,但在提交然后重新加载页面时似乎无法再次填充自己。我尝试将 &$form_state 的第二个参数传递给我的 hook_form 函数,但它会导致关于缺少第二个参数的错误。

我确定这很简单,但我不太明白,这是我的 .module 代码:

/*
* Implements hook_init().
*/

function movehub_featured_links_block_init() {

    drupal_add_css(drupal_get_path('module', 'movehub_featured_links_block') . '/assets/css/movehub-featured-links-block-style.css');

}

/**
* Implements hook_theme().
*/

function movehub_featured_links_block_theme() {

    return array(
        'movehub_featured_links_block_manage' => array(
            'template' => 'movehub-featured-links-block-admin-manage'
        ),
        'movehub_featured_links_block_add' => array(
            'template' => 'movehub-featured-links-block-admin-add',
            'arguments' => array('link_id' => null)
        ),
    );

}

function movehub_featured_links_block_manage() {

    return theme('movehub_featured_links_block_manage');

}

function movehub_featured_links_block_add_form(&$form) {print_r($form);

    $form = array();

    $form['#attributes'] = array('enctype' => 'multipart/form-data');

    $form['link_title'] = array(
        '#type' => 'textfield',
        '#title' => t('Link Title')
    );

    $form['image'] = array(
        '#type' => 'file',
        '#title' => t('Image')
    );

    $form['image_hover'] = array(
        '#type' => 'file',
        '#title' => t('Hover Image')
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Add!')
    );

    return $form;

}

/**
* Implements hook_menu().
*/

function movehub_featured_links_block_menu() {

    $items = array();

    $items['admin/featured_links'] = array(
        'title' => 'Featured Links',
        'description' => 'Add, view and edit featured links..',
        'page callback' => 'movehub_featured_links_block_manage',
        'page arguments' => array('movehub_featured_links_block_admin'),
        'access arguments' => array('administer movehub_featured_links_block settings'),
        'type' => MENU_NORMAL_ITEM,
    );

    $items['admin/featured_links/manage'] = array(
        'title' => 'Manage',
        'description' => 'Add, view and edit featured links.',
        'page callback' => 'movehub_featured_links_block_manage',
        'page arguments' => array('movehub_featured_links_block_admin'),
        'access arguments' => array('administer movehub_featured_links_block settings'),
        'type' => MENU_NORMAL_ITEM,
    );

    $items['admin/featured_links/add'] = array(
        'title' => 'Add',
        'description' => 'Add a new featured link.',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('movehub_featured_links_block_add_form'),
        'access arguments' => array('administer movehub_featured_links_block settings'),
        'type' => MENU_CALLBACK
    );

    return $items;

}

function movehub_featured_links_block_add_form_submit($form, &$form_state) {



}

function movehub_featured_links_block_add_form_validation($form, &$form_state) {



}
4

2 回答 2

0

设法找到导致我这个问题的非常简单的错误 - 即我在验证时使用了错误的钩子名称。

我曾经使用过:

movehub_featured_links_block_add_form_validation

它应该是:

movehub_featured_links_block_add_form_validate

关键部分是使用 hook_validate 而不是 hook_validation。菜鸟错误。

于 2014-09-29T20:17:39.633 回答
0

在您的表单函数中,为每个字段添加“#default_value”。(有关更多信息,请参阅Drupal 6 表单 API #default_value

您在表单中的字段定义将如下所示:

$form['link_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Link Title'),
    '#default_value' => 'My custom link title',
);

然后,Form API 应在首次加载表单时使用给定的默认值填充它。以这种方式执行此操作,如果用户验证失败并收到表单以纠正他们的错误,则表单值应包含他们先前输入的任何更新文本。Drupal 在刷新要显示的表单时使用 form_state 填充表单,因此如果您只设置#default_value,则不必担心那方面的事情。

于 2014-10-03T00:15:01.137 回答