如何在drupal 7中创建一个像按钮一样的图像按钮?

时间:2016-05-05 13:38:58

标签: drupal-7 drupal-forms drupal-fapi drupal-ajax

我希望它如何运作:

我有按钮列表(类型按钮),它触发将该项添加到列表的Ajax调用。这完全符合我的要求,除了按钮看起来很难看。

问题:

当我尝试用“图像按钮”替换“按钮”时,表单被提交,这不是我想要的。有没有办法将图像添加到不触发提交的按钮?我可以禁用“图像按钮”的提交吗?或者我应该使用按钮上的css添加图像吗?

“按钮”,“图片按钮”和“提交”之间有什么区别?

1 个答案:

答案 0 :(得分:0)

要使类型image_button像按钮一样在system.module中查找并​​找到"'#executes_submit_callback' => TRUE",,所以将其更改为false将阻止在image_button上调用提交函数。

system.module的代码:

$types['button'] = array(
    '#input' => TRUE,
    '#name' => 'op',
    '#button_type' => 'submit',
    '#executes_submit_callback' => FALSE,
    '#limit_validation_errors' => FALSE,
    '#process' => array('ajax_process_form'),
    '#theme_wrappers' => array('button'),
);
$types['image_button'] = array(
    '#input' => TRUE,
    '#button_type' => 'submit',
    '#executes_submit_callback' => TRUE,   // <--- This is why submit is triggered
    '#limit_validation_errors' => FALSE,
    '#process' => array('ajax_process_form'),
    '#return_value' => TRUE,
    '#has_garbage_value' => TRUE,
    '#src' => NULL,
    '#theme_wrappers' => array('image_button'),
);

您可能遇到的另一件事是验证错误,要删除它只是添加"'#validate' => array()",或者如果您想运行验证但忽略错误请使用"#limit_validation_errors' => array()"。这也适用于按钮。

这是一个示例,您可以在其中试验上述内容,并查看何时触发验证和提交回调。此外,还包含一个复选框,用于显示验证错误何时发生。

function button_menu()
{
    $items = array();

    $items['button'] = array(
        'title'           => 'Button',
        'page callback'   => 'drupal_get_form',
        'page arguments'  => array('button_form'),
        'access callback' => array(TRUE),
        'type'            => MENU_CALLBACK,
    );

    return $items;
}

function button_form($form, &$form_state)
{
    // Add to prove the point of how validation is working
    $form['checkbox'] = array(
        '#type'          => 'checkbox',
        '#title'         => t('checkbox'),
        '#default_value' => FALSE,
    );

    $form['button'] = array(
        '#id'       => 'button_1',
        '#type'     => 'button',
        '#name'     => 'test1',
        '#value'    => 'test1',
        //'#validate' => array(),  // This line will remove validation completely
        '#limit_validation_errors'  => array(), // This line will run validation but ignore any errors
        '#ajax'     => array(
            'callback' => 'button_test_callback',
            'wrapper'  => 'wrapper',
            'method'   => 'replace',
            'effect'   => 'fade',
        ),
    );

    $form['image_button'] = array(
        '#id'                       => 'image_button_1',
        '#type'                     => 'image_button',
        '#src'                      => '/themes/bartik/logo.png', // hope you still have bartik theme
        '#executes_submit_callback' => FALSE,   // This line removes the submit callback
        //'#validate' => array(),               // This line will remove validation completely
        '#limit_validation_errors'  => array(), // This line will run validation but ignore any errors
        '#ajax'                     => array(
            'callback' => 'button_test_callback',
            'wrapper'  => 'wrapper',
            'method'   => 'replace',
            'effect'   => 'fade',
        ),
    );

    // Just some code to show what button was pressed
    if (array_key_exists('triggering_element', $form_state) &&
        ($form_state['triggering_element']['#id'] == 'button_1' || $form_state['triggering_element']['#id'] == 'image_button_1'))
    {
        $form['markup'] = array(
            '#type' => 'markup',
            '#markup' => '<div id="wrapper"><p>'. $form_state['triggering_element']['#id'] .'</p></div>',
        );
    }
    else {
        $form['markup'] = array(
            '#type' => 'markup',
            '#markup' => '<div id="wrapper"><p>nothing</p></div>',
        );
    }

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

    return $form;
}

function button_test_callback($form, $form_state)
{
    return $form['markup'];
}

function button_form_validate($form, &$form_state)
{
    // To display when validation is triggered
    drupal_set_message('button_form_validate');

    // Only when submit button is pressed we care about this error.
    if ( $form_state['values']['checkbox'] == 0) {
        form_set_error('checkbox', 'checkbox not checked');
    }

}

function button_form_submit($form, &$form_state)
{
    // To display when submit is triggered
    drupal_set_message('button_form_submit');
}