Support Folks,<div><br></div><div>Need/Use-case: a very simple, lightweight UI for a customer to edit options on a select for a CCK field.<br><br>Idea to solve it. Create an unpublished node (linked to via for the customer via a custom site-admin block) whose body field are the select-list options. One option per line.<br>
<br>In the CCK field I&#39;m using the PHP option to return an array to create the allowed options:<br><br>This following code works... but is not quite what I want:<div>(I know that the client input part should be sanitized for security... but I&#39;m just trying to get it to work before I do that part.)<br>
<div><br><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">$sql = &quot;SELECT body FROM {node_revisions} WHERE nid=1274 ORDER BY timestamp DESC LIMIT 1&quot;;<br>$res = db_query($sql);<br>$node = db_fetch_object($res);<br>
$arr = explode(&quot;\n&quot;,$node-&gt;body);<br>return $arr</font><br><br></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; "><font class="Apple-style-span" face="arial, helvetica, sans-serif">from a node body field that looks like:</font><br>
<br>Cats<br>Dogs<br>Goats</span></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-style-span" style="font-family: arial; ">It returns an indexed array like</span></font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><font class="Apple-style-span" face="arial"><br></font>Array<br>(<br>    [0] =&gt; Cats<br>    [1] =&gt; Dogs<br>    [2] =&gt; Goats<br><br>)</font><br>
<br><b>Problem:</b> What gets stored in the database is the integer key from the indexed array created by explode(). The data integrity is thus completely dependent upon the order of the items listed. I don&#39;t want that. I want people entering the data to be able to change the order. <i>I want an associative array in which the keys and labels are the same.</i><br>
<br>Note that if, via the field configuration UI, I simply list the values in the allowed values box... Drupal turns that list into an associative array in which the keys and values are the same. Exactly what I want! But I don&#39;t know how to create that for the custom PHP value set. I&#39;m putting the code here from CCK where this happens. I don&#39;t quite get it.<br>
<br>Any hints would be most appreciated,<br><br>Shai<br><br><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">/**<br> *  Create an array of the allowed values for this field.<br> *<br> *  Used by number and text fields, expects to find either<br>
 *  PHP code that will return the correct value, or a string<br> *  with keys and labels separated with &#39;|&#39; and with each<br> *  new value on its own line.<br> *<br> * @param $field<br> *   The field whose allowed values are requested.<br>
 * @param $flatten<br> *   Optional. Use TRUE to return a flattened array (default).<br> *   FALSE can be used to support optgroups for select widgets<br> *   when allowed values list is generated using PHP code.<br> */<br>
function content_allowed_values($field, $flatten = TRUE) {<br>  static $allowed_values;<br><br>  $cid = $field[&#39;field_name&#39;] .&#39;:&#39;. ($flatten ? &#39;1&#39; : &#39;0&#39;);<br>  if (isset($allowed_values[$cid])) {<br>
    return $allowed_values[$cid];<br>  }<br><br>  $allowed_values[$cid] = array();<br><br>  if (isset($field[&#39;allowed_values_php&#39;])) {<br>    ob_start();<br>    $result = eval($field[&#39;allowed_values_php&#39;]);<br>
    if (is_array($result)) {<br>      if ($flatten) {<br>        $result = content_array_flatten($result);<br>      }<br>      $allowed_values[$cid] = $result;<br>    }<br>    ob_end_clean();<br>  }<br><br>  if (empty($allowed_values[$cid]) &amp;&amp; isset($field[&#39;allowed_values&#39;])) {<br>
    $list = explode(&quot;\n&quot;, $field[&#39;allowed_values&#39;]);<br>    $list = array_map(&#39;trim&#39;, $list);<br>    $list = array_filter($list, &#39;strlen&#39;);<br>    foreach ($list as $opt) {<br>      // Sanitize the user input with a permissive filter.<br>
      $opt = content_filter_xss($opt);<br>      if (strpos($opt, &#39;|&#39;) !== FALSE) {<br>        list($key, $value) = explode(&#39;|&#39;, $opt);<br>        $allowed_values[$cid][$key] = (isset($value) &amp;&amp; $value !==&#39;&#39;) ? $value : $key;<br>
      }<br>      else {<br>        $allowed_values[$cid][$opt] = $opt;<br>      }<br>    }<br>    // Allow external modules to translate allowed values list.<br>    drupal_alter(&#39;content_allowed_values&#39;, $allowed_values[$cid], $field);<br>
  }<br>  return $allowed_values[$cid];<br>}</font></div></div></div>