Skip to content

Commit abb0e63

Browse files
committed
example saving cp form for add-ons
some code fixes
1 parent 8827c7d commit abb0e63

File tree

7 files changed

+90
-52
lines changed

7 files changed

+90
-52
lines changed

docs/development/modules.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ NOTE:Before adding a Control Panel route to your add-on, you need to already hav
2727
If your add-on doesn't already have the required `ControlPanel/Routes` and `views` files, you can add a route to your add-on using the `make:cp-route` command in the CLI.
2828

2929
```
30-
$ php system/ee/eeclip.php make:cp-route
30+
$ php system/ee/eecli.php make:cp-route
3131
Let's create a control panel route!
3232
What is the route name? index
3333
What add-on is the route being added to? [amazing_add_on]: amazing_add_on
@@ -399,12 +399,44 @@ if (isset($form)) {
399399
}
400400
```
401401

402-
You're add-on will now have a Control Panel page with a form as seen in this screenshot:
402+
Your add-on will now have a Control Panel page with a form as seen in this screenshot:
403403

404404
![add-on with form](_images/add-on-view.png)
405405

406+
Finally, make the form submission do something. The form is being submitted to same CP controller, so we can check whether it has been submitted by adding `ee('Request')->isPost()` check. For the sake of this demo, we'll skip the database saving part and just display success message on submission.
406407

407-
TIP: This is only the beginning of what you can do with forms in the Control Panel. Read more in the docs on the [`CP\Form` service](development/services/cp-form.md)() to understand what else is possible.
408+
```
409+
public function process($id = false)
410+
{
411+
if (ee('Request')->isPost()) {
412+
ee('CP/Alert')->makeBanner('amazing_add_on')
413+
->asSuccess()
414+
->withTitle(lang('success'))
415+
->addToBody('Form has been submitted!')
416+
->now();
417+
}
418+
419+
// set the breadcrumb
420+
$this->addBreadcrumb('index', 'Home');
421+
422+
// call our getForm() method to get
423+
// our array
424+
$form = $this->getForm();
425+
426+
// store our form in our $variables array
427+
// to be passed into our view
428+
$variables = [
429+
'form' => $form
430+
];
431+
432+
$this->setBody('Index', $variables);
433+
434+
return $this;
435+
}
436+
```
437+
438+
439+
TIP: This is only the beginning of what you can do with forms in the Control Panel. Read more in the docs on the [`CP\Form` service](development/services/cp-form.md) to understand what else is possible.
408440

409441
TIP: **{ee:u}** Learn more about the [CP\Form service on ExpressionEngine University](https://u.expressionengine.com/article/ultra-double-secret-manual-shared-form-part-four).
410442

docs/development/services/cp-form.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
@license https://expressionengine.com/license Licensed under Apache License, Version 2.0
88
-->
99

10-
# Cp/Form Service
10+
# CP/Form Service
1111

1212
[TOC]
1313

14-
The Cp/Form Service allows for the creation of Shared Form View data using an object oriented interface.
14+
The CP/Form Service allows for the creation of Shared Form View data using an object oriented interface.
1515

1616
TIP: Note this requires PHP >= 7.1
1717

1818
## Usage
1919

20-
The below shows The Cp/Form Service at its simplest; every form can contain multiple groups, which can contain multiple field sets, which can contain multiple fields.
20+
The below shows The CP/Form Service at its simplest; every form can contain multiple groups, which can contain multiple field sets, which can contain multiple fields.
2121

2222
### Basic
2323
```
@@ -53,7 +53,7 @@ $form->toArray();
5353
```
5454
## API Reference
5555

56-
**class `ExpressionEngine\Library\Cp\Form`**
56+
**class `ExpressionEngine\Library\CP\Form`**
5757

5858
This object contains the complete outline for your Shared Form View array. For simple forms, all you'll have to do is call the Service using EE's loader, but for more complex Forms, you have quite a few options available.
5959

@@ -65,7 +65,7 @@ Will output the Form object as a Tabbed format array form.
6565

6666
| Parameter | Type | Description |
6767
| --------- | ---- | ----------- |
68-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
68+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
6969

7070
### `isTab()`
7171

@@ -81,7 +81,7 @@ Will output the Form object as a linear array form (the default)
8181

8282
| Parameter | Type | Description |
8383
| --------- | ---- | ----------- |
84-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
84+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
8585

8686
### `getGroup($group_name)`
8787

@@ -104,15 +104,15 @@ Removes the specified group from the Form object
104104

105105
### `toArray()`
106106

107-
Returns the entire Cp\Form object into an array compatible with the Shared Form View layer. Note that all child elements are converted to an array as well.
107+
Returns the entire CP\Form object into an array compatible with the Shared Form View layer. Note that all child elements are converted to an array as well.
108108

109109
| Parameter | Type | Description |
110110
| --------- | ---- | ----------- |
111111
| Returns | `array` | The complete Shared Form View array |
112112

113113
### `render()`
114114

115-
Returns the entire Cp\Form object as a string for use within the Control Panel.
115+
Returns the entire CP\Form object as a string for use within the Control Panel.
116116

117117
| Parameter | Type | Description |
118118
| --------- | ---- | ----------- |
@@ -124,16 +124,16 @@ Sets the Shared Form View layer to set the form input's `enctype` to multipart/f
124124

125125
| Parameter | Type | Description |
126126
| --------- | ---- | ----------- |
127-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
127+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
128128

129129
### `addAlert($alert_name)`
130130

131-
Sets the Form to render specific Cp/Alert objects
131+
Sets the Form to render specific CP/Alert objects
132132

133133
| Parameter | Type | Description |
134134
| --------- | ---- | ----------- |
135135
| \$alert_name | `string` | The name for the Alert |
136-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
136+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
137137

138138
### `removeAlert($alert_name)`
139139

@@ -152,7 +152,7 @@ Will return the specified `Form\Button` object if it exists, or prepare and retu
152152
| Parameter | Type | Description |
153153
| --------- | ---- | ----------- |
154154
| \$name | `string` | The name for the Button |
155-
| Returns | `Cp\Form\Button` | The Button object ready for use |
155+
| Returns | `CP\Form\Button` | The Button object ready for use |
156156

157157
### `removeButton($name)`
158158

@@ -170,7 +170,7 @@ Will return the specified `Form\Fields\Hidden` object if it exists, or prepare a
170170
| Parameter | Type | Description |
171171
| --------- | ---- | ----------- |
172172
| \$name | `string` | The name for the Button |
173-
| Returns | `Cp\Form\Fields\Hidden` | The Hidden Field object |
173+
| Returns | `CP\Form\Fields\Hidden` | The Hidden Field object |
174174

175175
### `removeHiddenField($name)`
176176

@@ -190,15 +190,15 @@ Will include a custom HTML button with a link in lieu of the top right button of
190190
| \$text | `string` | The value to display on your custom button |
191191
| \$href | `string` | What URL to send the user to |
192192
| \$rel | `string` | For HTML directives |
193-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
193+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
194194

195195
### `withOutActionButton()`
196196

197197
Will remove the set action button
198198

199199
| Parameter | Type | Description |
200200
| --------- | ---- | ----------- |
201-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
201+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
202202

203203
### `setCpPageTitle($text)`
204204

@@ -207,7 +207,7 @@ Sets the string to use for the Page Title within the Control Panel
207207
| Parameter | Type | Description |
208208
| --------- | ---- | ----------- |
209209
| \$text | `string` | The name for the page |
210-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
210+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
211211

212212
### `getCpPageTitle()`
213213

@@ -224,7 +224,7 @@ Sets the string to use for the Alternative Page Title within the Control Panel (
224224
| Parameter | Type | Description |
225225
| --------- | ---- | ----------- |
226226
| \$text | `string` | The name for the page |
227-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
227+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
228228

229229
### `getCpPageTitle()`
230230

@@ -241,7 +241,7 @@ Sets the internal Alerts ID to use specific to this Form (`alerts_name`)
241241
| Parameter | Type | Description |
242242
| --------- | ---- | ----------- |
243243
| \$text | `string` | The name for the page |
244-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
244+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
245245

246246
### `getAlertsName()`
247247

@@ -258,7 +258,7 @@ The URL to process the Form (`base_url`)
258258
| Parameter | Type | Description |
259259
| --------- | ---- | ----------- |
260260
| \$url | `string` | The URL the form should be processed at |
261-
| Returns | `Cp\Form` | `$this`, the Form object to help in chaining |
261+
| Returns | `CP\Form` | `$this`, the Form object to help in chaining |
262262

263263
### `getBaseUrl()`
264264

docs/development/services/cp-form/buttons.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $button->setType('submit')->setText('Submit Button')
2626

2727
## API Reference
2828

29-
**class `ExpressionEngine\Library\Cp\Form\Button`**
29+
**class `ExpressionEngine\Library\CP\Form\Button`**
3030

3131
[TOC=3]
3232

@@ -40,7 +40,7 @@ Returns the name used upon creation for the Button and the id.
4040

4141
### `toArray()`
4242

43-
Returns the entire `Cp\Form\Button` object into an array.
43+
Returns the entire `CP\Form\Button` object into an array.
4444

4545
| Parameter | Type | Description |
4646
| --------- | ---- | ----------- |

docs/development/services/cp-form/field-sets.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ It's important to note that while Field Sets are built to contain multiple Field
3333

3434
## API Reference
3535

36-
**class `ExpressionEngine\Library\Cp\Form\Set`**
36+
**class `ExpressionEngine\Library\CP\Form\Set`**
3737

3838
[TOC=3]
3939

4040
### `toArray()`
4141

42-
Returns the entire `Cp\Form\Set` object into an array. Note that all child elements are converted to an array as well.
42+
Returns the entire `CP\Form\Set` object into an array. Note that all child elements are converted to an array as well.
4343

4444
| Parameter | Type | Description |
4545
| --------- | ---- | ----------- |

0 commit comments

Comments
 (0)