2

My use case:

I would like to create a unique textfield per region in a custom form.

MYMODULE/MYMODULE.module:

function MYMODULE_preprocess_region(&$variables) {

    foreach (array($variables['elements']['#region']) as $key) {
        drupal_set_message($key);
    }

}

MYMODULE/src/form/MyModuleForm.php:

protected function getEditableConfigNames() {
    return [
        'module_config.module_config_settings',
    ];
}

public function buildForm(array $form, FormStateInterface $form_state) {

    $config = $this->config('module_config.module_config_settings');

    //build this...
    $form['region_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Form Item Title),
    );

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

}

What is the appropriate way to pass variables/info from the .module file to the .form?

*Note: I have a fully functioning form, hopefully I am showing enough of it to illustrate my focus.

2 Answers 2

2

It seems that you can get the list of blocks from this answer: How do i get the all the regions available in the current theme programatically?

MYMODULE/src/form/MyModuleForm.php:

protected function getEditableConfigNames() {
    return [
        'module_config.module_config_settings',
    ];
}

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get the block list based on previous Q/A
  $theme = \Drupal::theme()->getActiveTheme()->getName();
  $system_region = system_region_list($theme, REGIONS_ALL);

  foreach ($system_region as $region) {
    $form[$region . '_title'] = [
      '#type' => 'textfield',
      '#title' => t('Form ' . $region . ' Title'),
    ];
  }

  return parent::buildForm($form, $form_state);
}
2
  • $theme = \Drupal::theme()->getActiveTheme()->getName(); is a direct implementation and requires less code for this use case. Worked 'out of the box' Sep 25, 2018 at 3:46
  • 1
    Upon testing, I did find that $theme = \Drupal::config('system.theme')->get('default'); really best fit my original desire. This ensured my front end theme regions created the field items while viewing the admin theme. This was not really specified in my question, feel free to add to your answer as I feel it remains the same. Sep 25, 2018 at 4:52
1

You try the code

function MYMODULE_preprocess_region(&$variables) {
 $regions=[];
 foreach (array($variables['elements']['#region']) as $key) {
  $regions[] =$key;
 }
$config = \Drupal::service('config.factory')->getEditable('module_config.module_config_settings');
$config->set('config_regions', $regions);
$config->save();
}

call config:

$config = $this->config('module_config.module_config_settings');
$config->get('config_regions');
1
  • I thought something like $config->set() was what I was looking for, however, $theme = \Drupal::theme()->getActiveTheme()->getName(); fits my use case. I do think this answer will be useful to users searching for their own answers. Sep 25, 2018 at 3:44

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.