contentful / rich-text.php

Utilities for the Contentful Rich Text
https://www.contentful.com
MIT License
12 stars 9 forks source link

link resolver. #66

Closed unprintedch closed 8 months ago

unprintedch commented 1 year ago

Hello

I'm trying the get some nice html from the json i get from the graphql api using php. But i keep stuck with the "You will also need a link resolver to instanciate the parser." I have absolutely no idea how to get it. I there a way to have a full working example from the call to the render? Is what i'm trying even correct?

Sebb767 commented 1 year ago

Hi @davouid ,

this library is not usually used on its own, therefore, this is not straight-forward :) You basically have three options here:

  1. Use the link resolver from the Contentful Client, see here: https://github.com/contentful/contentful.php/blob/master/src/Client.php#L154 . It's not accessible by default, but you can copy the steps from the constructor or inherit from it to add a Getter. I can also add a Getter, since we'll be having a release for Contentful very soon.
  2. Create your own implementation of the LinkResolver that doesn't actually resolve links. If you don't have embedded entries or assets, this is probably the easiest solution.
  3. Create your own LinkResolver that resolves Links using the GraphQL API

Here's an example for 2:

class NullLinkResolver implements \Contentful\Core\Api\LinkResolverInterface {

    public function resolveLink(Link $link, array $parameters = []): \Contentful\Core\Resource\ResourceInterface
    {
        throw new \Exception("Found a link, but this class can not resolve links!");
    }

    public function resolveLinkCollection(array $links, array $parameters = []): array
    {
        throw new \Exception("Found a link, but this class can not resolve links!");
    }
}
unprintedch commented 1 year ago

Great.

I'have tried 2, but still stuck. I must be over my head here. I had the idea that it would be easier to render this array to html with php. But in the end, I'm asking myself if it is not easier to parse the array on my own!

Thank you very much for you time.