diff --git a/includes/menu/Definitions.php b/includes/menu/Definitions.php index 36575a767..235350294 100644 --- a/includes/menu/Definitions.php +++ b/includes/menu/Definitions.php @@ -126,7 +126,7 @@ final class Definitions { * @param Group $group */ public function insertHomeItem( Group $group ) { - $group->insertEntry( new SingleMenuEntry( + $group->insertEntry( new HomeMenuEntry( 'home', $this->context->msg( 'mobile-frontend-home-button' )->escaped(), Title::newMainPage()->getLocalURL() diff --git a/includes/menu/HomeMenuEntry.php b/includes/menu/HomeMenuEntry.php new file mode 100644 index 000000000..3a9af074b --- /dev/null +++ b/includes/menu/HomeMenuEntry.php @@ -0,0 +1,101 @@ +component['text'] = $text; + return $this; + } + + /** + * Override the CSS class used in the home menu entry. + * + * @param string $cssClass + * @return $this + */ + public function overrideCssClass( $cssClass ) { + $this->component['class'] = $cssClass; + return $this; + } + + /** + * Create a home menu element with one component + * + * @param string $name An unique menu element identifier + * @param string $text Text to show on menu element + * @param string $url URL menu element points to + * @param bool|string $trackClicks Should clicks be tracked. To override the tracking code + * pass the tracking code as string + */ + public function __construct( $name, $text, $url, $trackClicks = true ) { + $this->name = $name; + $this->component = [ + 'text' => $text, + 'href' => $url, + 'class' => trim( MinervaUI::iconClass( $name, 'before' ) ) + ]; + if ( $trackClicks !== false ) { + $this->component['data-event-name'] = $trackClicks === true ? $name : $trackClicks; + } + } + + /** + * @inheritDoc + */ + public function getName() { + return $this->name; + } + + /** + * @inheritDoc + */ + public function getCSSClasses(): array { + return []; + } + + /** + * @inheritDoc + */ + public function getComponents(): array { + return [ $this->component ]; + } + +} diff --git a/tests/phpunit/unit/menu/HomeMenuEntryTest.php b/tests/phpunit/unit/menu/HomeMenuEntryTest.php new file mode 100644 index 000000000..ee8891c0b --- /dev/null +++ b/tests/phpunit/unit/menu/HomeMenuEntryTest.php @@ -0,0 +1,57 @@ +assertSame( $name, $entry->getName() ); + $this->assertSame( [], $entry->getCSSClasses() ); + $this->assertSame( [ [ + 'text' => $text, + 'href' => $url, + 'class' => 'mw-ui-icon mw-ui-icon-before mw-ui-icon-minerva-foo', + 'data-event-name' => 'foo' + ] ], $entry->getComponents() ); + } + + /** + * @covers ::overrideCssClass + * @covers ::overrideText + * @covers ::getComponents + */ + public function testOverride() { + $entry = new HomeMenuEntry( 'foo', 'bar', 'http://baz' ); + $component = current( $entry->getComponents() ); + $this->assertSame( 'bar', $component['text'] ); + $this->assertSame( + 'mw-ui-icon mw-ui-icon-before mw-ui-icon-minerva-foo', + $component['class'] + ); + $entry->overrideText( 'blah' ) + ->overrideCssClass( 'classy' ); + $component = current( $entry->getComponents() ); + $this->assertSame( 'blah', $component['text'] ); + $this->assertSame( + 'classy', + $component['class'] + ); + } + +}