0

I created a custom module and in the code below the button is not rendered, there is only the text :

$form = ['#markup' => '<button type="button" class="btn btn-success" disabled="disabled">Product unavailable</button>']; 

How to display the button?

<?php

/**
 * @file
 * Hook implementations of commerce_add_to_cart_access module.
 */

use Drupal\commerce_product\Entity\ProductType;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;

function commerce_add_to_cart_access_form_commerce_order_item_add_to_cart_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  // Your original code to get store owner.
  $current_store = \Drupal::service('commerce_store.current_store');
  $owner = $current_store->getStore()->getOwner();
  if (!$owner->hasRole('marchand_premium')) {
    // Overwrite the whole form markup!
    $form = ['#markup' => '<button type="button" class="btn btn-success" disabled="disabled">Product unavailable</button>'];        
  }
}
  • if you change it to '#markup' => 'Hello World' do you see the text? – No Sssweat Oct 29 '18 at 1:07
  • @NoSssweat Yes it works, but I try to display the text in a button – 553aa08930 Oct 29 '18 at 2:01
  • If you remove/disable all CSS styles using web developer, or using some other tool, do you see the Product unavailable text? – No Sssweat Oct 29 '18 at 3:04
4

You can use the inline_template to solve the problem:

// Delete the other button.
unset($form);
$form['submit'] = [
  '#type' => 'inline_template',
  '#template' => '<button type="button" class="btn btn-success" disabled="disabled">Product unavailable</button>',
];
  • Comments are not for extended discussion; this conversation has been moved to chat. – Clive Oct 31 '18 at 15:45
1

I don't know much about commerce, but I think you need to output a render array rather than actual HTML.

So replace $form = ['#markup' ... with something like the following:

// Add the new button to the form
$form['button'] = [
  '#type' => 'button',
  '#value' => t('Product unavailable'),
  '#attributes' => [
    'class' => ['btn', 'btn-success'],
    'disabled' => 'disabled'
  ],
];

See this link for what you can render with Drupal https://api.drupal.org/api/drupal/namespace/Drupal%21Core%21Render%21Element/8.5.x

  • Thanks, with your code, the button is displayed but the module is broken. This answer does not work anymore if I apply your code drupal.stackexchange.com/questions/271623/… it shows the button "Add to cart" + the button ''Product unavailable" – 553aa08930 Oct 29 '18 at 4:08
  • Sorry not sure why the module would break with the above code, but to get rid of the duplicate button you can unset it - unset(form['existing_add_to_cart_button']) – 100pic Oct 29 '18 at 4:47

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.