A better notation?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
chx's picture

I have a wild idea, but this is the place to post them.

Let's say you have

$form['level1']['level2']['#options'] = array(....);

instead, let's close the leveling with a ['_'] and omit the #.

So.

$form['level1'][''] = array('type' => 'checkboxes', 'options' => array('this', that'));
$form['level1']'level2']['
'] = another form API element here.

This would save a lot of #.

Some code:

  function element_children($element) {
    $keys = array_key($element);
    unset($keys['']);
    return $keys;
  }
  foreach (element_children($element) as $key) {
    $function($element[$key]['
']);
  }

Post 4.7, of course.

Comments

some regexpes do the change

chx's picture

$x = file_get_contents('modules/comment.module');
$x = preg_replace('/[([\'"])#.+\1]/U', "['']$0", $x);
$x = preg_replace('/(\$\S)(?=\s=\sarray.([\'"])#.+\2)/Us', "$1['
']", $x);
$x = preg_replace('/([\'"])#(.+)\1(?=\s*[=>]])/Us', "$1$2$1", $x);
file_put_contents('test.txt', $x);

i like it

adrian's picture

makes my life easier for a couple of other things too.

i know nobody likes constants, but instead of ['_'] (which looks like a little face =P ) , can't we use [e] , or [i] , which just is a lot easier to type.
I mentioned [i] as I would like to use the notation for other things too possibly, so elements is not the only thing you can place.

$form['level1']['_'] = array('type' => 'checkboxes', 'options' => array('this', that'));
versus :
$form['level1'][i] = array('type' => 'checkboxes', 'options' => array('this', that'));

--
The future is so Bryght, I have to wear shades.

we spent a lot of time already removing notices

chx's picture

and [i] will throw another. maybe even a warning. You want ['i']

Otherwise, whatever character you folks like. I liked the underscore.

Maybe we could use actual

merlinofchaos's picture

Maybe we could use actual constants from define() -- I don't like [i] or even [_] as a notation (add quotes as necessary) in part because they don't have any intrinsic meaning to me. If we can find a 3 letter define that has some meaning, I'd be a lot happier. 3 letter define would be the same amount of typing as 'i' too.

Form API

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: