Skip to content

Commit

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

Additional changes:
- Remove obsolete extracted variables that are not present for this field type
- Correct variable name in doc block to match extracted name
- Refactor code to remove ternary overuse
  • Loading branch information
matrikular committed Oct 29, 2016
1 parent 2570285 commit a398760
Showing 1 changed file with 50 additions and 26 deletions.
76 changes: 50 additions & 26 deletions layouts/joomla/form/field/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

defined('JPATH_BASE') or die;

use Joomla\Utilities\ArrayHelper;

extract($displayData);

/**
Expand Down Expand Up @@ -37,46 +39,68 @@
* @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
*/

$link = 'index.php?option=com_users&view=users&layout=modal&tmpl=component&required='
. ($required ? 1 : 0) . '&field=' . htmlspecialchars($id, ENT_COMPAT, 'UTF-8')
. (isset($groups) ? ('&groups=' . base64_encode(json_encode($groups))) : '')
. (isset($excluded) ? ('&excluded=' . base64_encode(json_encode($excluded))) : '');
JHtml::_('behavior.modal', 'a.modal_' . $id);
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');

$uri->setVar('field', htmlspecialchars($id, ENT_COMPAT, 'UTF-8'));

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 (htmlspecialchars($userName, ENT_COMPAT, 'UTF-8') === JText::_('JLIB_FORM_SELECT_USER'))
{
$userName = '';
}

// Load the modal behavior script.
JHtml::_('behavior.modal', 'a.modal_' . $id);
$inputAttributes = array(
'type' => 'text', 'id' => $id, 'value' => htmlspecialchars($userName, ENT_COMPAT, 'UTF-8')
);

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

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

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

$anchorAttributes = array(
'class' => 'btn btn-primary modal_' . $id, 'title' => JText::_('JLIB_FORM_CHANGE_USER'), 'rel' => '{handler: \'iframe\', size: {x: 800, y: 500}}'
);

JHtml::script('jui/fielduser.min.js', false, true, false, false, true);
?>
<?php // Create a dummy text field with the user name. ?>
<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
<?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 modal_<?php echo $id; ?>" title="<?php echo JText::_('JLIB_FORM_CHANGE_USER'); ?>" href="<?php echo $link; ?>" rel="{handler: 'iframe', size: {x: 800, y: 500}}">
<span class="icon-user"></span>
</a>
<?php echo JHtml::_('link', (string) $uri, '<span class="icon-user"></span>', $anchorAttributes); ?>
<?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; ?>" data-onchange="<?php echo $this->escape($onchange); ?>"/>
<input type="hidden" id="<?php echo $id; ?>_id" name="<?php echo $name; ?>" value="<?php echo (int) $value; ?>" data-onchange="<?php echo $this->escape($onchange); ?>" />

0 comments on commit a398760

Please sign in to comment.