Passed
Push — master ( d17640...0c3f03 )
by Robbie
01:58
created

GridFieldDetailFormItemRequestExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateBreadcrumbs() 0 11 2
A updateItemEditForm() 0 16 3
1
<?php
2
3
namespace DNADesign\Elemental\Extensions;
4
5
use SilverStripe\ORM\FieldType\DBField;
6
use SilverStripe\Core\Extension;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\ORM\CMSPreviewable;
9
use SilverStripe\CMS\Controllers\SilverStripeNavigator;
10
use SilverStripe\Forms\LiteralField;
11
use DNADesign\Elemental\Models\BaseElement;
12
13
class GridFieldDetailFormItemRequestExtension extends Extension
14
{
15
    public function updateBreadcrumbs($crumbs)
16
    {
17
        $record = $this->owner->getRecord();
18
19
        if ($record instanceof BaseElement) {
20
            $last = $crumbs->Last();
21
22
            $last->Title = DBField::create_field('HTMLVarchar', sprintf(
23
                "%s <small>(%s)</small>",
24
                DBField::create_field('Varchar', $last->Title)->XML(),
25
                $record->getType()
26
            ));
27
        }
28
    }
29
30
    /**
31
     * Updates the edit form to inject the preview panel controls if needed
32
     * i.e. if the class being edited implements CMSPreviewable
33
     *
34
     * @param SilverStripe\Forms\Form $form to be modified by reference
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Exte...SilverStripe\Forms\Form was not found. Did you mean SilverStripe\Forms\Form? If so, make sure to prefix the type with \.
Loading history...
35
     */
36
    public function updateItemEditForm(&$form)
37
    {
38
        $fields = $form->Fields();
39
        if ($this->owner->record instanceof CMSPreviewable &&
40
            !$fields->fieldByName('SilverStripeNavigator')
41
        ) {
42
            $template = Controller::curr()
43
                ->getTemplatesWithSuffix('_SilverStripeNavigator');
44
            $navigator = SilverStripeNavigator::create($this->owner->record);
0 ignored issues
show
Bug introduced by
$this->owner->record of type SilverStripe\ORM\CMSPreviewable is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            $navigator = SilverStripeNavigator::create(/** @scrutinizer ignore-type */ $this->owner->record);
Loading history...
45
            $field = LiteralField::create(
46
                'SilverStripeNavigator',
0 ignored issues
show
Bug introduced by
'SilverStripeNavigator' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
                /** @scrutinizer ignore-type */ 'SilverStripeNavigator',
Loading history...
47
                $navigator->renderWith($template)
48
            )->setAllowHTML(true);
49
            $fields->push($field);
50
            $form->addExtraClass('cms-previewable')
51
                ->removeExtraClass('cms-panel-padded center');
52
        }
53
    }
54
}
55