drupal 7のフォームAPIを使用して、「送信」タイプのボタンではなく、通常の「ボタン」タイプのボタンを作成しようとしていますが、取得できないようです。
#typeを「button」に設定したり、#button_typeを「button」に設定したりするなど、さまざまなことを試しましたが、何をしても、drupalは常に「submit」タイプのボタンを作成します。
drupal 7のフォームAPIを使用して、「送信」タイプのボタンではなく、通常の「ボタン」タイプのボタンを作成しようとしていますが、取得できないようです。
#typeを「button」に設定したり、#button_typeを「button」に設定したりするなど、さまざまなことを試しましたが、何をしても、drupalは常に「submit」タイプのボタンを作成します。
次を使用できます。
"#executes_submit_callback" => FALSE
「送信」ステップを無効にします。
「検証」ステップのみを無効にする場合は、次を使用します。
"#limit_validation_errors" => array()
Drupal 7では、これは次を追加することで実現できます。
'#attributes' => array('onclick' =>'return(false);')、
ボタンの定義に。例えば:
$form['my_form'] = array(
'#type' => 'button',
'#attributes' => array('onclick' => 'return (false);'),
'#value' => t('My Button'),
'#prefix' => t('<div class="myButton">'),
'#suffix' => t('</div>')
);
これは私のアプリケーションで機能しました。
参照:https ://www.drupal.org/node/283065の[ボタンの無効化とオーバーライド]
非常に簡単なサイドステップは、フォームで次のとおりです
$form['your-form-element'] = array(
'#type' => 'button',
'#name' => 'any-name',
'#value' => t('Button Text'),
);
そしてあなたのフォームのテンプレートで:
print str_replace('type="submit"', 'type="button"', drupal_render($form['your-form-element']));
テンプレートのtemplate.phpファイルに次の関数を追加します。
function templatename_button($variables) {
$element = $variables['element'];
$type = strtolower($element['#button_type']);
switch($type){
case 'submit':
case 'reset':
case 'button':
break;
default:
$type = 'submit';
break;
}
$element['#attributes']['type'] = $type;
element_set_attributes($element, array('id', 'name', 'value'));
$element['#attributes']['class'][] = 'form-' . $element['#button_type'];
if (!empty($element['#attributes']['disabled'])) {
$element['#attributes']['class'][] = 'form-button-disabled';
}
return '<input' . drupal_attributes($element['#attributes']) . ' />';
}
そしてあなたの形で
$form['mybutton'] = array(
'#type' => 'button',
'#value' => t('mytext'),
'#button_type' => 'button',
);
フォームを送信するためにデフォルトのボタンを定義する必要がある場合がありますが、drupalのすべてのボタン要素( #type=button
、 )は常に属性であるため、必要なデフォルトのボタンを指定するようにこの属性を変更する必要があります。submit
TYPE
"submit"
"button"
フォーム要素をレンダリングし、TYPE属性を置き換えます。
echo strtr(drupal_render($form['btn']), array('type="submit"' => 'type="button"'));
フォーム定義を変更します。
form['btn']['#attributes'] = array('onclick' => 'this.type="submit"');