Skip to content

Commit

Permalink
Add LegacySE search Field Criteria support for RichText FieldType
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerom committed Jan 7, 2019
1 parent c4a47b0 commit 413f397
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -772,18 +772,6 @@ private function getDocumentWithLocationLink(Location $location)
return $document;
}

/**
* @todo Drop this once Legacy Engine is able to do basic searching on this; shortened text like Search on sort_key_string.
*/
protected function checkSearchEngineSupport()
{
if (ltrim(get_class($this->getSetupFactory()), '\\') === 'eZ\\Publish\\API\\Repository\\Tests\\SetupFactory\\Legacy') {
$this->markTestSkipped(
"'ezrichtext' field type is not searchable with Legacy Search Engine"
);
}
}

protected function getValidSearchValueOne()
{
return <<<EOT
Expand Down
13 changes: 10 additions & 3 deletions eZ/Publish/Core/FieldType/RichText/SearchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getIndexData(Field $field, FieldDefinition $fieldDefinition)
return array(
new Search\Field(
'value',
$this->extractShortText($document),
self::extractShortText($document),
new Search\FieldType\StringField()
),
new Search\Field(
Expand Down Expand Up @@ -72,13 +72,16 @@ private function extractText(DOMNode $node)
/**
* Extracts short text content of the given $document.
*
* @internal Only for use by RichText FieldType itself.
*
* @param \DOMDocument $document
*
* @return string
*/
private function extractShortText(DOMDocument $document)
public static function extractShortText(DOMDocument $document)
{
$result = null;
// try to extract first paragraph/tag
if ($section = $document->documentElement->firstChild) {
$textDom = $section->firstChild;

Expand All @@ -93,7 +96,11 @@ private function extractShortText(DOMDocument $document)
$result = $document->documentElement->textContent;
}

return trim($result);

// In case of newlines, extract first line. Also limit size to 255 which is maxsize on sql impl.
$lines = preg_split('/\r\n|\n|\r/', trim($result), -1, PREG_SPLIT_NO_EMPTY);

return empty($lines) ? '' : trim(mb_substr($lines[0], 0, 255));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion eZ/Publish/Core/FieldType/RichText/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function validate(FieldDefinition $fieldDefinition, SPIValue $value)
*/
protected function getSortInfo(BaseValue $value)
{
return false;
return SearchField::extractShortText($value->xml);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static function create()
public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
{
$storageFieldValue->dataText = $value->data;
$storageFieldValue->sortKeyString = $value->sortKey;
}

/**
Expand All @@ -51,6 +52,7 @@ public function toStorageValue(FieldValue $value, StorageFieldValue $storageFiel
public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
{
$fieldValue->data = $value->dataText ?: Value::EMPTY_VALUE;
$fieldValue->sortKey = $value->sortKeyString;
}

/**
Expand Down Expand Up @@ -86,6 +88,6 @@ public function toFieldDefinition(StorageFieldDefinition $storageDefinition, Fie
*/
public function getIndexColumn()
{
return false;
return 'sort_key_string';
}
}

0 comments on commit 413f397

Please sign in to comment.