Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
20 / 30
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
VectorComponentLanguageDropdown
66.67% covered (warning)
66.67%
20 / 30
50.00% covered (danger)
50.00%
1 / 2
5.93
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getTemplateData
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2namespace MediaWiki\Skins\Vector\Components;
3
4use MediaWiki\Title\Title;
5
6/**
7 * VectorComponentLanguageButton component
8 */
9class VectorComponentLanguageDropdown implements VectorComponent {
10    /** @var string */
11    private $label;
12    /** @var string */
13    private $ariaLabel;
14    /** @var string */
15    private $class;
16    /** @var int */
17    private $numLanguages;
18    /** @var array */
19    private $menuContentsData;
20    /** @var Title|null */
21    private $title;
22
23    /**
24     * @param string $label human readable
25     * @param string $ariaLabel label for accessibility
26     * @param string $class of the dropdown component
27     * @param int $numLanguages
28     * @param string $itemHTML the HTML of the list e.g. `<li>...</li>`
29     * @param string $beforePortlet no known usages. Perhaps can be removed in future
30     * @param string $afterPortlet used by Extension:ULS
31     * @param Title|null $title
32     */
33    public function __construct(
34        string $label, string $ariaLabel, string $class, int $numLanguages,
35        // @todo: replace with >MenuContents class.
36        string $itemHTML, string $beforePortlet = '', string $afterPortlet = '', $title = null
37    ) {
38        $this->label = $label;
39        $this->ariaLabel = $ariaLabel;
40        $this->class = $class;
41        $this->numLanguages = $numLanguages;
42        $this->menuContentsData = [
43            'html-items' => $itemHTML,
44            'html-before-portal' => $beforePortlet,
45            'html-after-portal' => $afterPortlet,
46        ];
47        $this->title = $title;
48    }
49
50    /**
51     * @inheritDoc
52     */
53    public function getTemplateData(): array {
54        $title = $this->title;
55        $isSubjectPage = $title && $title->exists() && !$title->isTalkPage();
56        // If page doesn't exist or if it's in a talk namespace, we should
57        // display a less prominent "language" button, without a label, and
58        // quiet instead of progressive. For this reason some default values
59        // should be updated for this case. (T316559)
60        $buttonClasses = 'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet';
61        if ( !$isSubjectPage ) {
62            $icon = 'language';
63            $this->class .= ' mw-portlet-lang-icon-only';
64            $labelClass = $buttonClasses . ' cdx-button--icon-only mw-portlet-lang-heading-empty';
65            $checkboxClass = 'mw-interlanguage-selector-empty';
66        } else {
67            $icon = 'language-progressive';
68            $labelClass = $buttonClasses . ' cdx-button--action-progressive'
69                . ' mw-portlet-lang-heading-' . strval( $this->numLanguages );
70            $checkboxClass = 'mw-interlanguage-selector';
71        }
72        $dropdown = new VectorComponentDropdown( 'p-lang-btn', $this->label, $this->class );
73        $dropdownData = $dropdown->getTemplateData();
74        // override default heading class.
75        $dropdownData['label-class'] = $labelClass;
76        // ext.uls.interface attaches click handler to this selector.
77        $dropdownData['checkbox-class'] = $checkboxClass;
78        $dropdownData['icon'] = $icon;
79        $dropdownData['aria-label'] = $this->ariaLabel;
80        $dropdownData['is-language-selector-empty'] = !$isSubjectPage;
81
82        return $dropdownData + $this->menuContentsData;
83    }
84}