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

My custom module for Drupal 8 creates errors in the logs

我为 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;
        }
      }
    }
    // Store type personnel.
    elseif ($bundle == 'personnel') {
      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('Offline sales');
        $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;
      }
    }
    // Store type professionnel.
    elseif ($bundle == 'professionnel') {

        if (!$owner->hasRole('marchand_premium')) {
          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('Offline sales');
            $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;
          }
        }

    }
  }
}

PHP 警告:

Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 264 of /var/www/www-example-com/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php) #0 /var/www/www-example-com/web/core/includes/bootstrap.inc(584): _drupal_error_handler_real(2, 'array_flip(): C...', '/var/www/www-s1...', 264, Array) #1 [internal function]: _drupal_error_handler(2, 'array_flip(): C...', '/var/www/www-s1...', 264, Array) #2 /var/www/www-example-com/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(264): array_flip(Array) #3 /var/www/www-example-com/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(249): Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) #4 /var/www/www-example-com/web/core/lib/Drupal/Core/Entity/Entity.php(527): Drupal\Core\Entity\EntityStorageBase->load(NULL) #5 /var/www/www-example-com/web/modules/custom/commerce_marketplace_premium_merchant/commerce_marketplace_premium_merchant.module(25): Drupal\Core\Entity\Entity::load(NULL) #6 /var/www/www-example-com/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(539): commerce_marketplace_premium_merchant_form_commerce_order_item_add_to_cart_form_alter(Array, Object(Drupal\Core\Form\FormState), 'commerce_order_...') #7 /var/www/www-example-com/web/core/lib/Drupal/Core/Form/FormBuilder.php(834): Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object(Drupal\Core\Form\FormState), 'commerce_order_...') #8 /var/www/www-example-com/web/core/lib/Drupal/Core/Form/FormBuilder.php(276): Drupal\Core\Form\FormBuilder->prepareForm('commerce_order_...', Array, Object(Drupal\Core\Form\FormState)) #9 /var/www/www-example-com/web/modules/contrib/commerce/modules/product/src/ProductLazyBuilders.php(96): Drupal\Core\Form\FormBuilder->buildForm('commerce_order_...', Object(Drupal\Core\Form\FormState)) #10 [internal function]: Drupal\commerce_product\ProductLazyBuilders->addToCartForm('50', 'full', true, 'fr') #11 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(358): call_user_func_array(Array, Array) #12 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, true) #13 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(151): Drupal\Core\Render\Renderer->render(Array, true) #14 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() #15 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(152): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #16 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(166): Drupal\Core\Render\Renderer->renderPlain(Array) #17 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(667): Drupal\Core\Render\Renderer->renderPlaceholder('<drupal-render-...', Array) #18 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(552): Drupal\Core\Render\Renderer->replacePlaceholders(Array) #19 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, true) #20 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(139): Drupal\Core\Render\Renderer->render(Array, true) #21 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() #22 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/Renderer.php(140): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #23 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php(258): Drupal\Core\Render\Renderer->renderRoot(Array) #24 /var/www/www-example-com/web/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php(131): Drupal\Core\Render\HtmlResponseAttachmentsProcessor->renderPlaceholders(Object(Drupal\Core\Render\HtmlResponse)) #25 /var/www/www-example-com/web/core/lib/Drupal/Core/EventSubscriber/HtmlResponseSubscriber.php(45): Drupal\Core\Render\HtmlResponseAttachmentsProcessor->processAttachments(Object(Drupal\Core\Render\HtmlResponse)) #26 [internal function]: Drupal\Core\EventSubscriber\HtmlResponseSubscriber->onRespond(Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent), 'kernel.response', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) #27 /var/www/www-example-com/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent), 'kernel.response', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) #28 /var/www/www-example-com/vendor/symfony/http-kernel/HttpKernel.php(191): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent)) #29 /var/www/www-example-com/vendor/symfony/http-kernel/HttpKernel.php(130): Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object(Drupal\Core\Render\HtmlResponse), Object(Symfony\Component\HttpFoundation\Request), 1) #30 /var/www/www-example-com/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #31 /var/www/www-example-com/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #32 /var/www/www-example-com/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #33 /var/www/www-example-com/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #34 /var/www/www-example-com/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #35 /var/www/www-example-com/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #36 /var/www/www-example-com/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #37 /var/www/www-example-com/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #38 /var/www/www-example-com/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #39 /var/www/www-example-com/web/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #40 /var/www/www-example-com/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #41 {main}.

这是模块所在的页面:

<article{{ attributes.addClass('commerce-product--default--full') }}>

  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bs-region-header">

    {{ product.stores }}

    <div class="row">

      <h1>{{ product_entity.title.value }}</h1>

      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 bs-left">
        {{ product.variation_field_default_image }}
      </div>

      <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 bs-right">
        <div class="bs-field-variation">
          {{ product.variation_sku }}
          {{ product.variation_field_default_sku_store }}
          {{ product.variation_list_price }}
          {{ product.variation_price }}
          {{ product.variations }}
          <div class="prerequis-abonnement">
            <i><b>Pourquoi le bouton "S'abonner" est-il désactivé ?</b><br>
            Vous devez créer une boutique professionnelle et y ajouter des produits ou des services.<br>
            Une fois que celle-ci aura été validée par l'administrateur, votre abonnement sera automatiquement activé.<br>
            <u>Vous n'avez pas besoin d'entrer vos coordonnées bancaires</u></i> <i class="far fa-thumbs-up"></i>
          </div>
        </div>
      </div>

      <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bs-bottom">
        {{ product.body }}
        {{ product.flag_report_product }}
        {{ product.flag_like_product }}
        {{ drupal_block('shariff_block') }}
        {{ product_entity.links }}
      </div>

    </div>

  </div>

</article>

似乎这一行是问题所在(并且有多个实例):

$selectedVariation = ProductVariation::load($selectedVariationId);

$selectedVariationIdNULL 时,似乎会导致错误。

也许用 if (!empty($selectedVariationId)) 或类似的词围绕该块?