0

We have a custom module, mymodule that has a form, mymoduleForm with two fields: enabled content types and urls.

We no longer need the enabled content types field on the form and want to remove it from the form and configuration. We do not have either config/install/mymodule.settings.yml and config/schema/mymodule.schema.yml files.

How can we remove the field (enabled content types) from both the form and configuration?

mymoduleForm.php (all lines below we are thinking need to be removed related to enabled_content_types)

public function buildForm(array $form, FormStateInterface $form_state) {
  $mymodule_config = $this->config('mymodule.settings');

  $types = \Drupal::entityTypeManager()
    ->getStorage('node_type')
    ->loadMultiple();

  foreach($types as $type) {
    $content_types[$type->id()] = $type->label();
  }

  $form['enabled_content_types'] = [
    '#type' => 'checkboxes', 
    '#title' => $this->t('Enabled Content Types'),
    '#description' => $this->t('content types .'),
    '#default_value' => $config->get('enabled_content_types'),
    '#options' => $content_types,
  ];

  ...

  return parent::buildForm($form, $form_state);
}

public function submitForm(array &$form, FormStateInterface $form_state) {
  $mymodule_config = $this->config('mymodule.settings');
  $mymodule_config
    ->set('enabled_content_types', $form_state->getValue('enabled_content_types'))
    ->set('urls', $form_state->getValue('urls'))
    ->save();

  parent::submitForm($form, $form_state);
}
1

change this line

->set('enabled_content_types', $form_state->getValue('enabled_content_types')) 

to

->clear('enabled_content_types')

In Drupal, go to config form and click save.

Side Note: Alternatively, you could also delete the config via Drupal Console or Drush as mentioned in the answers here, How do I remove a configuration object from the active configuration?

Now you may now remove the relevant code.

public function buildForm(array $form, FormStateInterface $form_state) {
  $mymodule_config = $this->config('mymodule.settings');

  $types = \Drupal::entityTypeManager() // remove
    ->getStorage('node_type') // remove
    ->loadMultiple(); // remove

  foreach($types as $type) { // remove
    $content_types[$type->id()] = $type->label(); // remove
  } // remove

  $form['enabled_content_types'] = [ // remove
    '#type' => 'checkboxes', // remove 
    '#title' => $this->t('Enabled Content Types'), // remove
    '#description' => $this->t('content types .'), // remove
    '#default_value' => $config->get('enabled_content_types'), // remove
    '#options' => $content_types, // remove
  ]; // remove

  ...

  return parent::buildForm($form, $form_state);
}

public function submitForm(array &$form, FormStateInterface $form_state) {
  $mymodule_config = $this->config('mymodule.settings');
    ->clear('enabled_content_types') // remove
    ->set('urls', $form_state->getValue('urls'))
    ->save();
  parent::submitForm($form, $form_state);
}
  • if i do this in my local development environment, would i have to do the same steps in our cloud Dev environment? For example (local dev), if change line to be clear(), save the form, and remove all the lines, i would not have to replicate that after deploying those changes to our cloud dev environment, correct? – usernameabc Oct 3 '18 at 16:06
  • 1
    @usernameabc yes, correct. If you don't want to do that, you could also use Drupal Console or Drush as the answers in How do I remove a configuration object from the active configuration? point out. It would be Drupal::configFactory()->getEditable('mymodule.settings')->clear('enabled_content_types');. OR you could use Devel module and paste this in the execute php and then uninstall devel, since you don't want that module enabled in production – No Sssweat Oct 3 '18 at 19:59
  • Drupal::configFactory()->getEditable('mymodule.settings')->clear('enabled_content_types'); that along with the removal of the code did it for us and we were able to get it done successfully. – usernameabc Oct 3 '18 at 20:29

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.