isStacked = boolval($params['stacked']); } if ( array_key_exists('fieldset', $params) ) { $this->wantsFieldset = $params['fieldset']; } if ( isset($params['fullWidth']) ) { $this->isFullWidth = boolval($params['fullWidth']); } $this->parseEnabledParam($params); } public function add($child) { if ( $child instanceof Section ) { throw new \InvalidArgumentException('Control groups cannot contain sections.'); } parent::add($child); } /** * @return string|null */ public function getLabelFor() { if ( $this->labelFor === null ) { //Automatically choose a target for the label only if there is exactly //one control that wants a label, and it is the first child. $target = null; $childNumber = 0; foreach ($this->children as $child) { $childNumber++; if ( ($child instanceof Control) && $child->parentLabelEnabled() ) { if ( $childNumber === 1 ) { $target = $child; } else { //Either this is not the first child, or more than one //child wants a label. $target = null; break; } } } if ( $target instanceof Control ) { $this->labelFor = $target->getPrimaryInputId(); } else { $this->labelFor = false; } } return $this->labelFor ?: null; } public function isStacked() { return $this->isStacked; } public function wantsFieldset() { return $this->wantsFieldset; } /** * @return bool */ public function isFullWidth() { return $this->isFullWidth; } protected function getJsUiElementType() { return 'control-group'; } public function serializeForJs() { $result = parent::serializeForJs(); $labelFor = $this->getLabelFor(); if ( $labelFor !== null ) { $result['labelFor'] = $labelFor; } return $result; } protected function getKoComponentParams() { $params = parent::getKoComponentParams(); $params['enabled'] = $this->serializeConditionForJs(); //The "full width" flag is not directly relevant to group components because they //are usually already full width, but it is sometimes used to disable the title //(e.g. for groups that contain only a single checkbox). if ( $this->isFullWidth() ) { $params['isFullWidth'] = true; $params['titleDisabled'] = true; } return $params; } }