Skip to content

Commit a7dd6a5

Browse files
committed
Issue #66 Replace from raw template to rendered one if possible
1 parent e30930e commit a7dd6a5

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

classes/search/cmsfield.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
defined('MOODLE_INTERNAL') || die();
2020

21+
use mod_cms\local\model\cms;
22+
use mod_cms\local\renderer;
23+
2124
require_once($CFG->dirroot . '/mod/cms/lib.php');
2225

2326
/**
@@ -95,7 +98,6 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
9598
* @return \core_search\document
9699
*/
97100
public function get_document($record, $options = []) {
98-
global $DB;
99101
try {
100102
$cm = $this->get_cm('cms', $record->id, $record->courseid);
101103
$context = \context_module::instance($cm->id);
@@ -130,9 +132,21 @@ public function get_document($record, $options = []) {
130132
$value = $record->value;
131133
$valueformat = $record->valueformat;
132134
}
135+
133136
// Add mustache template to value.
134137
if (!empty($defaultvalues[$record->typeid]->mustache)) {
135-
$value .= ' ' . $defaultvalues[$record->typeid]->mustache;
138+
$cms = new cms($cm->instance);
139+
$renderer = new renderer($cms);
140+
ob_start();
141+
try {
142+
// Indexer uses "Empty" session, it may get an error from rendering.
143+
$value .= $renderer->get_html();
144+
} catch (\Exception $e) {
145+
// Use template when an error occurs.
146+
$value .= ' ' . $defaultvalues[$record->typeid]->mustache;
147+
}
148+
// Do not show any errors from rendering.
149+
ob_end_clean();
136150
if (empty($title)) {
137151
$title = $defaultvalues[$record->typeid]->name;
138152
}

tests/search/search_test.php

-6
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,6 @@ public function test_get_document_recordset(): void {
190190
$this->assertEquals($this->field->get('name'), $doc->get('title'));
191191
$this->assertStringContainsString($data->value, $doc->get('content'));
192192
$this->assertStringContainsString($this->cmstype->get('mustache'), $doc->get('content'));
193-
194-
// Static caches are working.
195-
$dbreads = $DB->perf_get_reads();
196-
$doc = $searcharea->get_document($record);
197-
$this->assertEquals($dbreads, $DB->perf_get_reads());
198-
$this->assertInstanceOf('\core_search\document', $doc);
199193
$count++;
200194
}
201195
$this->assertEquals(1, $count);

0 commit comments

Comments
 (0)