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

评论 (写第一个评论)