MediaWiki REL1_39
FormAction.php
Go to the documentation of this file.
1<?php
30abstract class FormAction extends Action {
31
37 protected function getFormFields() {
38 // Default to an empty form with just a submit button
39 return [];
40 }
41
47 protected function preText() {
48 return '';
49 }
50
55 protected function postText() {
56 return '';
57 }
58
64 protected function alterForm( HTMLForm $form ) {
65 }
66
72 protected function usesOOUI() {
73 return false;
74 }
75
81 protected function getForm() {
82 $this->fields = $this->getFormFields();
83
84 // Give hooks a chance to alter the form, adding extra fields or text etc
85 $this->getHookRunner()->onActionModifyFormFields(
86 $this->getName(),
87 $this->fields,
88 $this->getArticle()
89 );
90
91 if ( $this->usesOOUI() ) {
92 $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
93 } else {
94 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
95 }
96 $form->setSubmitCallback( [ $this, 'onSubmit' ] );
97
98 $title = $this->getTitle();
99 $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
100 // Retain query parameters (uselang etc)
101 $params = array_diff_key(
102 $this->getRequest()->getQueryValues(),
103 [ 'action' => null, 'title' => null ]
104 );
105 if ( $params ) {
106 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
107 }
108
109 $form->addPreText( $this->preText() );
110 $form->addPostText( $this->postText() );
111 $this->alterForm( $form );
112
113 // Give hooks a chance to alter the form, adding extra fields or text etc
114 $this->getHookRunner()->onActionBeforeFormDisplay(
115 $this->getName(),
116 $form,
117 $this->getArticle()
118 );
119
120 return $form;
121 }
122
136 abstract public function onSubmit( $data );
137
143 abstract public function onSuccess();
144
153 public function show() {
154 $this->setHeaders();
155
156 // This will throw exceptions if there's a problem
157 $this->checkCanExecute( $this->getUser() );
158
159 $form = $this->getForm();
160 if ( $form->show() ) {
161 $this->onSuccess();
162 }
163 }
164
169 public function doesWrites() {
170 return true;
171 }
172}
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Actions are things which can be done to pages (edit, delete, rollback, etc).
Definition Action.php:44
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition Action.php:353
getHookRunner()
Definition Action.php:261
getName()
Return the name of the action this object responds to.
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:230
getContext()
Get the IContextSource in use here.
Definition Action.php:136
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:170
setHeaders()
Set output headers for noindexing etc.
Definition Action.php:419
getArticle()
Get a Article object.
Definition Action.php:220
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:150
An action which shows a form and does something based on the input from the form.
onSuccess()
Do something exciting on successful processing of the form.
getFormFields()
Get an HTMLForm descriptor array.
usesOOUI()
Whether the form should use OOUI.
show()
The basic pattern for actions is to display some sort of HTMLForm UI, maybe with some stuff underneat...
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
preText()
Add pre- or post-text to the form.
onSubmit( $data)
Process the form on POST submission.
getForm()
Get the HTMLForm to control behavior.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:150