Block의 설정 폼 처리

1974 단어
홈페이지:https://www.drupal.org/docs/8/creating-custom-modules/add-a-form-to-the-block-configuration
코드:
getConfiguration();
  
    if (isset($config['first_form_block_setting']) && !empty($config['first_form_block_setting'])) {
      $name = $config['first_form_block_setting'];
    }
    else {
      $name = $this->t('to no one');
    }
  
    return array(
      '#markup' => $this->t('Hello @name!', array('@name' => $name)),
    );
  }

    /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);

    $config = $this->getConfiguration();

    $form['first_form_block_setting'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Who'),
      '#description' => $this->t('Who do you want to say hello to?'),
      '#default_value' => isset($config['first_form_block_setting']) ? $config['first_form_block_setting'] : '',
    );

    return $form;
  }
  
  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    parent::blockSubmit($form, $form_state);
    $values = $form_state->getValues();
    $this->configuration['first_form_block_setting'] = $values['first_form_block_setting'];
  }
}

좋은 웹페이지 즐겨찾기