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
Plain for
files of the MIME type text/. Extensions can add text extractors for
other file types.
Table of contents
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
Text in
ext_localconf.php has no effect anymore. The methods of
Text
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
Text
and is registered with the PHP attribute
As:
<?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());
}
}
- Method
canExtract Text () - Return
trueif 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. can be
used:
Extract the text of a file
Text
returns the first text extractor that can read a file, or null if there is
none:
<?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);
}
}