prismicio-community / php-kit

Community maintained development kit for Prismic and the PHP language
https://prismic.io
Other
108 stars 82 forks source link

Attributes missing in slices with linked documents #140

Closed CalvinMcGee closed 1 year ago

CalvinMcGee commented 7 years ago

I'm having some issues with slices and document links. In my slices I have a reference to another custom type.

This is how i fetch from API:

$api = $prismic->get_api();
$page = $api->getByUID('pages', $uid, ['fetchLinks' => 'product.product_name, product.producer, product.price, product.description, product.systembolaget_link, product.product_image']);

Which gives the following result:

object(stdClass)#119 (12) {
  ["id"]=>
  string(16) "WZYBVygAAPRQNb4q"
  ["uid"]=>
  string(8) "products"
  ["type"]=>
  string(5) "pages"
  ["href"]=>
  string(142) "https://carlwine.prismic.io/api/v1/documents/search?ref=WZYYbigAAPRQNiPt&q=%5B%5B%3Ad+%3D+at%28document.id%2C+%22WZYBVygAAPRQNb4q%22%29+%5D%5D"
  ["tags"]=>
  array(0) {
  }
  ["first_publication_date"]=>
  string(24) "2017-08-17T20:50:14+0000"
  ["last_publication_date"]=>
  string(24) "2017-08-17T21:57:08+0000"
  ["slugs"]=>
  array(1) {
    [0]=>
    string(8) "products"
  }
  ["linked_documents"]=>
  NULL
  ["lang"]=>
  string(5) "sv-se"
  ["alternate_languages"]=>
  array(0) {
  }
  ["data"]=>
  object(stdClass)#112 (1) {
    ["pages"]=>
    object(stdClass)#117 (2) {
      ["title"]=>
      object(stdClass)#114 (2) {
        ["type"]=>
        string(14) "StructuredText"
        ["value"]=>
        array(1) {
          [0]=>
          object(stdClass)#113 (3) {
            ["type"]=>
            string(8) "heading1"
            ["text"]=>
            string(8) "Products"
            ["spans"]=>
            array(0) {
            }
          }
        }
      }
      ["body"]=>
      object(stdClass)#100 (2) {
        ["type"]=>
        string(9) "SliceZone"
        ["value"]=>
        array(3) {
          [0]=>
          object(stdClass)#120 (5) {
            ["type"]=>
            string(5) "Slice"
            ["slice_type"]=>
            string(7) "product"
            ["slice_label"]=>
            NULL
            ["repeat"]=>
            array(2) {
              [0]=>
              object(stdClass)#115 (1) {
                ["product"]=>
                object(stdClass)#122 (2) {
                  ["type"]=>
                  string(13) "Link.document"
                  ["value"]=>
                  object(stdClass)#123 (2) {
                    ["document"]=>
                    object(stdClass)#124 (6) {
                      ["id"]=>
                      string(16) "WZX99SgAAPRTNa8T"
                      ["type"]=>
                      string(7) "product"
                      ["tags"]=>
                      array(0) {
                      }
                      ["lang"]=>
                      string(5) "sv-se"
                      ["slug"]=>
                      string(37) "morellino-docg-poggio-valente-riserva"
                      ["data"]=>
                      object(stdClass)#125 (1) {
                        ["product"]=>
                        object(stdClass)#126 (5) {
                          ["product_name"]=>
                          array(1) {
                            [0]=>
                            object(stdClass)#127 (3) {
                              ["type"]=>
                              string(8) "heading1"
                              ["text"]=>
                              string(37) "Morellino DOCG Poggio Valente Riserva"
                              ["spans"]=>
                              array(0) {
                              }
                            }
                          }
                          ["description"]=>
                          array(1) {
                            [0]=>
                            object(stdClass)#128 (3) {
                              ["type"]=>
                              string(9) "paragraph"
                              ["text"]=>
                              string(277) "Ruby red with light garnet red reflections. In the nose we feel red fruit aromas which  reminds also of the Macchia Mediterranea perfumes. This wine is characterized by a remarkably elegant structure, well integrated tannins, full body pleasantly fresh, persistent  aftertaste."
                              ["spans"]=>
                              array(0) {
                              }
                            }
                          }
                          ["price"]=>
                          array(1) {
                            [0]=>
                            object(stdClass)#129 (3) {
                              ["type"]=>
                              string(8) "heading1"
                              ["text"]=>
                              string(3) "318"
                              ["spans"]=>
                              array(0) {
                              }
                            }
                          }
                          ["producer"]=>
                          array(1) {
                            [0]=>
                            object(stdClass)#130 (3) {
                              ["type"]=>
                              string(8) "heading1"
                              ["text"]=>
                              string(19) "Fattoria Le Pupille"
                              ["spans"]=>
                              array(0) {
                              }
                            }
                          }
                          ["product_image"]=>
                          object(stdClass)#131 (2) {
                            ["type"]=>
                            string(5) "Image"
                            ["value"]=>
                            object(stdClass)#132 (2) {
                              ["main"]=>
                              object(stdClass)#133 (4) {
                                ["dimensions"]=>
                                object(stdClass)#134 (2) {
                                  ["width"]=>
                                  int(190)
                                  ["height"]=>
                                  int(694)
                                }
                                ["alt"]=>
                                NULL
                                ["copyright"]=>
                                NULL
                                ["url"]=>
                                string(104) "https://prismic-io.s3.amazonaws.com/carlwine/d634c6bd9c9af313e0d943bb9f3a0ecda1c5013b_poggio_valente.jpg"
                              }
                              ["views"]=>
                              object(stdClass)#135 (0) {
                              }
                            }
                          }
                        }
                      }
                    }
                    ["isBroken"]=>
                    bool(false)
                  }
                }
              }
              [1]=>
              object(stdClass)#136 (1) {
                ["product"]=>
                object(stdClass)#137 (2) {
                  ["type"]=>
                  string(13) "Link.document"
                  ["value"]=>
                  object(stdClass)#138 (2) {
                    ["document"]=>
                    object(stdClass)#139 (6) {
                      ["id"]=>
                      string(16) "WZX_ZygAAPRQNbWQ"
                      ["type"]=>
                      string(7) "product"
                      ["tags"]=>
                      array(0) {
                      }
                      ["lang"]=>
                      string(5) "sv-se"
                      ["slug"]=>
                      string(30) "prosecco-merotto-graziano-brut"
                      ["data"]=>
                      object(stdClass)#140 (1) {
                        ["product"]=>
                        object(stdClass)#141 (5) {
                          ["product_name"]=>
                          array(1) {
                            [0]=>
                            object(stdClass)#142 (3) {
                              ["type"]=>
                              string(8) "heading1"
                              ["text"]=>
                              string(30) "Prosecco Merotto Graziano Brut"
                              ["spans"]=>
                              array(0) {
                              }
                            }
                          }
                          ["description"]=>
                          array(1) {
                            [0]=>
                            object(stdClass)#143 (3) {
                              ["type"]=>
                              string(9) "paragraph"
                              ["text"]=>
                              string(281) "Its perfume is Fine and intense, with immediately obvious notes of white fruits and citrus alongside pleasant floral sensations softened by a mineral flourish. Taste is wonderfully fresh and elegantly tangy with good volume and balance providing an overall sense of great pleasure."
                              ["spans"]=>
                              array(0) {
                              }
                            }
                          }
                          ["price"]=>
                          array(1) {
                            [0]=>
                            object(stdClass)#144 (3) {
                              ["type"]=>
                              string(8) "heading1"
                              ["text"]=>
                              string(3) "179"
                              ["spans"]=>
                              array(0) {
                              }
                            }
                          }
                          ["producer"]=>
                          array(1) {
                            [0]=>
                            object(stdClass)#145 (3) {
                              ["type"]=>
                              string(8) "heading1"
                              ["text"]=>
                              string(7) "Merotto"
                              ["spans"]=>
                              array(0) {
                              }
                            }
                          }
                          ["product_image"]=>
                          object(stdClass)#146 (2) {
                            ["type"]=>
                            string(5) "Image"
                            ["value"]=>
                            object(stdClass)#147 (2) {
                              ["main"]=>
                              object(stdClass)#148 (4) {
                                ["dimensions"]=>
                                object(stdClass)#149 (2) {
                                  ["width"]=>
                                  int(385)
                                  ["height"]=>
                                  int(864)
                                }
                                ["alt"]=>
                                NULL
                                ["copyright"]=>
                                NULL
                                ["url"]=>
                                string(105) "https://prismic-io.s3.amazonaws.com/carlwine/adbf0c734e2d721b54def2068219db96dd061c6b_cuvee-fondatore.jpg"
                              }
                              ["views"]=>
                              object(stdClass)#150 (0) {
                              }
                            }
                          }
                        }
                      }
                    }
                    ["isBroken"]=>
                    bool(false)
                  }
                }
              }
            }
            ["non-repeat"]=>
            object(stdClass)#151 (0) {
            }
          }
          [1]=>
          object(stdClass)#152 (5) {
            ["type"]=>
            string(5) "Slice"
            ["slice_type"]=>
            string(4) "text"
            ["slice_label"]=>
            NULL
            ["repeat"]=>
            array(2) {
              [0]=>
              object(stdClass)#153 (1) {
                ["text"]=>
                object(stdClass)#154 (2) {
                  ["type"]=>
                  string(14) "StructuredText"
                  ["value"]=>
                  array(1) {
                    [0]=>
                    object(stdClass)#155 (3) {
                      ["type"]=>
                      string(9) "paragraph"
                      ["text"]=>
                      string(14) "Hejsan, hejsan"
                      ["spans"]=>
                      array(0) {
                      }
                    }
                  }
                }
              }
              [1]=>
              object(stdClass)#156 (1) {
                ["text"]=>
                object(stdClass)#157 (2) {
                  ["type"]=>
                  string(14) "StructuredText"
                  ["value"]=>
                  array(1) {
                    [0]=>
                    object(stdClass)#158 (3) {
                      ["type"]=>
                      string(9) "paragraph"
                      ["text"]=>
                      string(6) "Hopsan"
                      ["spans"]=>
                      array(0) {
                      }
                    }
                  }
                }
              }
            }
            ["non-repeat"]=>
            object(stdClass)#159 (0) {
            }
          }
          [2]=>
          object(stdClass)#160 (5) {
            ["type"]=>
            string(5) "Slice"
            ["slice_type"]=>
            string(19) "non_repeatable_text"
            ["slice_label"]=>
            NULL
            ["repeat"]=>
            array(1) {
              [0]=>
              object(stdClass)#161 (0) {
              }
            }
            ["non-repeat"]=>
            object(stdClass)#162 (1) {
              ["text"]=>
              object(stdClass)#163 (2) {
                ["type"]=>
                string(4) "Text"
                ["value"]=>
                string(6) "Hejsan"
              }
            }
          }
        }
      }
    }
  }
}

So all the attributes of the linked documents are there. So everything is fine for now. Moving on to the template:

$prismic = $WPGLOBAL['prismic'];
$page = $WPGLOBAL['page'];

$slices = $page->getSliceZone('pages.body')->getSlices();
foreach ($slices as $slice) {
    switch ($slice->getSliceType()) {
        case 'product':
            foreach ($slice->getItems()->getArray() as $product) {
              var_dump($product);
            }
            break;
    }
}

This is where it breaks. The $product variable only contains the attribute product.product_image:

object(Prismic\Fragment\GroupDoc)#168 (1) {
  ["fragments":"Prismic\WithFragments":private]=>
  array(1) {
    ["product"]=>
    object(Prismic\Fragment\Link\DocumentLink)#167 (8) {
      ["id":"Prismic\Fragment\Link\DocumentLink":private]=>
      string(16) "WZX99SgAAPRTNa8T"
      ["type":"Prismic\Fragment\Link\DocumentLink":private]=>
      string(7) "product"
      ["tags":"Prismic\Fragment\Link\DocumentLink":private]=>
      array(0) {
      }
      ["slug":"Prismic\Fragment\Link\DocumentLink":private]=>
      string(37) "morellino-docg-poggio-valente-riserva"
      ["lang":"Prismic\Fragment\Link\DocumentLink":private]=>
      string(5) "sv-se"
      ["isBroken":"Prismic\Fragment\Link\DocumentLink":private]=>
      bool(false)
      ["fragments":"Prismic\WithFragments":private]=>
      array(1) {
        ["product.product_image"]=>
        object(Prismic\Fragment\Image)#166 (2) {
          ["main":"Prismic\Fragment\Image":private]=>
          object(Prismic\Fragment\ImageView)#165 (6) {
            ["url":"Prismic\Fragment\ImageView":private]=>
            string(104) "https://prismic-io.s3.amazonaws.com/carlwine/d634c6bd9c9af313e0d943bb9f3a0ecda1c5013b_poggio_valente.jpg"
            ["alt":"Prismic\Fragment\ImageView":private]=>
            NULL
            ["copyright":"Prismic\Fragment\ImageView":private]=>
            NULL
            ["width":"Prismic\Fragment\ImageView":private]=>
            int(190)
            ["height":"Prismic\Fragment\ImageView":private]=>
            int(694)
            ["link":"Prismic\Fragment\ImageView":private]=>
            NULL
          }
          ["views":"Prismic\Fragment\Image":private]=>
          array(0) {
          }
        }
      }
      ["uid"]=>
      NULL
    }
  }
}
object(Prismic\Fragment\GroupDoc)#172 (1) {
  ["fragments":"Prismic\WithFragments":private]=>
  array(1) {
    ["product"]=>
    object(Prismic\Fragment\Link\DocumentLink)#171 (8) {
      ["id":"Prismic\Fragment\Link\DocumentLink":private]=>
      string(16) "WZX_ZygAAPRQNbWQ"
      ["type":"Prismic\Fragment\Link\DocumentLink":private]=>
      string(7) "product"
      ["tags":"Prismic\Fragment\Link\DocumentLink":private]=>
      array(0) {
      }
      ["slug":"Prismic\Fragment\Link\DocumentLink":private]=>
      string(30) "prosecco-merotto-graziano-brut"
      ["lang":"Prismic\Fragment\Link\DocumentLink":private]=>
      string(5) "sv-se"
      ["isBroken":"Prismic\Fragment\Link\DocumentLink":private]=>
      bool(false)
      ["fragments":"Prismic\WithFragments":private]=>
      array(1) {
        ["product.product_image"]=>
        object(Prismic\Fragment\Image)#170 (2) {
          ["main":"Prismic\Fragment\Image":private]=>
          object(Prismic\Fragment\ImageView)#169 (6) {
            ["url":"Prismic\Fragment\ImageView":private]=>
            string(105) "https://prismic-io.s3.amazonaws.com/carlwine/adbf0c734e2d721b54def2068219db96dd061c6b_cuvee-fondatore.jpg"
            ["alt":"Prismic\Fragment\ImageView":private]=>
            NULL
            ["copyright":"Prismic\Fragment\ImageView":private]=>
            NULL
            ["width":"Prismic\Fragment\ImageView":private]=>
            int(385)
            ["height":"Prismic\Fragment\ImageView":private]=>
            int(864)
            ["link":"Prismic\Fragment\ImageView":private]=>
            NULL
          }
          ["views":"Prismic\Fragment\Image":private]=>
          array(0) {
          }
        }
      }
      ["uid"]=>
      NULL
    }
  }
}
levimykel commented 6 years ago

Hi @CalvinMcGee. Thanks for submitting this. The issue here is that the kit can't handle the partial Rich Text fields that you are retrieving via the fetchLinks option.

This issue is resolved in the API v2 version of this kit. You can update to the new version, and you shouldn't have this issue anymore. This will take a bit of work, though, to update all your templating over to the new syntax. So be prepared for that.

Alternatively, we are planning to release an update to the fetchLinks option which should also fix this issue. We're hoping to release the update in the next month or so.

I'll leave this issue open for now. When we release fetchLinks update I'll test this and make sure that it is working as expected. Thanks for your patience.

c0nst4ntin commented 1 year ago

Alternatively, we are planning to release an update to the fetchLinks option which should also fix this issue. We're hoping to release the update in the next month or so.

@levimykel @CalvinMcGee Do either of you recall whether the patch mentioned was implemented and released? If so we could probably close this Issue?

levimykel commented 1 year ago

@c0nst4ntin I don't remember for sure, but I would guess so. And we recommend anyone using this kit to be using the API v2. Given that and how old this issue is, I say we can close this.

c0nst4ntin commented 1 year ago

@levimykel I'd say the same thing. API V2 has been released for so long, that this should be irrelevant.