1 | <?php |
||
22 | class GridFieldAddByDBField implements GridField_ActionProvider, GridField_HTMLProvider |
||
23 | { |
||
24 | use Injectable; |
||
25 | |||
26 | /** |
||
27 | * HTML Fragment to render the field. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $targetFragment; |
||
32 | |||
33 | /** |
||
34 | * Default field to create the DataObject by should be Title. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $dataObjectField = 'Title'; |
||
39 | |||
40 | /** |
||
41 | * Name for the buttons displayed in the CMS |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $buttonName; |
||
46 | |||
47 | /** |
||
48 | * Creates a text field and add button which allows the user to directly create a new |
||
49 | * DataObject by just entering the title. |
||
50 | * |
||
51 | * @param string $targetFragment |
||
52 | * @param string $dataObjectField |
||
53 | */ |
||
54 | public function __construct($targetFragment = 'before', $dataObjectField = 'Title') |
||
59 | |||
60 | /** |
||
61 | * Provide actions to this component. |
||
62 | * |
||
63 | * @param GridField $gridField |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function getActions($gridField) |
||
73 | |||
74 | /** |
||
75 | * Handles the add action for the given DataObject. |
||
76 | * |
||
77 | * @param $gridField GridField |
||
78 | * @param $actionName string |
||
79 | * @param $arguments mixed |
||
80 | * @param $data array |
||
81 | * |
||
82 | * @return null|HTTPResponse |
||
83 | * |
||
84 | * @throws UnexpectedValueException |
||
85 | */ |
||
86 | public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
||
142 | |||
143 | /** |
||
144 | * Returns the database field for which we'll add the new data object. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getDataObjectField() |
||
152 | |||
153 | /** |
||
154 | * Set the button name |
||
155 | * |
||
156 | * @param $name string |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function setButtonName($name) |
||
165 | |||
166 | /** |
||
167 | * Set the database field. |
||
168 | * |
||
169 | * @param $field string |
||
170 | */ |
||
171 | public function setDataObjectField($field) |
||
175 | |||
176 | /** |
||
177 | * Renders the TextField and add button to the GridField. |
||
178 | * |
||
179 | * @param $gridField GridField |
||
180 | * |
||
181 | * @return string[] |
||
182 | */ |
||
183 | public function getHTMLFragments($gridField) |
||
241 | } |
||
242 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.