7.6 修改已存在的表单

龸龵龴龰龯龬龪龨龧龦龢龞龚:

  • hook_form_alter

  • hook_form_FORM_ID_alter

齴龦龢龞龚齳齯齫龬齨齯齧齴龦龢API齟齝齙龞齕龬龸龵齑齍齋齉齆齅齄龞齋齉龬龪龨齨齀鼽鼼鼹鼸鼴齫鼱

鼰鼯鼭鼪鼩鼭鼥鼢鼠鼟鼜鼛鼹龵system_site_information_settings齴龢龪龨鼱

1鼋hook_form_FORM_ID_alter

龯hello_world.module黿黻鼸黺鼯鼜鼛黹

/**
 * Implements hook_form_FORM_ID_alter().
 */

function hello_world_form_system_site_information_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $form['site_phone'] = array(
        '#type' => 'tel',
        '#title' => t('Site phone'),
        '#default_value' => \Drupal::config('system.site')- >get('phone'),
    );

    $form['#submit'][] = 'hello_world_system_site_information_phone_submit';
}

麽麻黹$form齳齯齧麹齫麸麴齧鼭龬鼱

麳麰submit龞龚齨鼜鼛黺鼯黹

/**
 * Form callback to save site_phone.
 *
 * @param array $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */

function hello_world_system_site_information_phone_submit(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $config = \Drupal::configFactory()->getEditable('system.site');
    $config
        ->set('phone', $form_state->getValue('site_phone'))
        ->save();

}

龯麨麥麢麠麝龰齳麚麘麗鼱

本书共79小节:

评论 (写第一个评论)