Download the PHP package contentful/rich-text without Composer
On this page you can find all versions of the php package contentful/rich-text. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download contentful/rich-text
More information about contentful/rich-text
Files in contentful/rich-text
Package rich-text
Short Description Utilities for the Contentful Rich Text
License MIT
Informations about the package rich-text
rich-text.php
This library is built to help you with the parsing and rendering of the rich text field type in Contentful. It requires PHP 7.2 and up or PHP 8.0 and up.
Setup
Add this package to your application by using Composer and executing the following command:
Then, if you haven't already, include the Composer autoloader:
Parsing
The method Contentful\RichText\Parser::parseLocalized(array $data, string|null $locale)
accepts a valid, unserialized rich text data structure, and returns an object which implements Contentful\RichText\Node\NodeInterface
.
You will also need a link resolver to instanciate the parser.
The parser will also automatically resolve any linked assets and entries. To do this correctly, you will need to supply the current locale - otherwise, the parser will resolve the links in the default locale for the given space.
Depending of which type of node it actually is, the hierarchy can be navigated using getter methods. Please refer to the full list of available nodes for a complete reference.
Rendering
The main purpose of this library is to provide an automated way of rendering nodes. The simplest setup involves just creating an instance of the Contentful\RichText\Renderer
class:
The library provides defaults for all types of supported nodes. However, it is likely that you will need to override some of these defaults, in order to customize the output. To do this, you will create NodeRenderer
classes, which implement the Contentful\RichText\NodeRenderer\NodeRendererInterface
interface:
For instance, if you want to add a class to all h1
tags, you will have something similar to this:
Finally, you will need to tell the main renderer to use your custom node renderer:
Now all instances of Heading1
node will be rendered using the custom node renderer. You can implement any sort of logic in the supports
method, and since only an interface is required, you can inject any sort of dependency in the constructor. This is done, for instance, for allowing the use of templating engines such as Twig or Plates:
This library provides out-of-the-box support for Twig and Plates, which allow you to call RenderInterface::render()
and RenderInterface::renderCollection()
methods from a template. To enable the appropriate extension, just let the Twig environment or Plates engine know about it as described below.
Twig integration
Setup:
Template:
For an example implementation of a Twig-based rendering process, check the test node renderer and the complete integration test.
Plates integration
Setup:
Template:
For an example implementation of a Plates-based rendering process, check the test node renderer and the complete integration test.
How to avoid having the main renderer throw an exception on unknown nodes
The default renderer behavior for when it does not find an appropriate node renderer is to throw an exception. To avoid this, you must set it up to use a special catch-all node renderer:
The special Contentful\RichText\NodeRenderer\CatchAll
node renderer will return an empty string regardless of the node type. It's important to use the appendNodeRenderer
instead of the usual pushNodeRenderer
method to make this special node renderer have the lowest priority, therefore avoiding it intercepting regular node renderers.
Glossary
Name | Interface | Description |
---|---|---|
Node | Contentful\RichText\Node\NodeInterface |
The PHP representation of a rich text node |
Renderer | Contentful\RichText\RendererInterface |
A class which accepts all sorts of nodes, and then delegates rendering to the appropriate node renderer |
Node renderer | Contentful\RichText\NodeRenderer\NodeRendererInterface |
A class whose purpose is to be able to render a specific type of node |
Parser | Contentful\RichText\ParserInterface |
A class that's responsible for turning an array of unserialized JSON data into a tree of node objects |
About Contentful
Contentful is a content management platform for web applications, mobile apps and connected devices. It allows you to create, edit & manage content in the cloud and publish it anywhere via powerful API. Contentful offers tools for managing editorial teams and enabling cooperation between organizations.
License
Copyright (c) 2015-2019 Contentful GmbH. Code released under the MIT license. See LICENSE for further details.