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小节:

评论 (写第一个评论)