猿问

我的Drupal 8自定义模块在日志中创建错误

我为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;

        }

      }

    }


慕桂英3389331
浏览 181回答 1
1回答

紫衣仙女

似乎这行是问题所在(并且有多个实例):$selectedVariation = ProductVariation::load($selectedVariationId);如果$selectedVariationId是NULL,似乎导致错误。也许用if (!empty($selectedVariationId))或类似的符号包围该块?
随时随地看视频慕课网APP
我要回答