If this is your first visit, check our documentation. To post, register or login.
Forums / Zen Cart Code Suggestions / tiles, ids for form elements

tiles, ids for form elements

Started by torvista on 2 Dec 2015, 11:25 PM · Views: 112

Results 1 to 7 of 7
2 Dec 2015, 11:25 PM
#1
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

tiles, ids for form elements

Various items:

  1.  Drop down menu select options
    

The select should have an id so a <label> tag can be linked to it .e.g. <label for="id"
The select and options should have a title for accessibility as this gets used by screen readers.
SO

/*
 *  Output a form pull down menu
 *  Pulls values from a passed array, with the indicated option pre-selected
 */
  function zen_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
    $field = '<select name="' . zen_output_string($name) . '"';
 
    if (zen_not_null($parameters)) $field .= ' ' . $parameters;
 
    $field .= '>' . "\n";
 
    if (empty($default) && isset($GLOBALS[$name]) && is_string($GLOBALS[$name]) ) $default = stripslashes($GLOBALS[$name]);
 
    for ($i=0, $n=sizeof($values); $i<$n; $i++) {
      $field .= '  <option value="' . zen_output_string($values[$i]['id']) . '"';
      if ($default == $values[$i]['id']) {
        $field .= ' selected="selected"';
      }
 
      $field .= '>' . zen_output_string($values[$i]['text'], array('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</option>' . "\n";
    }
    $field .= '</select>' . "\n";
 
    if ($required == true) $field .= TEXT_FIELD_REQUIRED;
 
    return $field;
  }

To

/* steve modified to add title attributes
 *  Output a form pull down menu
 *  Pulls values from a passed array, with the indicated option pre-selected
 */
  function zen_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
    $field = '<select name="' . zen_output_string($name) . '"';
    $field .= ' title="'.zen_output_string($name).'"';//steve added
    $field .= ' id="'.zen_output_string($name).'"';//steve added
    if (zen_not_null($parameters)) $field .= ' ' . $parameters;
 
    $field .= '>' . "\n";
 
    if (empty($default) && isset($GLOBALS[$name]) && is_string($GLOBALS[$name]) ) $default = stripslashes($GLOBALS[$name]);
 
    for ($i=0, $n=sizeof($values); $i<$n; $i++) {
      $field .= '  <option value="' . zen_output_string($values[$i]['id']) . '"';
     $field .= ' title="' . zen_output_string($values[$i]['text']) . '"';//steve added
      if ($default == $values[$i]['id']) {
        $field .= ' selected="selected"';
      }
 
      $field .= '>' . zen_output_string($values[$i]['text'], array('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</option>' . "\n";
    }
    $field .= '</select>' . "\n";
 
    if ($required == true) $field .= TEXT_FIELD_REQUIRED;
 
    return $field;
  }
  1.  Similarly for the input field, add an id and title
    

From

/*
 *  Output a form input field
 */
  function zen_draw_input_field($name, $value = '', $parameters = '', $type = 'text', $reinsert_value = true) {
    $field = '<input type="' . zen_output_string($type) . '" name="' . zen_sanitize_string(zen_output_string($name)) . '"';
    if ( (isset($GLOBALS[$name]) && is_string($GLOBALS[$name])) && ($reinsert_value == true) ) {
      $field .= ' value="' . zen_output_string(stripslashes($GLOBALS[$name])) . '"';
    } elseif (zen_not_null($value)) {
      $field .= ' value="' . zen_output_string($value) . '"';
    }
 
    if (zen_not_null($parameters)) $field .= ' ' . $parameters;
 
    $field .= ' />';
 
    return $field;
  }

To

/*
 *  Output a form input field
 */
  function zen_draw_input_field($name, $value = '', $parameters = '', $type = 'text', $reinsert_value = true) {
    $field = '<input type="' . zen_output_string($type) . '" name="' . zen_sanitize_string(zen_output_string($name)) . '"';
    if ( (isset($GLOBALS[$name]) && is_string($GLOBALS[$name])) && ($reinsert_value == true) ) {
      $field .= ' value="' . zen_output_string(stripslashes($GLOBALS[$name])) . '"';
    } elseif (zen_not_null($value)) {
      $field .= ' value="' . zen_output_string($value) . '"';
      $field .= ' id="' . zen_output_string($name) . '"';//steve added for label "for"
    }
    $field .= ' title="' . zen_output_string($value) . '"';//steve added for accessibility
    if (zen_not_null($parameters)) $field .= ' ' . $parameters;
 
    $field .= ' />';
 
    return $field;
  }
  1.  And while we are changing this file, you could also get rid of the spaces around the title attributes that occur in three places, as attributes should not have whitespace around them.
    

Ie
From
```php
$image .= ' title=" ' . zen_output_string($alt) . ' "';

To
```php
$image .= ' title="' . zen_output_string($alt) . '"';//steve removed spaces
  1.  And sort of related... the sideboxes that have a drop-down, have the box name tagged as a label (the rest do not have any html tags).
    

Eg ```php
$title = '<label>' . BOX_HEADING_MANUFACTURERS . '</label>';

 
This gives it slightly different css formatting (which is not really relevant).
I understand that a label  should also have a "for" attribute to link it to a form element or a form (html5). Reading around leads me to conclude that in html5 the label can be anywhere on the page if it is linked to a form rather than a form element. So it seems these labels need a "for" and the forms need an id, or get rid of the labels.

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

29 Apr 2016, 1:14 PM
#2
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Re: tiles, ids for form elements

So, no interest in making the ids auto-generated in the functions instead of passing them as an unnecessary parameter?

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

30 Apr 2016, 1:03 AM
#3
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: tiles, ids for form elements

Perhaps if prepared as a Pull Request against the ZC github package it would be incorporated? Not sure that the lack of posting to this implies a lack of interest per se.

Only thing I see/have to say about it is that there are those that say that functions shouldn't generate html related responses and the above suggestions kind of diverge from that direction even though it improves related functionality. :/

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

1 May 2016, 1:14 PM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,140
Plugin Contributions:
56

Re: tiles, ids for form elements

torvista:

So, no interest in making the ids auto-generated in the functions instead of passing them as an unnecessary parameter?
The only issue I see with the proposal is that the updated code doesn't account for function calls where the id= and/or title= attributes are passed in the $parameters. The following code segments show my suggested updates:

/* steve modified to add title attributes
 *  Output a form pull down menu
 *  Pulls values from a passed array, with the indicated option pre-selected
 */
  function zen_draw_pull_down_menu($name, $values, $default = '', $parameters = '', $required = false) {
    $field = '<select name="' . zen_output_string($name) . '"';
    [B]if (strpos ($parameters, 'title="' === false) {[/B]
      $field .= ' title="'.zen_output_string($name).'"';//steve added
   [B] }
    if (strpos ($parameters, 'id="') === false) {[/B]
      $field .= ' id="'.zen_output_string($name).'"';//steve added
    }
    if (zen_not_null($parameters)) $field .= ' ' . $parameters;
 
    $field .= '>' . "\n";
 
    if (empty($default) && isset($GLOBALS[$name]) && is_string($GLOBALS[$name]) ) $default = stripslashes($GLOBALS[$name]);
 
    for ($i=0, $n=sizeof($values); $i<$n; $i++) {
      $field .= '  <option value="' . zen_output_string($values[$i]['id']) . '"';
     $field .= ' title="' . zen_output_string($values[$i]['text']) . '"';//steve added
      if ($default == $values[$i]['id']) {
        $field .= ' selected="selected"';
      }
 
      $field .= '>' . zen_output_string($values[$i]['text'], array('"' => '"', '\'' => ''', '<' => '<', '>' => '>')) . '</option>' . "\n";
    }
    $field .= '</select>' . "\n";
 
    if ($required == true) $field .= TEXT_FIELD_REQUIRED;
 
    return $field;
  }  
/*
 *  Output a form input field
 */
  function zen_draw_input_field($name, $value = '', $parameters = '', $type = 'text', $reinsert_value = true) {
    $field = '<input type="' . zen_output_string($type) . '" name="' . zen_sanitize_string(zen_output_string($name)) . '"';
    if ( (isset($GLOBALS[$name]) && is_string($GLOBALS[$name])) && ($reinsert_value == true) ) {
      $field .= ' value="' . zen_output_string(stripslashes($GLOBALS[$name])) . '"';
    } elseif (zen_not_null($value)) {
      $field .= ' value="' . zen_output_string($value) . '"';
    }
    [B]if (strpos ($parameters, 'id="') === false) {[/B]
      $field .= ' id="' . zen_output_string($name) . '"';//steve added for label "for"
    [B]}
    if (strpos ($parameters, 'title="') === false) {[/B]
      $field .= ' title="' . zen_output_string($value) . '"';//steve added for accessibility
   [B] }[/B]
    if (zen_not_null($parameters)) $field .= ' ' . $parameters;
 
    $field .= ' />';
 
    return $field;
  }  
16 Jun 2016, 9:10 AM
#5
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Re: tiles, ids for form elements

Thanks!
Finally got around to doing this on these items and on zen_draw_form...

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

16 Jun 2016, 12:17 PM
#6
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,140
Plugin Contributions:
56

Re: tiles, ids for form elements

It looks like Zen Cart 1.5.5 has included the changes identified above for the zen_draw_pull_down_menu function!

17 Jun 2016, 6:55 AM
#7
dw08gm avatar

dw08gm

Totally Zenned

Join Date:
Sep 2008
Location:
DownUnder, overlooking South Pole.
Posts:
1,022
Plugin Contributions:
2

Re: tiles, ids for form elements

This thread should be re-titled

titles, ids for form elements