1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Extensions; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\ElementalArea; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\View\Parsers\HTML4Value; |
8
|
|
|
use SilverStripe\View\SSViewer; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method ElementalArea ElementalArea() |
12
|
|
|
* @property int ElementalAreaID |
13
|
|
|
*/ |
14
|
|
|
class ElementalPageExtension extends ElementalAreasExtension |
15
|
|
|
{ |
16
|
|
|
private static $has_one = [ |
|
|
|
|
17
|
|
|
'ElementalArea' => ElementalArea::class, |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
private static $owns = [ |
|
|
|
|
21
|
|
|
'ElementalArea', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
private static $cascade_duplicates = [ |
|
|
|
|
25
|
|
|
'ElementalArea', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The delimiter to separate distinct elements in indexed content. |
30
|
|
|
* |
31
|
|
|
* When using the getElementsForSearch() method to index all elements in a single field, |
32
|
|
|
* a custom delimiter can be used help to avoid false positive results for phrase queries. |
33
|
|
|
* |
34
|
|
|
* @config |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private static $search_index_element_delimiter = ' '; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the contents of each ElementalArea has_one's markup for use in Solr or Elastic search indexing |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getElementsForSearch() |
45
|
|
|
{ |
46
|
|
|
$oldThemes = SSViewer::get_themes(); |
47
|
|
|
SSViewer::set_themes(SSViewer::config()->get('themes')); |
48
|
|
|
try { |
49
|
|
|
$output = []; |
50
|
|
|
foreach ($this->owner->hasOne() as $key => $class) { |
51
|
|
|
if ($class !== ElementalArea::class) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
/** @var ElementalArea $area */ |
55
|
|
|
$area = $this->owner->$key(); |
56
|
|
|
if ($area) { |
57
|
|
|
foreach ($area->Elements() as $element) { |
58
|
|
|
if ($element->getSearchIndexable()) { |
59
|
|
|
$content = $element->getContentForSearchIndex(); |
60
|
|
|
if ($content) { |
61
|
|
|
$output[] = $content; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} finally { |
68
|
|
|
// Reset theme if an exception occurs, if you don't have a |
69
|
|
|
// try / finally around code that might throw an Exception, |
70
|
|
|
// CMS layout can break on the response. (SilverStripe 4.1.1) |
71
|
|
|
SSViewer::set_themes($oldThemes); |
72
|
|
|
} |
73
|
|
|
return implode($this->owner->config()->get('search_index_element_delimiter'), $output); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function MetaTags(&$tags) |
77
|
|
|
{ |
78
|
|
|
if (!Controller::has_curr()) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
$controller = Controller::curr(); |
82
|
|
|
$request = $controller->getRequest(); |
83
|
|
|
if ($request->getVar('ElementalPreview') !== null) { |
84
|
|
|
$html = HTML4Value::create($tags); |
85
|
|
|
$xpath = "//meta[@name='x-page-id' or @name='x-cms-edit-link']"; |
86
|
|
|
$removeTags = $html->query($xpath); |
87
|
|
|
$body = $html->getBody(); |
88
|
|
|
foreach ($removeTags as $tag) { |
89
|
|
|
$body->removeChild($tag); |
90
|
|
|
} |
91
|
|
|
$tags = $html->getContent(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|