Empty basic_cart_content in Drupal 8/9

Recently, it was with the next problem - in the prevailing purchased goods of the basic cart module field with the name of the product was empty.

Knowledge of the printing was a topic on drupal.org with a similar mistake where the bull was given a patch that corrected the mistake.

diff --git a/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php b/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php
index 9787f97..53f567c 100755
--- a/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php
@@ -38,15 +38,15 @@ class AddToCartFormatter extends FormatterBase {
    * {@inheritdoc}
    */
   public function viewElements(FieldItemListInterface $items, $langcode) {
-    $entity = $items->getEntity();
-    if ($entity->get('add_to_cart')->getValue()[0]['value'] == 1) {
+    $elements = array();
+    if (!empty($items->value)) {
+      $entity = $items->getEntity();
       $addtocart_wrapper_container_class = Html::escape($this->getSetting('addtocart_wrapper_container_class'));
       $addtocart_button_container_class = Html::escape($this->getSetting('addtocart_button_container_class'));
       $addtocart_button_class = Html::escape($this->getSetting('addtocart_button_class'));
       $addtocart_message_wrapper_class = Html::escape($this->getSetting('addtocart_message_wrapper_class'));
 
       $config = \Drupal::config('basic_cart.settings');
-      $elements = array();
       $option = [
         'query' => ['entitytype' => $entity->getEntityTypeId(), 'quantity' => ''],
         'absolute' => TRUE,
@@ -67,9 +67,9 @@ class AddToCartFormatter extends FormatterBase {
           '#suffix' => '</div>',
         ];
       }
+      $elements['#attached']['library'][] = 'core/drupal.ajax';
     }
 
-    $elements['#attached']['library'][] = 'core/drupal.ajax';
     return $elements;
   }
 

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.