Score:0

Checkout Flow Submit Pane Form Checkbox to Boolean Field

br flag

I create a Checkbox in the Checkout Flow, CheckoutPane. How i can send the Value of the Checkbox to a Entity in the User-Profile or the Order.

Description: I need a Checkbox in the CheckoutPane, when the Customer select the Checkbox and click submit i must save the value in a Field in the Order or Profile of the specific user with the date. When the customer select the checkbox first time, its hidden for future orders.

The checkbox is correctly printed, but cannot submit the value of the checkbox to a entity in order, or better a entity in the user-profile. I create the Field field_customer_check in the Order and try it same as in the Drupal Commerce Checkout Flow Example with comments. https://docs.drupalcommerce.org/commerce2/developer-guide/checkout/create-custom-checkout-pane

public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $values = $form_state->getValue($pane_form['#parents']);
  $this->order->setData('order_comment', $values['comment']);
}

enter image description here

enter image description here Here is my previous Code for the CheckoutPane:

<?php

namespace Drupal\mymodule_checkout\Plugin\Commerce\CheckoutPane;

use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce\InlineFormManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;


/**
 * Provides the completion message pane.
 *
 * @CommerceCheckoutPane(
 *   id = "agree_terms",
 *   label = @Translation("Permanent agree"),
 * )
 */
class AgreeTerms extends CheckoutPaneBase implements CheckoutPaneInterface {
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $order_id = $this->order->id();

        $pane_form['user_check_accept'] = [
          '#type' => 'checkbox', 
          '#default_value' => FALSE,
          '#title' =>$this->t('My Checkbox Title'),
          '#required' => FALSE,
          '#weight' => $this->getWeight(),
        ];

        return $pane_form;
        
  }
public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $values = $form_state->getValue($pane_form['#parents']);

  // How to submit the value from a Checkbox in a Boolean Field in the Order
  // Problem, field_customer_check not get any data 
  $this->order->setData('field_customer_check', $values['user_check_accept']);

}

 }
Score:1
eg flag

If I got the question right, you have 2 different use cases; first, you want to update the user field with values from the pane, and the second is to hide it if the user already filled the information in a previous purchase.

To solve the first problem, you can update the user fields by calling the current user and setting the value that you want as the following:

public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
  $values = $form_state->getValue($pane_form['#parents']);

  // Get the current user entity.
  $user = User::load(\Drupal::currentUser());

  // Set the value of the field.
  $user->FIELD_NAME->value = $form_state->getValue('THE VALUE');

  // Save the user entity.
  $user->save;

}

For the second part of the question, you need to check for the values while building the form and you can get the current user in the same way using

$user = User::load(\Drupal::currentUser()->id());

and to check for the existing value in the same way:

if (!empty($user->FIELD_NAME->value)) { DO SOMETHING }
Eurasia-Lab avatar
br flag
I try it many times, i have the invisible function, it works. I get the Value from the field_revoked_consent. But i cannot save the checkbox value to the boolean-field. Maybe you have a solution ?
apaderno avatar
us flag
The `User::load()` parameter is the user ID. `User::load(\Drupal::currentUser())` won't work.
Score:0
br flag

I try now many times to save it in the user field_revoke_consent, but i not bring the value from the checkbox into this field.

  public function submitPaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
    $values = $form_state->getValue($pane_form['#parents']);

    // Get the current user entity.
    $user = User::load(\Drupal::currentUser()->id());
    $fieldconsent = ('field_revoke_consent');
     // Set the value of the field.
     $user->$fieldconsent->value = $form_state->getValue($pane_form['user_consent_accept']);
  
  
      // Save the user entity.
    $user->save;


    /*
    $pane = $form_state->getValue('agree_terms');

    $values = $form_state->getValue($pane_form['#parents']);
    
    $checked = $form_state->getValue($pane_form['user_consent_accept']['#default_value']);
 
    $user = User::load(\Drupal::currentUser()->id());

   if($checked ==['#default_value' => 1] ) {$user->field_revoke_consent->value = 1; }

   $user->field_revoke_consent->value = $form_state->getValue($pane_form['user_consent_accept']);  
  $user->save;
   */  
  } 
}
Hodba Khalaf avatar
eg flag
you need to remove the ->id() as it calls the user ID. just use. "$user = User::load(\Drupal::currentUser());"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.