TransWikia.com

Get Checkbox Value From Another Form

Drupal Answers Asked on December 28, 2021

How do i get the value of a checkbox from another form/page and how do i make it so if the checkbox is enabled, it’ll allow a button to be visible?

checkbox in SettingsForm.php:

$form['#tree'] = TRUE;
    $form['clients_enabled'] = array(
      '#title' => t('Enable PixelPin OpenID Connect'),
      '#type' => 'checkboxes',
      '#options' => array('pixelpin' => $this->t('Enable')), 
      '#default_value' => $clients_enabled,
    );

button ion LoginForm.php:

public function buildForm(array $form, FormStateInterface $form_state) {
    $definitions = $this->pluginManager->getDefinitions();
    foreach ($definitions as $client_id => $client) {
      foreach ($this->pluginManager->getDefinitions() as $client_name => $client_plugin) {
        if (!$this->config('pixelpin_openid_connect.settings.' . $client_id)
          ->get('enabled')) {             
          $url = Drupal::service('path.current')->getPath();

          $find = 'login';

          $pos = strpos($url, $find);

          if ($pos === false){
                $value = 'Register Using @client_title';
            } else {
                $value = 'Log in with @client_title';
            } 

          $element = 'clients_enabled[' . $client_plugin['id'] . ']';
          $form['pixelpin_openid_connect_client_' . $client_id . '_login'] = array(
            '#type' => 'submit',
            '#value' => t($value, array(
              '@client_title' => $client['label'],
            )),
            'visible' => array(
              ':input[name="' . $element . '"]' => array('checked' => TRUE),
            ),
            '#name' => $client_id,
            '#prefix' => '<div>',
            '#suffix' => '</div>',
          );
        }
      }
    }
    return $form;

One Answer

Create a config file inside your_module/config/install/.

Inside SettingsForm.php write,

$form['#tree'] = TRUE;
$form['clients_enabled'] = array(
  '#title' => t('Enable PixelPin OpenID Connect'),
  '#type' => 'checkboxes',
  '#options' => array('pixelpin' => $this->t('Enable')), 
  '#default_value' => $clients_enabled,
);

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this->config('pixelpin_openid_connect.settings');
  $config->set('clients_enabled_key', $form_state->getValue('clients_enabled'))->save();

}

Inside LoginForm.php, get it :

public function buildForm(array $form, FormStateInterface $form_state) {
$definitions = $this->pluginManager->getDefinitions();
foreach ($definitions as $client_id => $client) {
  foreach ($this->pluginManager->getDefinitions() as $client_name => $client_plugin) {
$is_enabled = isset($this->config('pixelpin_openid_connect.settings')
      ->get('clients_enabled')) ? $this->config('pixelpin_openid_connect.settings')
      ->get('clients_enabled') : false;
    if ($is_enabled) {             
      $url = Drupal::service('path.current')->getPath();

      $find = 'login';

      $pos = strpos($url, $find);

      if ($pos === false){
            $value = 'Register Using @client_title';
        } else {
            $value = 'Log in with @client_title';
        } 

      $element = 'clients_enabled[' . $client_plugin['id'] . ']';
      $form['pixelpin_openid_connect_client_' . $client_id . '_login'] = array(
        '#type' => 'submit',
        '#value' => t($value, array(
          '@client_title' => $client['label'],
        )),
        'visible' => array(
          ':input[name="' . $element . '"]' => array('checked' => TRUE),
        ),
        '#name' => $client_id,
        '#prefix' => '<div>',
        '#suffix' => '</div>',
      );
    }
  }
}
return $form;
}

Answered by Ashish Deynap on December 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP