Using the ContentObjectRenderer

In some cases, the presence of the current ContentObjectRenderer may be necessary in the DataProvider. For this case an interface Fr\Typo3Handlebars\ContentObjectRendererAwareInterface is provided, which can be used in combination with the trait Fr\Typo3Handlebars\Traits\ContentObjectRendererAwareTrait.

Usage

  1. Transfer of the ContentObjectRenderer

    If the rendering process is triggered via TypoScript, the DataProcessor is automatically assigned the current instance of the ContentObjectRenderer (via the cObj property). It can then pass this to the DataProvider:

    # Classes/DataProcessing/CustomProcessor.php
    
    namespace Vendor\Extension\DataProcessing;
    
    use Fr\Typo3Handlebars\DataProcessing\AbstractDataProcessor;
    
    class CustomProcessor extends AbstractDataProcessor
    {
        protected function render(): string
        {
            $this->provider->setContentObjectRenderer($this->cObj);
            // ...
        }
    }
    
  2. Assure ContentObjectRenderer is available

    In the DataProvider, the existence of the ContentObjectRenderer can be easily checked if the associated trait is used:

    # Classes/Data/CustomProvider.php
    
    namespace Vendor\Extension\Data;
    
    use Fr\Typo3Handlebars\ContentObjectRendererAwareInterface;
    use Fr\Typo3Handlebars\Data\DataProviderInterface;
    use Fr\Typo3Handlebars\Data\Response\ProviderResponseInterface;
    use Fr\Typo3Handlebars\Traits\ContentObjectRendererAwareTrait;
    
    class CustomProvider implements DataProviderInterface, ContentObjectRendererAwareInterface
    {
        use ContentObjectRendererAwareTrait;
    
        public function get(array $data): ProviderResponseInterface
        {
            $this->assertContentObjectRendererIsAvailable();
            // ...
        }
    }
    
  3. Use the ContentObjectRenderer

    If successful, the ContentObjectRenderer can then be used, for example, to parse database content generated using RTE:

     # Classes/Data/CustomProvider.php
    
     namespace Vendor\Extension\Data;
    
     use Fr\Typo3Handlebars\ContentObjectRendererAwareInterface;
     use Fr\Typo3Handlebars\Data\DataProviderInterface;
     use Fr\Typo3Handlebars\Data\Response\ProviderResponseInterface;
     use Fr\Typo3Handlebars\Traits\ContentObjectRendererAwareTrait;
    +use Vendor\Extension\Data\Response\CustomProviderResponse;
    
     class CustomProvider implements DataProviderInterface, ContentObjectRendererAwareInterface
     {
         use ContentObjectRendererAwareTrait;
    
         public function get(array $data): ProviderResponseInterface
         {
             $this->assertContentObjectRendererIsAvailable();
    -        // ...
    +
    +        $text = $this->parseText($data);
    +
    +        return new CustomProviderResponse($text);
        }
    +
    +    private function parseText(string $plaintext): string
    +    {
    +        return $this->contentObjectRenderer->parseFunc($plaintext, [], '< lib.parseFunc_RTE');
    +    }
     }
    

Sources