Skip to content

Commit

Permalink
Fix placeholder showing "Select a User" when in readonly mode (e.g. …
Browse files Browse the repository at this point in the history
…modified_by)

Additional changes:
- Change htmlspecialchars($VARNAME, ENT_COMPAT, 'UTF-8') to $this->escape($VARNAME)
- Refactor code to remove the ternary overuse
- Change JHtml::script call to JHtml::_('script' ...) to allow overrides via custom register method
- Correct variable name ($exclude) in doc block to match extracted name
- Remove obsolete extracted variables that are not present for this field type
- Removed the class suffix from the hidden field
- Remove (Fix) obsolete whitespace in class attribute for the visible field when no suffix was entered
  • Loading branch information
matrikular committed Oct 29, 2016
1 parent a9c02b1 commit bf78bf4
Showing 1 changed file with 57 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

defined('_JEXEC') or die;

use Joomla\Utilities\ArrayHelper;

extract($displayData);

/**
Expand Down Expand Up @@ -37,63 +39,84 @@
* @var boolean $spellcheck Spellcheck state for the form field.
* @var string $validate Validation rules to apply.
* @var string $value Value attribute of the field.
* @var array $checkedOptions Options that will be set as checked.
* @var boolean $hasValue Has this field a value assigned?
* @var array $options Options available for this field.
*
* @var string $userName The user name
* @var mixed $groups The filtering groups (null means no filtering)
* @var mixed $exclude The users to exclude from the list of users
* @var mixed $excluded The users to exclude from the list of users
*/

// Set the link for the user selection page
$link = 'index.php?option=com_users&view=users&layout=modal&tmpl=component&required='
. ($required ? 1 : 0) . '&field={field-user-id}&ismoo=0'
. (isset($groups) ? ('&groups=' . base64_encode(json_encode($groups))) : '')
. (isset($excluded) ? ('&excluded=' . base64_encode(json_encode($excluded))) : '');
JHtml::_('script', 'jui/fielduser.min.js', false, true, false, false, true);

$uri = new JUri('index.php?option=com_users&view=users&layout=modal&tmpl=component&required=0&field={field-user-id}&ismoo=0');

if ($required)
{
$uri->setVar('required', 1);
}

if (!empty($groups))
{
$uri->setVar('groups', base64_encode(json_encode($groups)));
}

if (!empty($excluded))
{
$uri->setVar('excluded', base64_encode(json_encode($excluded)));
}

// Invalidate the input value if no user selected
if (JText::_('JLIB_FORM_SELECT_USER') == htmlspecialchars($userName, ENT_COMPAT, 'UTF-8'))
if ($this->escape($userName) === JText::_('JLIB_FORM_SELECT_USER'))
{
$userName = '';
}

$inputAttributes = array(
'type' => 'text', 'id' => $id, 'class' => 'field-user-input-name', 'value' => $this->escape($userName)
);

if ($class)
{
$inputAttributes['class'] .= ' ' . $class;
}

if ($size)
{
$inputAttributes['size'] = (int) $size;
}

if ($required)
{
$inputAttributes['required'] = 'required';
}

if (!$readonly)
{
$userName = "";
$inputAttributes['placeholder'] = JText::_('JLIB_FORM_SELECT_USER');
}

JHtml::script('jui/fielduser.min.js', false, true, false, false, true);
?>
<?php // Create a dummy text field with the user name. ?>
<div class="field-user-wrapper"
data-url="<?php echo $link; ?>"
data-modal=".modal"
data-modal-width="100%"
data-modal-height="400px"
data-input=".field-user-input"
data-input-name=".field-user-input-name"
data-button-select=".button-select"
>
data-url="<?php echo (string) $uri; ?>"
data-modal=".modal"
data-modal-width="100%"
data-modal-height="400px"
data-input=".field-user-input"
data-input-name=".field-user-input-name"
data-button-select=".button-select">
<div class="input-append">
<input
type="text" id="<?php echo $id; ?>"
value="<?php echo htmlspecialchars($userName, ENT_COMPAT, 'UTF-8'); ?>"
placeholder="<?php echo JText::_('JLIB_FORM_SELECT_USER'); ?>"
readonly
class="field-user-input-name <?php echo $class ? (string) $class : ''?>"
<?php echo $size ? ' size="' . (int) $size . '"' : ''; ?>
<?php echo $required ? 'required' : ''; ?>/>
<input <?php echo ArrayHelper::toString($inputAttributes); ?> readonly />
<?php if (!$readonly) : ?>
<a class="btn btn-primary button-select" title="<?php echo JText::_('JLIB_FORM_CHANGE_USER') ?>"><span class="icon-user"></span></a>
<?php echo JHtml::_(
'bootstrap.renderModal',
'userModal_' . $id,
array(
'title' => JText::_('JLIB_FORM_CHANGE_USER'),
'title' => JText::_('JLIB_FORM_CHANGE_USER'),
'closeButton' => true,
'footer' => '<a class="btn" data-dismiss="modal">' . JText::_('JCANCEL') . '</a>'
)
); ?>
<input type="hidden" id="<?php echo $id; ?>_id" name="<?php echo $name; ?>" value="<?php echo (int) $value; ?>" class="field-user-input" data-onchange="<?php echo $this->escape($onchange); ?>" />
<?php endif; ?>
</div>
<?php // Create the real field, hidden, that stored the user id. ?>
<input type="hidden" id="<?php echo $id; ?>_id" name="<?php echo $name; ?>" value="<?php echo (int) $value; ?>"
class="field-user-input <?php echo $class ? (string) $class : ''?>"
data-onchange="<?php echo $this->escape($onchange); ?>"/>
</div>

0 comments on commit bf78bf4

Please sign in to comment.