/**
* Method to get the id used for the field input tag.
*
* @param string $fieldId The field element id.
* @param string $fieldName The field element name.
*
* @return string The id to be used for the field input tag.
*
* @since 1.7.0
*/
protected function getId($fieldId, $fieldName)
{
$id = '';
// If there is a form control set for the attached form add it first.
if ($this->formControl) {
$id .= $this->formControl;
}
// If the field is in a group add the group control to the field id.
if ($this->group) {
// If we already have an id segment add the group control as another level.
if ($id) {
$id .= '_' . str_replace('.', '_', $this->group);
} else {
$id .= str_replace('.', '_', $this->group);
}
}
// If we already have an id segment add the field id/name as another level.
if ($id) {
$id .= '_' . ($fieldId ?: $fieldName);
} else {
$id .= $fieldId ?: $fieldName;
}
// Clean up any invalid characters.
$id = preg_replace('#\\W#', '_', $id);
// If this is a repeatable element, add the repeat count to the ID
if ($this->repeat) {
$repeatCounter = empty($this->form->repeatCounter) ? 0 : $this->form->repeatCounter;
$id .= '-' . $repeatCounter;
if (strtolower($this->type) === 'radio') {
$id .= '-';
}
}
return $id;
}