Browse Source

related flag missing bug fix in materio_flag module

bach 10 months ago
parent
commit
2e57fbfee6

+ 35 - 25
web/modules/custom/materio_flag/src/Controller/MaterioFlagController.php

@@ -65,32 +65,42 @@ class MaterioFlagController extends ControllerBase {
 
     $data = [];
     foreach ($colls as $id => $collection) {
-      $flag_id = $collection->getRelatedFlag()->id();
-
-      // get the items
-      $itemsids = $this->flagListsService->getFlagListItemIds($flag_id,$id);
-      $items = [];
-      foreach ($this->flagListsService->getFlagListItems($itemsids) as $id => $item) {
-        // $items[] = array(
-        //   'id' => $id,
-        //   'name' => $item->getName(),
-        //   'connected_entity_id' => $item->getConnectedEntityId(),
-        // );
-        $items[] = (int)$item->getConnectedEntityId();
+      $relatedflag = $collection->getRelatedFlag();
+      if ($relatedflag) {
+        $flag_id = $relatedflag->id();
+
+        // get the items
+        $itemsids = $this->flagListsService->getFlagListItemIds($flag_id,$id);
+        $items = [];
+        foreach ($this->flagListsService->getFlagListItems($itemsids) as $id => $item) {
+          // $items[] = array(
+          //   'id' => $id,
+          //   'name' => $item->getName(),
+          //   'connected_entity_id' => $item->getConnectedEntityId(),
+          // );
+          $items[] = (int)$item->getConnectedEntityId();
+        }
+        // $items_uuids = [];
+        // foreach ($items as $nid) {
+        //   $node = $this->entityTypeManager->getStorage('node')->load($nid);
+        //   $items_uuids[] = $node->uuid();
+        // }
+
+        $data[$collection->id()] = array(
+          "id" => $collection->id(),
+          "name" => $collection->getName(),
+          "flag_id" => $flag_id,
+          "items" => $items
+          // "items_uuids" => $items_uuids
+        );
+      } else {
+        \Drupal::logger('materio_flag')->notice('%colname (%colid): Collection\'s Related flag does not exists.',
+        [
+          '%colid' => $collection->id(),
+          '%colname' => $collection->getName()
+        ]);
+
       }
-      // $items_uuids = [];
-      // foreach ($items as $nid) {
-      //   $node = $this->entityTypeManager->getStorage('node')->load($nid);
-      //   $items_uuids[] = $node->uuid();
-      // }
-
-      $data[$collection->id()] = array(
-        "id" => $collection->id(),
-        "name" => $collection->getName(),
-        "flag_id" => $flag_id,
-        "items" => $items
-        // "items_uuids" => $items_uuids
-      );
     }
 
     return new JsonResponse($data);