Writing a Factory Function

A factory function creates other functions.

  • Suppose we want a function that creates links to Drupal project pages. It will allow us to refer to the project writeup using $writeup by creating the definition:
    $writeup=[[https://www.drupal.org/project/writeup writeup]]

Solution 1

  • Our first attempt at the factory function $project might look like this:
$parameters $project projid
$project={ // create shortcut for Drupal project
  ${$projid}=[[https://www.drupal.org/project/$projid $projid]]
  -_setting \$$projid=${$projid}.litdoc_         //this line is for testing purposes
}

It works fine with one project, but the function contains a reference to the project name, so because of lazy evaluation, $projid will pick up the value from the most recent factory function call. E.g.

$project writeup
  • setting $writeup=[[https://www.drupal.org/project/$projid $projid]]
$project views
  • setting $views=[[https://www.drupal.org/project/$projid $projid]]

Two very important Drupal projects are views (views) and writeup (views) —the writeup link is picking up the variable from views.

Solution 2: use $setval to force evaluation of project name

$parameters $project projid
$project={ // create shortcut for Drupal project
  $setval ${$projid}="[[https://www.drupal.org/project/$projid $projid]]"
  -_setting \$$projid=${$projid}.litdoc_          //this line is for testing purposes
}
$project writeup
  • setting $writeup=<a href="https://www.drupal.org/project/writeup">writeup</a>
$project views
  • setting $views=<a href="https://www.drupal.org/project/views">views</a>

Two very important Drupal projects are views (views) and writeup (writeup).

Final Solution allowing an optional extra project title

$parameters $project projid projtitle_line
$project={ // create shortcut for Drupal project
  $if !$projtitle_line
    $setval ${$projid}="[[https://www.drupal.org/project/$projid $projid]]"
  $else
    $setval ${$projid}="[[https://www.drupal.org/project/$projid $projtitle_line]] ([[https://www.drupal.org/project/$projid $projid]])"
  $endif
}
$project urllogin URL Login
$project views
$project writeup Writeup

Three very important Drupal projects are URL Login (urllogin), views and Writeup (writeup).

Extended Solution allowing project title on mouseover

$parameters $project projid projtitle_line
$project={ // create shortcut for Drupal project
 $if !$projtitle_line
   $projtitle_line=$projid
 $endif
 $set ${$projid}="$set $a_title='" $concatv $projtitle_line $concat "' [[https://www.drupal.org/project/" $concatv "$projid $projid" $concat "]]"
}
$project urllogin URL Login
$project views
$project writeup Writeup

Three very important Drupal projects are urllogin, views and writeup.