1. Go to this page and download the library: Download contentful/rich-text library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
contentful / rich-text example snippets
php
php
$parser = new Contentful\RichText\Parser($linkResolver);
// Fetch some data from an entry field from Contentful
/** @var Contentful\RichText\Node\NodeInterface $node */
$node = $parser->parseLocalized($data, $locale);
php
$renderer = new Contentful\RichText\Renderer();
$output = $renderer->render($node);
php
use Twig\Environment;
use Contentful\RichText\NodeRenderer\NodeRendererInterface;
use Contentful\RichText\Node\NodeInterface;
use Contentful\RichText\Node\Heading1;
use Contentful\RichText\RendererInterface;
class TwigCustomHeading1 implements NodeRendererInterface
{
/**
* @var Environment
*/
private $twig;
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public function supports(NodeInterface $node): bool
{
return $node instanceof Heading1;
}
public function render(RendererInterface $renderer, NodeInterface $node, array $context = []): string
{
$context['node'] = $node;
return $this->twig->render('heading1.html.twig', $context);
}
}
php
$renderer = new Contentful\RichText\Renderer();
// Register the Twig extension, which will provide functions
// rich_text_render() and rich_text_render_collection()
// in a Twig template
$extension = new Contentful\RichText\Bridge\TwigExtension($renderer);
/** @var Twig\Environment $twig */
$twig->addExtension($extension);
// Finally, tell the main renderer about your custom node renderer
$customTwigHeading1NodeRenderer = new TwigCustomHeading1($twig);
$renderer->pushNodeRenderer($customTwigHeading1NodeRenderer);
php
$renderer = new \Contentful\RichText\Renderer();
// Register the Plates extension, which will provide functions
// $this->richTextRender() and $this->richTextRenderCollection()
// in a Plates template
$extension = new \Contentful\RichText\Bridge\PlatesExtension($renderer);
/** @var League\Plates\Engine $plates */
$plates->loadExtension($extension);
// Finally, tell the main renderer about your custom node renderer
$customPlatesHeading1NodeRenderer = new PlatesCustomHeading1($plates);
$renderer->pushNodeRenderer($customPlatesHeading1NodeRenderer);
php
// The function will output HTML, so remember *not* to escape it using $this->e()
<?= $this->richTextRenderCollection($node->getContent())
php
$renderer = new Contentful\RichText\Renderer();
$renderer->appendNodeRenderer(new Contentful\RichText\NodeRenderer\CatchAll());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.