我为Drupal 8.6和Drupal Commerce 2创建了一个自定义模块。它可以工作,但是当我查看drupal日志时,每次重新加载页面时都会收到PHP警告。我该如何纠正这些警告?commerce_marketplace_premium_merchant.module :<?php/** * @file * Hook implementations of commerce_marketplace_premium_merchant module. */use Drupal\commerce_store\Entity\StoreType;use Drupal\commerce_product\Entity\ProductType;use Drupal\commerce_product\Entity\ProductVariationType;use Drupal\Core\Entity\Display\EntityViewDisplayInterface;use Drupal\Core\Url;use Drupal\Core\Form\FormStateInterface;use Drupal\commerce_product\Entity\ProductVariation;function commerce_marketplace_premium_merchant_form_commerce_order_item_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) { $current_store = \Drupal::service('commerce_store.current_store'); $owner = $current_store->getStore()->getOwner(); foreach ($form_state->getFormObject()->getEntity()->getPurchasedEntity()->getProduct()->getStores() as $store) { $bundle = $store->bundle(); // Product type abonnement. if ($bundle == 'online') { if (isset($form["#attributes"]["class"]) && in_array("commerce-order-item-add-to-cart-form", $form["#attributes"]["class"])) { $selectedVariationId = $form_state->get('selected_variation'); $selectedVariation = ProductVariation::load($selectedVariationId); $form['actions']['submit']['#value'] = t('Subscribe'); if (!$owner->hasRole('marchand')) { $form['actions']['submit']['#attributes']['class'] = array('button--add-to-cart', 'button button--primary', 'js-form-submit', 'form-submit', 'is-disabled', 'btn-warning', 'btn'); $form['actions']['submit']['#disabled'] = TRUE; } } }
1 回答
紫衣仙女
TA贡献1839条经验 获得超15个赞
似乎这行是问题所在(并且有多个实例):
$selectedVariation = ProductVariation::load($selectedVariationId);
如果$selectedVariationId
是NULL
,似乎导致错误。
也许用if (!empty($selectedVariationId))
或类似的符号包围该块?
- 1 回答
- 0 关注
- 180 浏览
添加回答
举报
0/150
提交
取消