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

评论 (写第一个评论)