ElementalGridFieldAddExistingAutocompleter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getManipulatedData() 0 29 5
1
<?php
2
3
namespace DNADesign\ElementalVirtual\Forms;
4
5
use DNADesign\ElementalVirtual\Model\ElementVirtual;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\SS_List;
11
12
/**
13
 * This class is is responsible for adding objects to another object's has_many
14
 * and many_many relation, as defined by the {@link RelationList} passed to the
15
 * {@link GridField} constructor.
16
 *
17
 * Objects can be searched through an input field (partially matching one or
18
 * more fields).
19
 *
20
 * Selecting from the results will add the object to the relation.
21
 *
22
 * Often used alongside {@link GridFieldDeleteAction} for detaching existing
23
 * records from a relationship.
24
 *
25
 * For easier setup, have a look at a sample configuration in
26
 * {@link GridFieldConfig_RelationEditor}.
27
 *
28
 * @package forms
29
 * @subpackage fields-gridfield
30
 */
31
class ElementalGridFieldAddExistingAutocompleter extends GridFieldAddExistingAutocompleter
32
{
33
34
    /**
35
     * If an object ID is set, add the object to the list
36
     *
37
     * @param GridField $gridField
38
     * @param SS_List $dataList
39
     * @return SS_List
40
     */
41
    public function getManipulatedData(GridField $gridField, SS_List $dataList)
42
    {
43
        if (!$gridField->State->GridFieldAddRelation) {
44
            return $dataList;
45
        }
46
47
        $objectID = Convert::raw2sql($gridField->State->GridFieldAddRelation);
48
49
        if ($objectID) {
50
            $object = DataObject::get_by_id($gridField->getModelClass(), $objectID);
0 ignored issues
show
Bug introduced by
$objectID of type array|string is incompatible with the type boolean|integer expected by parameter $idOrCache of SilverStripe\ORM\DataObject::get_by_id(). ( Ignorable by Annotation )

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

50
            $object = DataObject::get_by_id($gridField->getModelClass(), /** @scrutinizer ignore-type */ $objectID);
Loading history...
51
52
            if ($object) {
0 ignored issues
show
introduced by
$object is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
53
                // if the object is currently not linked to either a page or another list then we want to link to
54
                // the original, otherwise link to a clone
55
                if (!$object->ParentID) {
56
                    $dataList->add($object);
57
                } else {
58
                    $virtual = ElementVirtual::create();
59
                    $virtual->LinkedElementID = $object->ID;
60
                    $virtual->write();
61
62
                    $dataList->add($virtual);
63
                }
64
            }
65
        }
66
67
        $gridField->State->GridFieldAddRelation = null;
68
69
        return $dataList;
70
    }
71
}
72