Download & Extend

Syntax for XML patterns which enables defining numeric array keys in XML

Project:Patterns
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

I have created a pattern with a text field action:

    <field>
      <type>story</type>
      <name>country</name>
      <label>Country</label>
      <option>text</option>
      <widget>text_textfield</widget>
      <size>64</size>
      <weight>-2</weight>
      <max_length>128</max_length>
      <text_processing>0</text_processing>
      <default_value>abcd 1234</default_value>
    </field>

It runs ok but when I try to load the field settings page (admin/content/node-type/story/fields/field_country) the error is issued:

Fatal error: Cannot unset string offsets in /var/www/drupal/root/sites/all/modules/cck/includes/content.node_form.inc on line 60

The same action with no default_value tag doesn't break the field settings page

Comments

#1

In order for this action to work correctly it should be rewritten like this

    <field>
      <type>story</type>
      <name>country</name>
      <label>Country</label>
      <option>text</option>
      <widget>text_textfield</widget>
      <size>64</size>
      <weight>-2</weight>
      <max_length>128</max_length>
      <text_processing>0</text_processing>
      <default_value>
         <value><value>abcd 1234</value></value>
         <value></value>
      </default_value>
    </field>

This solution works vell but seems a bit awkward – all these <value> tags are not very human friendly.




It would be nice to improve readability of <default_value> definition having the single tag to do all the work

<default_value>abcd 1234</default_value>

#2

Take one more look at that weird construct

      <default_value>
         <value><value>abcd 1234</value></value>
         <value></value>     <====
      </default_value>

Here is the reason for the highlighted line to exist. The form expects the following array on the input: default_value[0]['value'].
How to implement this in xml? Numeric tags are not allowed:

      <default_value>
         <0>
             <value>abcd 1234</value>
         </0>
      </default_value>
      
     NOT VALID!




There is a way to have numeric keys in the output of a pattern - you have to place several tags with the same name within one parent.
That's why empty value tag is added

      <default_value>
         <value><value>abcd 1234</value></value>
         <value></value>
      </default_value>

gives
default_value[0]['value'] = 'abcd 1234';
default_value[1] = NULL;

      <default_value>
         <value><value>abcd 1234</value></value>
      </default_value>

while this gives default_value['value']['value'] = 'abcd 1234';




There is another way to have numeric keys in results, but for some reason it is applicable for scalar values only:

   <default_value>
      <value tag=”0”>abcd 123</value>         OK!
   </default_value>

gives default_value[0] = 'abcd 123':

   <default_value>
      <value tag=”0”>
           <value>abcd 123</value>             DOESN'T WORK!
      </value>
   </default_value>

and this doesn’t give default_value[0]['value'] = 'abcd 123':

#3

Title:Field action with default_value breaks a site» Syntax for XML patterns which enables defining numeric array keys in XML
Status:active» fixed

Thanks for taking your time to make such a detailed analysis. I created a workaround for this issue by introducing a new syntax for XML patterns that will produce numeric array keys. Here is an example:

      <default_value _numeric_keys="1">   
        <n0>                              
           <value>abcd 123</value>
        </n0>
      </default_value>     

When _numeric_keys="1" is specified, keys will be processed additionally and first character of the keys stripped out. This enables us to have valid XML while still producing numeric array keys after the XML is converted to PHP array. So, all you need to do is to prefix your numeric keys with a single character and make sure parent element has '_numeric_keys' attribute defined.

Alternative is to write your patterns in YAML. It's less verbose then XML and doesn't require workarounds like this one.

Also note that simplifying the syntax to

<default_value>abcd 1234</default_value>

is not viable option because different CCK fields use different syntax for default values and there would be too many exceptions that need to be handled and very difficult to make sure patterns code works for all the fields.

#4

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

nobody click here