drupal version issue drupal 8 to drupal 10

16 Views Asked by At

Question I need to add the attribute on the input field in the product subscription form.

In Drupal 8 use attributes as the variation. In Drupal 10 the form shows the attribute in the form field except variation.

In Drupal 10 I did not include the the js files used in this functions I create the product attribute and use it as a product variation but in frontend shows as the attribute div structure I need the variations in the frontend.

code: .module file

function commerce_form_customizations_preprocess_input__radio(&$variables) {

  if ($variables['element']['#type'] == 'radio' && strpos($variables['element']['#id'], 'edit-purchased-entity-0-variation') !== FALSE) {

    $variation = \Drupal\commerce_product\Entity\ProductVariation::load($variables['element']['#return_value']);
    if ($variation->bundle() === 'default') {
      if (is_array($variables['attributes'])) {
        $variables['attributes']['data-js-pricing-length'] = $variation->getAttributeValue('attribute_length')
          ->label();
      }
      else {
        $variables['attributes']->setAttribute('data-js-pricing-length', $variation->getAttributeValue('attribute_length')->label());
      }
    }
  }

}

function commerce_form_customizations_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if (strpos($form_id, 'commerce_order_item_add_to_cart_form_commerce_product') !== FALSE) {
    if (strpos($form_id, 'commerce_product_bundle') !== FALSE) {
      return;
    }
    $first_option = '';
    if($form['purchased_entity']['widget'][0]['variation']['#options']){

      $first_option = reset($form['purchased_entity']['widget'][0]['variation']['#options']);

    }
    if (stripos($first_option, 'Rx Bricks Library') !== FALSE) {
      commerce_form_customizations_ensure_options_sort($form['purchased_entity']['widget'][0]['variation']['#options'])
    }
    $form['purchased_entity']['widget'][0]['variation']['#type'] = 'radios';
    $form['#prefix'] = '<div class="add-to-cart commerce-product-add-to-cart-form">';
    unset($form['purchased_entity']['widget'][0]['variation']['#default_value']);
    unset($form['purchased_entity']['widget'][0]['variation']['#ajax']);
    commerce_form_customizations_update_variation_titles($form['purchased_entity']['widget'][0]['variation']['#options']);
    $form['#attached']['library'][] = 'commerce_form_customizations/add_to_cart_guard';
    $form['#attached']['library'][] = 'commerce_form_customizations/facebook_pixel_events';
    $form['#attached']['library'][] = 'commerce_form_customizations/google_events';
  }

 
  if (strpos($form_id, 'views_form_commerce_cart_form_default') !== FALSE) {
   
    $form['actions']['submit']['#access'] = FALSE;
    $form['#attached']['library'][] = 'commerce_form_customizations/facebook_pixel_events';
  }

 
  if (strpos($form_id, 'views_form_commerce_cart_form_default') !== FALSE || $form_id === 'commerce_checkout_flow_multistep_default') {
    $form['#attached']['library'][] = 'commerce_form_customizations/prevent_duplicate_click';

  }
 
  if ($form_id == 'commerce_checkout_flow_multistep_default') {
    $block_manager = \Drupal::service('plugin.manager.block');
    $plugin_block = $block_manager->createInstance('order_summary_block', []);
    $form['sidebar']['order_summary'] = $plugin_block->build();
    
    $form['#attached']['library'][] = 'commerce_form_customizations/facebook_pixel_events';
    $form['#attached']['library'][] = 'commerce_form_customizations/google_events';
  }
  if ($form_id == 'commerce_checkout_flow_multistep_default' && $form['#step_id'] == 'review') {
    if (isset($form['sidebar']) && isset($form['sidebar']['coupon_redemption'])) {
      $form['sidebar']['coupon_redemption']['#access'] = FALSE;
    }
  }
}

can you please help me to solve this problem Thanks

0

There are 0 best solutions below