1

I am trying to migrate a module from drupal 7 to drupal 8, but sadly I cannot find any proper documentation on how to do this. I already re-wrote .info file to .info.yml but at this point its just a dead-end. For example I have a admin.inc file which creates a configuration form for settings. It looks like this:

function commerce_shipping_gls_settings() {
  $form = array();
  $form['rates'] = array(
    '#type' => 'fieldset',
    '#title' => t('Shipping costs'),
  );
  $form['rates']['commerce_shipping_gls_shipping_rate_amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Shipping service rate amount'),
    '#default_value' => variable_get('commerce_shipping_gls_shipping_rate_amount', 3900),
    '#required' => TRUE,
    '#element_validate' => array('element_validate_integer_positive'),
  );
  return system_settings_form($form);
}

I was trying to find how to convert a config file to drupal 8, but sadly I didn't get much. Could someone please give me some guidance on how can I achieve this?

  • While porting or just working with d8, this page is a life saver: drupal.org/list-changes/drupal. You'll be able to see what the code changed to and go from there. – timodwhit Sep 29 '16 at 13:42
3

system_settings_form() is replaced by \Drupal\Core\Form\ConfigFormBase in D8.

you can see
https://www.drupal.org/node/1910694
Working with Configuration Forms

Example:

namespace Drupal\example_module\Forms;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class StudentConfigForm extends ConfigFormBase {
  // The Form ID that is used in alter hooks.
  public function getFormId() {
    return 'student_config_form';
  }
  // Configuration names that will be editable
  public function getEditableConfigNames() {
    return ['example_module.student'];
  }

  public function buildForm(array $form, FormStateInterface $form_state, $args = '' ){
    $config = $this->config('example_module.student');
    $values = $config->get();
    $form['student'] = array(
      '#type' => 'details',
      '#title' => $this->t('Student'),
      '#open' => TRUE,
      '#tree' => TRUE,
    );
    $form['student']['first_name'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('First name'),
      '#default_value' => $values['student']['first_name']
    );
    $form['student']['last_name'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Job title'),
      '#default_value' => $values['student']['last_name']
    );
    $form['student']['age'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Age'),
      '#default_value' => $values['student']['age']
    );
    return parent::buildForm($form, $form_state);
  }

  public function submitForm(array &$form, FormStateInterface $state) {
    $values = $state->getValues();
    $config = $this->config('example_module.student')
      ->set('student', $values['student'])
      ->save();
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.