This patch adds an interface for generating variable, roles, and perms for a site based on existing configuration. The idea is to speed up the pattern generation process. It makes no attempt at generating a "complete" pattern that solves a particular configuration use case.

Its use case is generate some xml elements that can be pasted into a pattern.

A 4 minute walk through is at:

http://www.youtube.com/watch?v=NmwZmKS7S30

Comments

ChrisBryant’s picture

This is definitely a feature that we've been wanting to add for a good while now! Thanks for sharing your patch as well.

The direction that some if this goes may be influenced by some of the work being done in the Configuration Framework module. It's goal is to make this type of feature much easier.

Sarva and/or Vaish can provide more info about that, but if you haven't had a peek at that yet, please have a look.

Thanks again for sharing and I hope we can get this included asap!

johnbarclay’s picture

Sounds good. I'll keep an eye on the configuration framework before I add any more of these "generate" widgets. Thanks again for your work on this module and presenting at Drupalcon.

paulap’s picture

Title: Patch for Generating Variable, Role, and Perm Patterns from existing site. » Doesn't work for me - No Generate tab

Hi,

I use the latest dev version of patterns. I patched the module (cd to patterns module directory, patch < ....patch.

The patch created one file (patterns.generate.inc) and changed the menu-entries in the module file.

But nothing happend in the browser if i refresh the patterns page.

PS: i use drupal 6.13 and a lot of other modules...

Please help
wr
Gerald aka paulap

sarvab’s picture

If you have the module admin_menu, you could go to admin/settings/admin_menu and click the Wipe and Rebuild at the bottom. If not, maybe try changing a menu item anywhere admin/build/menu.

It can be a pain to have drupal recognize menu changes sometimes ;)

paulap’s picture

Title: Doesn't work for me - No Generate tab » Thx - SOLVED
sarvab’s picture

Status: Needs review » Closed (fixed)
sarvab’s picture

Status: Closed (fixed) » Needs review
sarvab’s picture

Title: Thx - SOLVED » Patch for Generating Variable, Role, and Perm Patterns from existing site.
johnbarclay’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new17.21 KB

This is the current version of this patch. I've been using it for a while and made minor changes and wanted to diff it against the current patterns module.

joestewart’s picture

The generate patch is working pretty well. A big help, thanks.

It doesn't handle variable arrays yet.

How should variables that don't contain strings ( integers only) as array keys be built? There is a great example at http://drupal.org/node/409180 of how to represent variable arrays with string keys.

johnbarclay’s picture

I changed it to deal with variables with associative arrays in them, but don't see the syntax for non-associative arrays.

The IMCE_profile variable is an example:

a:2:{i:1;a:9:{s:4:"name";s:6:"User-1";s:8:"filesize";i:0;s:5:"quota";i:0;s:7:"tuquota";i:0;s:10:"extensions";s:1:"*";s:10:"dimensions";s:9:"1200x1200";s:7:"filenum";i:0;s:11:"directories";a:1:{i:0;a:7:{s:4:"name";s:1:".";s:6:"subnav";i:1;s:6:"browse";i:1;s:6:"upload";i:1;s:5:"thumb";i:1;s:6:"delete";i:1;s:6:"resize";i:1;}}s:10:"thumbnails";a:3:{i:0;a:4:{s:4:"name";s:5:"Small";s:10:"dimensions";s:5:"90x90";s:6:"prefix";s:6:"small_";s:6:"suffix";s:0:"";}i:1;a:4:{s:4:"name";s:6:"Medium";s:10:"dimensions";s:7:"120x120";s:6:"prefix";s:7:"medium_";s:6:"suffix";s:0:"";}i:2;a:4:{s:4:"name";s:5:"Large";s:10:"dimensions";s:7:"180x180";s:6:"prefix";s:6:"large_";s:6:"suffix";s:0:"";}}}i:2;a:9:{s:4:"name";s:14:"Sample profile";s:8:"filesize";i:1;s:5:"quota";i:2;s:7:"tuquota";i:0;s:10:"extensions";s:16:"gif png jpg jpeg";s:10:"dimensions";s:7:"800x600";s:7:"filenum";i:1;s:11:"directories";a:1:{i:0;a:7:{s:4:"name";s:5:"u%uid";s:6:"subnav";i:0;s:6:"browse";i:1;s:6:"upload";i:1;s:5:"thumb";i:1;s:6:"delete";i:0;s:6:"resize";i:0;}}s:10:"thumbnails";a:1:{i:0;a:4:{s:4:"name";s:5:"Thumb";s:10:"dimensions";s:5:"90x90";s:6:"prefix";s:6:"thumb_";s:6:"suffix";s:0:"";}}}}

with recursion, a part of the variable becomes:

...
<thumbnails>
  <0>
  <name>Small</name>  
  <dimensions>90x90</dimensions>  
  <prefix>small_</prefix>  
  <suffix></suffix>  
  </0>  
  <1>
  <name>Medium</name>  
  <dimensions>120x120</dimensions>  
  <prefix>medium_</prefix>  
  <suffix></suffix>  </1>  
  <2>
  <name>Large</name>  
  <dimensions>180x180</dimensions>  
  <prefix>large_</prefix>  
  <suffix></suffix>  
</2>  
</thumbnails> 
...

where the 0, 1, 2 represent array indexes for non-associative arrays.

How should such a variable be represented in the xml? I'm afraid this feature is useless if it breaks some variables.

dan.nsk’s picture

Hi, John

It seems to me that it is impossible to represent multi level arrays with numeric keys in xml (at least when we are talking about the Patterns module).

I have found a workaround to have a numeric key for a scalar (non-array) value:

<var>
   <value tag='10'>aaaa</value>
   <value tag='11'>bbbb</value>
   <value tag='12'>cccc</value>
</var1>

this results in

$var[10] = 'aaaa';
$var[11] = 'bbbb';
$var[12] = 'cccc';

But this method doesn’t work when a numeric key corresponds to an array value:

<var>
   <value tag=’11’>
      <next_level_key_1>aaaa</next_level_key_1>
      <next_level_key_2>bbbb</next_level_key_2>
   </value>
   <value tag=’12’>
      <next_level_key_1>cccc</next_level_key_1>
      <next_level_key_2>dddd</next_level_key_2>
   </value>
</var1>

It would be nice for this structure to result in

$var[11]['next_level_key_1'] = 'aaaa';
$var[11]['next_level_key_2'] = 'bbbb';
$var[12]['next_level_key_1'] = 'cccc';
$var[12]['next_level_key_2'] = 'dddd';

but it does not.

It looks like that the only consistent way to represent an array with numeric keys is using YAML pattern syntax instead of XML.

R-H’s picture

Hey,

Just wanted to say thanks for the generate patch for this module! I got it installed pretty easily and love the extra functionality.

Is this the latest patch?

http://drupal.org/files/issues/generate_1.patch

What's the status of incorporating this into the module?

vaish’s picture

Johnbarclay, thanks for working on this patch. This is something we wanted to do long time ago but never got to it.

It seems that patch in #9 is not the latest one. Could you please provide your latest patch that handles associative arrays? I would like to test it and work on adding support for handling numerical arrays as well. Also, please check out #613270: Syntax for XML patterns which enables defining numeric array keys in XML where support for arrays with numerical keys has been added to patterns.

chriscalip’s picture

subscribe -note to self: dealing with settings on dev->staging->production

johnbarclay’s picture

Status: Reviewed & tested by the community » Needs work

I have to say that until there is the ability to deal with multilevel arrays, the patch is problematic. Someone could export and import such a variable and create some quite confusing problems.

Either it needs to be able to import and export such variables or that functionality should be taken out. That's why I'm changing the status back to needs work.

vaish’s picture

@johnbarclay - did you notice comment #14? We do have some ways of handling multilevel arrays so it would be nice to incorporate that in your patch and commit it. Would you be interested working on this further? If not, just post your latest patch and I'll work on it.

Thanks

johnbarclay’s picture

StatusFileSize
new17.12 KB

I doesn't support multilevel arrays in variables yet. Are you saying that you have an approach to processing their representation...or to generating their representation (or both)?

I'm attaching the current code. Its just a file and the following menu items. I can't do a patch well because I have some other changes like ldap_integration that are hard to pull out.

I can integrate the multilevel arrays if you have the code snippets that deal with them, otherwise if its easier for you to do it yourself that is fine.

 $items['admin/build/patterns/generate'] = array(
    'title' => 'Generate',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('patterns_generate_module'),
    'access arguments' => array('administer patterns'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'patterns.generate.inc',
  );
  
  $items['admin/build/patterns/generate/module'] = array(
    'title' => 'Single Module',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
  );
    
  $items['admin/build/patterns/generate/rolesandperms'] = array(
    'title' => 'Roles and Perms',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('patterns_generate_rolesandperms'),
    'access arguments' => array('administer patterns'),
    'type' => MENU_LOCAL_TASK,
    'weight' => -3,
    'file' => 'patterns.generate.inc',
  );
  
  $items['admin/build/patterns/generate/variables'] = array(
    'title' => 'Variables',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('patterns_generate_variables'),
    'access arguments' => array('administer patterns'),
    'type' => MENU_LOCAL_TASK,
    'weight' => -7,
    'file' => 'patterns.generate.inc',
  );
  
johnbarclay’s picture

StatusFileSize
new16.3 KB

I reworked this quite a bit; mostly to clean up the code. Attached is the patch.

To add support for multidimensional arrays, the "patterns_generate_array_to_xml" function in patterns.generate.inc will need the work.

If this approach works, I suppose some easy ones are taxonomy and menu for pattern generation.

vaish’s picture

Thanks for the latest patch. I'm going to be away for the rest of the week and I will be able to check it out only when I come back. In the meantime here is a few ideas regarding multidimensional arrays.

Recently I added support for numeric array keys by introducing the following workaround:

      <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. I don't think this is very pretty but it works until somebody comes up with some better solution.

Much cleaner solution would be to use YAML instead of XML as a format for generated patterns. YAML doesn't need any workarounds and additionally you can make your code simpler by relying on spyc library for converting PHP array into YAML.

  include('../spyc/spyc.php');
  $yaml = Spyc::YAMLDump($array);

Main disadvantage is that spyc library doesn't come bundled with the patterns module so you can't be sure if it will be available always.

Patterns now also support PHP as a patterns format so that should also be considered as an option.

Cheers,
Vaish

johnbarclay’s picture

Sounds good. I think before this gets committed, both yaml and xml should be supported. As well as the fix for multilevel arrays. I'll update this as I make progress.

vaish’s picture

@johnbarclay: It's ultimately your choice what to work on first, but if I would have to chose between this patch and improvements to patterns form helper, I would give higher priority to patterns form helper.

Thanks

ChrisBryant’s picture

@johnbarclay, have you made any progress on this work/patch since it was posted?

johnbarclay’s picture

No progress. I'm behind on rewriting the ldap module so likely won't get a chance. If anyone wants to take this up, I think a function in patterns called patterns_array_to($array, $mode = "yaml"), where $mode = yaml, xml, etc would solve the problem of the multidimensional array.

anciano’s picture

?