Field action with default_value breaks a site

dan.nsk - October 24, 2009 - 03:59
Project:Patterns
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

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

#1

dan.nsk - October 24, 2009 - 20:15

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

dan.nsk - October 24, 2009 - 20:57

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':

 
 

Drupal is a registered trademark of Dries Buytaert.