Test Setup Failed
Pull Request — master (#197)
by Gorrie
01:07
created

src/Extensions/ElementalPageExtension.php (2 issues)

1
<?php
2
3
namespace DNADesign\Elemental\Extensions;
4
5
use DNADesign\Elemental\Models\ElementalArea;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\View\Parsers\HTML4Value;
9
10
class ElementalPageExtension extends ElementalAreasExtension
11
{
12
    /**
13
     * @var array
14
     */
15
    private static $has_one = [
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
16
        'ElementalArea' => ElementalArea::class
17
    ];
18
19
    /**
20
     * @var array
21
     */
22
    private static $owns = [
0 ignored issues
show
The private property $owns is not used, and could be removed.
Loading history...
23
        'ElementalArea'
24
    ];
25
26
    /**
27
     * Returns the contents of each ElementalArea has_one's markup for use in Solr search indexing
28
     *
29
     * @return string
30
     */
31
    public function getElementsForSearch()
32
    {
33
        $output = [];
34
        foreach ($this->owner->hasOne() as $key => $class) {
35
            if ($class !== ElementalArea::class) {
36
                continue;
37
            }
38
39
            /** @var ElementalArea $area */
40
            $area = $this->owner->$key();
41
            if ($area) {
42
                $output[] = strip_tags($area->forTemplate());
43
            }
44
        }
45
        return implode($output);
46
    }
47
48
    public function MetaTags(&$tags)
49
    {
50
        $controller = Controller::curr();
51
        $request = $controller->getRequest();
52
        if ($request->getVar('ElementalPreview') !== null) {
53
            $html = HTML4Value::create($tags);
54
            $xpath = "//meta[@name='x-page-id' or @name='x-cms-edit-link']";
55
            $removeTags = $html->query($xpath);
56
            $body = $html->getBody();
57
            foreach ($removeTags as $tag) {
58
                $body->removeChild($tag);
59
            }
60
            $tags = $html->getContent();
61
        }
62
    }
63
}
64