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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package rich-text

rich-text.php

Packagist PHP minimum version CircleCI License

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.


All versions of rich-text with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
ext-json Version *
contentful/core Version ^3.0|^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package contentful/rich-text contains the following files

Loading the files please wait ....