Pass Preprocess Values From Custom Module to Custom Form

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












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.










share|improve this question





























    up vote
    2
    down vote

    favorite












    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.










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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.










      share|improve this question















      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.







      8 forms hooks






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 25 at 2:00

























      asked Sep 25 at 1:04









      Prestosaurus

      4229




      4229




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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);






          share|improve this answer




















          • $theme = Drupal::theme()->getActiveTheme()->getName(); is a direct implementation and requires less code for this use case. Worked 'out of the box'
            – Prestosaurus
            Sep 25 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.
            – Prestosaurus
            Sep 25 at 4:52


















          up vote
          1
          down vote













          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');





          share|improve this answer




















          • 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.
            – Prestosaurus
            Sep 25 at 3:44










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "220"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f269880%2fpass-preprocess-values-from-custom-module-to-custom-form%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          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);






          share|improve this answer




















          • $theme = Drupal::theme()->getActiveTheme()->getName(); is a direct implementation and requires less code for this use case. Worked 'out of the box'
            – Prestosaurus
            Sep 25 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.
            – Prestosaurus
            Sep 25 at 4:52















          up vote
          2
          down vote



          accepted










          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);






          share|improve this answer




















          • $theme = Drupal::theme()->getActiveTheme()->getName(); is a direct implementation and requires less code for this use case. Worked 'out of the box'
            – Prestosaurus
            Sep 25 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.
            – Prestosaurus
            Sep 25 at 4:52













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          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);






          share|improve this answer












          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);







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 25 at 3:27









          Cesar Moore

          880317




          880317











          • $theme = Drupal::theme()->getActiveTheme()->getName(); is a direct implementation and requires less code for this use case. Worked 'out of the box'
            – Prestosaurus
            Sep 25 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.
            – Prestosaurus
            Sep 25 at 4:52

















          • $theme = Drupal::theme()->getActiveTheme()->getName(); is a direct implementation and requires less code for this use case. Worked 'out of the box'
            – Prestosaurus
            Sep 25 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.
            – Prestosaurus
            Sep 25 at 4:52
















          $theme = Drupal::theme()->getActiveTheme()->getName(); is a direct implementation and requires less code for this use case. Worked 'out of the box'
          – Prestosaurus
          Sep 25 at 3:46





          $theme = Drupal::theme()->getActiveTheme()->getName(); is a direct implementation and requires less code for this use case. Worked 'out of the box'
          – Prestosaurus
          Sep 25 at 3:46





          1




          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.
          – Prestosaurus
          Sep 25 at 4:52





          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.
          – Prestosaurus
          Sep 25 at 4:52













          up vote
          1
          down vote













          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');





          share|improve this answer




















          • 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.
            – Prestosaurus
            Sep 25 at 3:44














          up vote
          1
          down vote













          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');





          share|improve this answer




















          • 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.
            – Prestosaurus
            Sep 25 at 3:44












          up vote
          1
          down vote










          up vote
          1
          down vote









          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');





          share|improve this answer












          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');






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 25 at 2:26









          vinhdv

          80319




          80319











          • 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.
            – Prestosaurus
            Sep 25 at 3:44
















          • 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.
            – Prestosaurus
            Sep 25 at 3:44















          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.
          – Prestosaurus
          Sep 25 at 3:44




          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.
          – Prestosaurus
          Sep 25 at 3:44

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f269880%2fpass-preprocess-values-from-custom-module-to-custom-form%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          The Forum (Inglewood, California)

          Palaiologos