HTMLFormElement.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * Allows custom data specific to HTMLFormField to be set for OOUI forms. A matching JS widget
  4. * (defined in htmlform.Element.js) picks up the extra config when constructed using OO.ui.infuse().
  5. *
  6. * Currently only supports passing 'hide-if' data.
  7. * @phan-file-suppress PhanUndeclaredMethod
  8. */
  9. trait HTMLFormElement {
  10. protected $hideIf = null;
  11. protected $modules = null;
  12. public function initializeHTMLFormElement( array $config = [] ) {
  13. // Properties
  14. $this->hideIf = $config['hideIf'] ?? null;
  15. $this->modules = $config['modules'] ?? [];
  16. // Initialization
  17. if ( $this->hideIf ) {
  18. $this->addClasses( [ 'mw-htmlform-hide-if' ] );
  19. }
  20. if ( $this->modules ) {
  21. // JS code must be able to read this before infusing (before OOUI is even loaded),
  22. // so we put this in a separate attribute (not with the rest of the config).
  23. // And it's not needed anymore after infusing, so we don't put it in JS config at all.
  24. $this->setAttributes( [ 'data-mw-modules' => implode( ',', $this->modules ) ] );
  25. }
  26. $this->registerConfigCallback( function ( &$config ) {
  27. if ( $this->hideIf !== null ) {
  28. $config['hideIf'] = $this->hideIf;
  29. }
  30. } );
  31. }
  32. }