Allow providing 'notices' for OOUI HTMLForm fields
[lhc/web/wiklou.git] / includes / htmlform / HTMLFormField.php
index d5f4cc0..afbb7d5 100644 (file)
@@ -28,7 +28,7 @@ abstract class HTMLFormField {
        protected $mShowEmptyLabels = true;
 
        /**
-        * @var HTMLForm
+        * @var HTMLForm|null
         */
        public $mParent;
 
@@ -117,8 +117,8 @@ abstract class HTMLFormField {
                        $tmp = $m[1];
                }
                if ( substr( $tmp, 0, 2 ) == 'wp' &&
-                       !isset( $alldata[$tmp] ) &&
-                       isset( $alldata[substr( $tmp, 2 )] )
+                       !array_key_exists( $tmp, $alldata ) &&
+                       array_key_exists( substr( $tmp, 2 ), $alldata )
                ) {
                        // Adjust for name mangling.
                        $tmp = substr( $tmp, 2 );
@@ -139,7 +139,7 @@ abstract class HTMLFormField {
                        $data = $alldata;
                        while ( $keys ) {
                                $key = array_shift( $keys );
-                               if ( !is_array( $data ) || !isset( $data[$key] ) ) {
+                               if ( !is_array( $data ) || !array_key_exists( $key, $data ) ) {
                                        continue 2;
                                }
                                $data = $data[$key];
@@ -598,11 +598,17 @@ abstract class HTMLFormField {
                        $error = new OOUI\HtmlSnippet( $error );
                }
 
+               $notices = $this->getNotices();
+               foreach ( $notices as &$notice ) {
+                       $notice = new OOUI\HtmlSnippet( $notice );
+               }
+
                $config = [
                        'classes' => [ "mw-htmlform-field-$fieldType", $this->mClass ],
                        'align' => $this->getLabelAlignOOUI(),
                        'help' => $helpText !== null ? new OOUI\HtmlSnippet( $helpText ) : null,
                        'errors' => $errors,
+                       'notices' => $notices,
                        'infusable' => $infusable,
                ];
 
@@ -840,6 +846,30 @@ abstract class HTMLFormField {
                return $errors;
        }
 
+       /**
+        * Determine notices to display for the field.
+        *
+        * @since 1.28
+        * @return string[]
+        */
+       function getNotices() {
+               $notices = [];
+
+               if ( isset( $this->mParams['notice-message'] ) ) {
+                       $notices[] = $this->getMessage( $this->mParams['notice-message'] )->parse();
+               }
+
+               if ( isset( $this->mParams['notice-messages'] ) ) {
+                       foreach ( $this->mParams['notice-messages'] as $msg ) {
+                               $notices[] = $this->getMessage( $msg )->parse();
+                       }
+               } elseif ( isset( $this->mParams['notice'] ) ) {
+                       $notices[] = $this->mParams['notice'];
+               }
+
+               return $notices;
+       }
+
        /**
         * @return string HTML
         */
@@ -1095,16 +1125,13 @@ abstract class HTMLFormField {
         * @return Message
         */
        protected function getMessage( $value ) {
-               if ( $value instanceof Message ) {
-                       return $value;
-               } elseif ( $value instanceof MessageSpecifier ) {
-                       return Message::newFromKey( $value );
-               } elseif ( is_array( $value ) ) {
-                       $msg = array_shift( $value );
-                       return $this->msg( $msg, $value );
-               } else {
-                       return $this->msg( $value, [] );
+               $message = Message::newFromSpecifier( $value );
+
+               if ( $this->mParent ) {
+                       $message->setContext( $this->mParent );
                }
+
+               return $message;
        }
 
        /**