处理block的配置表单

官网: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'];
  }
}

你可能感兴趣的:(处理block的配置表单)