Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

RichText/SearchField.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\RichText;
8
9 use eZ\Publish\SPI\Persistence\Content\Field;
10 use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
11 use eZ\Publish\SPI\FieldType\Indexable;
12 use eZ\Publish\SPI\Search;
13 use DOMDocument;
14 use DOMNode;
15
21 class SearchField implements Indexable
22 {
31 public function getIndexData(Field $field, FieldDefinition $fieldDefinition)
32 {
33 $document = new DOMDocument();
34 $document->loadXML($field->value->data);
35
36 return [
37 new Search\Field(
38 'value',
39 self::extractShortText($document),
40 new Search\FieldType\StringField()
41 ),
42 new Search\Field(
43 'fulltext',
44 $this->extractText($document->documentElement),
45 new Search\FieldType\FullTextField()
46 ),
47 ];
48 }
49
57 private function extractText(DOMNode $node)
58 {
59 $text = '';
60
61 if (null !== $node->childNodes && $node->childNodes->length > 0) {
62 foreach ($node->childNodes as $child) {
63 $text .= $this->extractText($child);
64 }
65 } elseif (!empty($node->nodeValue)) {
66 $text .= $node->nodeValue . ' ';
67 }
68
69 return $text;
70 }
71
81 public static function extractShortText(DOMDocument $document)
82 {
83 $result = null;
84 // try to extract first paragraph/tag
85 if ($section = $document->documentElement->firstChild) {
86 $textDom = $section->firstChild;
87
88 if ($textDom && $textDom->hasChildNodes()) {
89 $result = $textDom->firstChild->textContent;
90 } elseif ($textDom) {
91 $result = $textDom->textContent;
92 }
93 }
94
95 if ($result === null) {
96 $result = $document->documentElement->textContent;
97 }
98
99 // In case of newlines, extract first line. Also limit size to 255 which is maxsize on sql impl.
100 $lines = preg_split('/\r\n|\n|\r/', trim($result), -1, PREG_SPLIT_NO_EMPTY);
101
102 return empty($lines) ? '' : trim(mb_substr($lines[0], 0, 255));
103 }
104
110 public function getIndexDefinition()
111 {
112 return [
113 'value' => new Search\FieldType\StringField(),
114 ];
115 }
116
126 public function getDefaultMatchField()
127 {
128 return 'value';
129 }
130
140 public function getDefaultSortField()
141 {
142 return $this->getDefaultMatchField();
143 }
144 }
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\Core\FieldType\FieldType
Base class for field types, the most basic storage unit of data inside eZ Publish.
Definition: Core/FieldType/FieldType.php:35
eZ\Publish\Core\FieldType\RichText\SearchField
Indexable definition for RichText field type.
Definition: RichText/SearchField.php:22
eZ\Publish\Core\FieldType\RichText\SearchField\extractText
extractText(DOMNode $node)
Extracts text content of the given $node.
Definition: RichText/SearchField.php:57
eZ\Publish\Core\FieldType\RichText\SearchField\getIndexDefinition
getIndexDefinition()
Get index field types for search backend.
Definition: RichText/SearchField.php:110
eZ\Publish\Core\FieldType\RichText\SearchField\extractShortText
static extractShortText(DOMDocument $document)
Extracts short text content of the given $document.
Definition: RichText/SearchField.php:81
eZ\Publish\Core\FieldType\RichText\SearchField\getIndexData
getIndexData(Field $field, FieldDefinition $fieldDefinition)
Get index data for field for search backend.
Definition: RichText/SearchField.php:31
eZ\Publish\Core\FieldType\RichText\SearchField\getDefaultSortField
getDefaultSortField()
Get name of the default field to be used for sorting.
Definition: RichText/SearchField.php:140
eZ\Publish\Core\FieldType\RichText\SearchField\getDefaultMatchField
getDefaultMatchField()
Get name of the default field to be used for matching.
Definition: RichText/SearchField.php:126
eZ\Publish\SPI\Persistence\Content\Field
Definition: SPI/Persistence/Content/Field.php:12
eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.php:18
eZ\Publish\SPI\FieldType\Indexable
The field type interface which all field types have to implement to be indexable by search backends.
Definition: Indexable.php:17
eZ\Publish\SPI\Search
Definition: Capable.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.