Text extractors for the file abstraction layer (FAL) 

A text extractor returns the text content of a file, for example for a search index. TYPO3 Core ships PlainTextExtractor for files of the MIME type text/plain. Extensions can add text extractors for other file types.

Register a text extractor with #[AsTextExtractor] 

Changed in version 15.0 Breaking: #110286 - Text extractor registration and interface changed

Text extractors are registered as services. A registration with TextExtractorRegistry->registerTextExtractor() in ext_localconf.php has no effect anymore. The methods of TextExtractorInterface are now strictly typed. To support TYPO3 v14 as well, keep the registration in ext_localconf.php and add the attribute.

A text extractor implements TextExtractorInterface and is registered with the PHP attribute AsTextExtractor :

EXT:my_extension/Classes/Resource/TextExtraction/MarkdownTextExtractor.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Resource\TextExtraction;

use TYPO3\CMS\Core\Attribute\AsTextExtractor;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\TextExtraction\TextExtractorInterface;

#[AsTextExtractor(priority: 10)]
final class MarkdownTextExtractor implements TextExtractorInterface
{
  public function canExtractText(FileInterface $file): bool
  {
    return $file->getExtension() === 'md';
  }

  public function extractText(FileInterface $file): string
  {
    // Remove the Markdown markup for headlines, emphasis, and code
    return (string)preg_replace('/[#*_`]+/', '', $file->getContents());
  }
}
Copied!
Method canExtractText()
Return true if the extractor can read the given file.
Method extractText()
Return the text content of the file.

A text extractor is a service, so it can use dependency injection in its constructor.

Priority of text extractors 

TYPO3 asks the text extractors in the order of their priority, the highest first. The first extractor that can read the file extracts the text. The extractors of TYPO3 Core have the priority 0, so an extractor with a higher priority takes precedence. The order of extractors with the same priority is not defined. Give each extractor its own priority if the order matters.

Register a text extractor with the service tag fal.text_extractor 

Instead of the attribute, the service tag fal.text_extractor can be used:

EXT:my_extension/Configuration/Services.yaml
services:
  # Place here the default dependency injection configuration

  MyVendor\MyExtension\Resource\TextExtraction\MarkdownTextExtractor:
    tags:
      - name: fal.text_extractor
        priority: 10
Copied!

Extract the text of a file 

TextExtractorRegistry returns the first text extractor that can read a file, or null if there is none:

EXT:my_extension/Classes/Search/FileContentIndexer.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Search;

use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\TextExtraction\TextExtractorRegistry;

final readonly class FileContentIndexer
{
  public function __construct(
    private TextExtractorRegistry $textExtractorRegistry,
  ) {}

  public function getIndexableText(FileInterface $file): string
  {
    $textExtractor = $this->textExtractorRegistry->getTextExtractor($file);
    if ($textExtractor === null) {
      return '';
    }
    return $textExtractor->extractText($file);
  }
}
Copied!